using Ice;
using System;
using System.Threading;
using System.Threading.Tasks;
using BattleIce;

namespace ET.Server
{
    [FriendOf(typeof (BattleIceAgentComponent))]
    public static class BattleIceAgentComponentSystem
    {
        public class BattleIceAgentComponentAwakeSystem: AwakeSystem<BattleIceAgentComponent>
        {
            protected override void Awake(BattleIceAgentComponent self)
            {
                BattleIceAgentComponent.Instance = self;

                Thread thread = new Thread(() =>
                {
                    IceApp IceApp = new IceApp();
                    IceApp.main(new string[] {}, "../Config/ice.config");
                });
                thread.Start();
            }
        }

        public class BattleIceAgentComponentDestroySystem: DestroySystem<BattleIceAgentComponent>
        {
            protected override void Destroy(BattleIceAgentComponent self)
            {
                Log.Info($"Ice component destroyed");
                Application.communicator().Dispose();
            }
        }

        [ObjectSystem]
        [FriendOf(typeof (BattleIceAgentComponent))]
        private class IceApp: Application
        {
            public override int run(string[] args)
            {
                try
                {
                    ZoneManagerPrx ZoneIce = ZoneManagerPrxHelper.checkedCast(communicator().propertyToProxy("zoneManager.Proxy"));
                    if (ZoneIce == null)
                    {
                        Log.Error("not found ice proxy: zoneManager");
                        return 1;
                    }

                    BattleIceAgentComponent.Instance.IceZoneManager = ZoneIce;
                    Log.Debug("got zoneManager.Proxy");
                    ZoneIce.ice_invocationTimeout(15);

                    ObjectAdapter adapter = communicator().createObjectAdapter("");
                    ObjectPrx prx = adapter.add(new ZoneManagerCallback(), communicator().stringToIdentity("bs-" + ConstGame.GameServerId));
                    ZoneIce.ice_getCachedConnection().setAdapter(adapter);
                    int ret = ZoneIce.setCallback(prx.ice_getIdentity(), ConstGame.GameServerId.ToString());
                    if (ret < 0)
                    {
                        Log.Error($"ice connect error: {ret}");
                        return 2;
                    }

                    //向战斗服注册本GameServer
                    string res = ZoneIce.registerGameServer(ConstGame.GameServerId, 0);
                    BattleIceAgentComponent.Instance.StrBattleServerVersion = res;
                    Log.Info($"Battle Server version:  {res}");
                }
                catch (Ice.Exception e)
                {
                    Log.Error(e.Message);
                    return 3;
                }

                try
                {
                    XmdsManagerPrx XmdsIce = XmdsManagerPrxHelper.checkedCast(communicator().propertyToProxy("XmdsManager.Proxy"));
                    if (XmdsIce == null)
                    {
                        Log.Error("not found ice proxy: XmdsManager");
                        return 4;
                    }

                    Log.Debug("got XmdsManager.Proxy");
                    BattleIceAgentComponent.Instance.IceXmdsManager = XmdsIce;
                    XmdsIce.ice_invocationTimeout(15);
                }
                catch (Ice.Exception e)
                {
                    Log.Error(e.Message);
                    return 5;
                }

                //连接战斗服FastStream组件
                BattleIceAgentComponent.Instance.DomainScene().AddComponent<FastStreamComponent>();

                IceApp.communicator().waitForShutdown();
                Log.Info("ice thread end");
                return 0;
            }
        }

        [ObjectSystem]
        private class ZoneManagerCallback: ZoneManagerCallbackDisp_
        {
            public override void eventNotify(string eventType, string msg, Current current__)
            {
                Log.Debug("======================================");
                Log.Debug($"battleServer zone notify: type({eventType}), msg({msg})");
                Log.Debug("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^");

                //TODO:GWorld.java @ battleServerEvent
                if (eventType == "areaEvent" || eventType == "zoneEvent")
                {
                }
                else if (eventType == "playerEvent")
                {
                }
                else if (eventType == "mapNotify")
                {
                }
                else if (eventType == "taskEvent")
                {
                }
            }
        }
    }
}