using Beginor.Owin.StaticFile;
using JinianNet.JNTemplate;
using Microsoft.Extensions.Configuration;
using Microsoft.Owin.Hosting;
using Owin;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net.Http.Formatting;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Http;
using 电子展板.Utility.Extension;
using 电子展板.Utility.Logs;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace 电子展板
{
    public class WebServer
    {
        private static IDisposable host;
        public static void Start()
        {
            LogHelper.Instance.Info("正在开启Web服务");
            StartOptions startOptions = new StartOptions();
            startOptions.Urls.Add($"http://*:80/");
            host = WebApp.Start<Startup>(startOptions);
        }

        public static void Stop()
        {
            if (host == null)
                return;
            LogHelper.Instance.Info("正在停止Web服务");
            host.Dispose();
            host = null;
        }
    }


    /// <summary>
    /// Web启动类
    /// </summary>
    public class Startup
    {
        private HttpConfiguration _config;
        /// <summary>
        /// 配置
        /// </summary>
        /// <param name="app"></param>
        /// <param name="env"></param>
        public void Configuration(IAppBuilder app)
        {
            _config = new HttpConfiguration();
            _config.MapHttpAttributeRoutes();
            _config.Formatters.XmlFormatter.SupportedMediaTypes.Clear();
            _config.Formatters.JsonFormatter.MediaTypeMappings.Add(new QueryStringMapping("datatype", "json", "application/json"));
            app.UseWebApi(_config);
            //配置模板引擎
            Engine.Configure(c =>
            {
                c.TagPrefix = "${";
                c.TagSuffix = "}";
                c.TagFlag = '^';
            });
        }
    }
}
