using FLY.Thick.FilmCasting.Server;
using FLY.Thick.FilmCasting.Server.OBJProxy;
using FLY.Thick.FilmCasting.Server.WsProxy;
using FLY.Thick.FilmCasting.WebAPI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Unity;
using WebSocketSharp.Server;

namespace FLY.Thick.FilmCasting.UI.Server
{
    internal class Launcher
    {
        TDGage mGage;
        OBJProxys mOBJProxy;
        ThkWebApiServer thkWebApiServer;
        /// <summary>
        /// WebSocket 服务
        /// </summary>
        WebSocketServer wss;
        WsProxys thkWsProxys;

        //用于界面
        public IUnityContainer container;

        public Launcher()
        {
            container = new UnityContainer();
        }

        public async Task Init()
        {
            //更改路径
            string rootDir = "Gage1";
            if (!System.IO.Directory.Exists(rootDir))
                System.IO.Directory.CreateDirectory(rootDir);

            System.Environment.CurrentDirectory = rootDir;

            mGage = new TDGage();
            await mGage.Init();

            mOBJProxy = new OBJProxys(0, mGage);

            FObjBase.FObjSys.Currents[0].Start_Conn_Server(
                Misc.StringConverter.ToIPEndPoint(mGage.sysParam.Addr));

            FObjBase.PollModule.Current.Start();


            thkWebApiServer = new ThkWebApiServer();
            thkWebApiServer.Init(mGage);
            thkWebApiServer.Start();

            wss = new WebSocketServer($"ws://{mGage.sysParam.WsAddr}/");
            thkWsProxys = new WsProxys(wss, mGage);
            wss.Start();


            //----------------------------------------------------------------------------
            //注册容器
            container.RegisterInstance(mGage);
            container.RegisterInstance(thkWebApiServer);
            container.RegisterInstance(mGage.sysParam);
            container.RegisterInstance(mGage.flyAd);
            container.RegisterInstance(wss);
            container.RegisterInstance(mGage.errnoList);
            container.RegisterInstance(mGage.warning);
        }

    }
}
