using CommonAI.Zone;
using CommonLang.Geometry;
using ET.Client;
using ET.EventType;
using ET.Server;
using System;
using System.Collections.Generic;
using UnityEngine;
using Random = System.Random;
using Vector2 = CommonLang.Geometry.Vector2;

namespace ET
{
    [Event]
    public class BattleFuncHandler : BEvent<BattleFunc>
    {
        protected override async ETTask OnEvent(BattleFunc a)
        {
            if(PlayerComponent.Instance == null || PlayerComponent.Instance.ClientScene() == null || PlayerComponent.Instance.ClientScene().GetComponent<SessionComponent>() == null)
            {
                return;
            }
            Log.Debug($"Battle Func:{a.FuncIndex}");

            var session = PlayerComponent.Instance.ClientScene().GetComponent<SessionComponent>().Session;
            switch (a.FuncIndex)
            {
                case (int)BattleFunc.FUNC.ClientIsReady:
                    session.Call(new C2G_BattleNotify()
                    {
                        Message = BattleNotify.ClientIsReady.ToString()
                    }).Coroutine();
                    break;
                case (int)BattleFunc.FUNC.Start:
                    session.Call(new C2G_BattleNotify()
                    {
                        Message = BattleNotify.StartRefreshMonster.ToString()
                    }).Coroutine();
                    break;
                case (int)BattleFunc.FUNC.Start2:
                    session.Call(new C2G_BattleNotify()
                    {
                        Message = BattleNotify.StartRefreshMonster2.ToString()
                    }).Coroutine();
                    break;
                case (int)BattleFunc.FUNC.Start3:
                    session.Call(new C2G_BattleNotify()
                    {
                        Message = BattleNotify.StartRefreshMonster3.ToString()
                    }).Coroutine();
                    break;
                case (int)KeyCode.F1:
                    session.Call(new C2G_BattleNotify() { Message = "test:8" }).Coroutine();
                    break;
                case (int)KeyCode.F2:
                    session.Call(new C2G_BattleNotify() { Message = "test:1" }).Coroutine();
                    break;
                case (int)KeyCode.F3:
                    session.Call(new C2G_BattleNotify() { Message = "test:2" }).Coroutine();
                    break;
                case (int)KeyCode.F4:
                    session.Call(new C2G_BattleNotify() { Message = "test:3" }).Coroutine();
                    break;
                case (int)KeyCode.F5:
                    session.Call(new C2G_BattleNotify() { Message = "test:4" }).Coroutine();
                    break;
                case (int)KeyCode.F6:
                    session.Call(new C2G_BattleNotify() { Message = "test:5" }).Coroutine();
                    break;
                case (int)KeyCode.F7:
                    session.Call(new C2G_BattleNotify() { Message = "test:6" }).Coroutine();
                    break;
                case (int)KeyCode.F8:
                    session.Call(new C2G_BattleNotify() { Message = "test:7" }).Coroutine();
                    break;
                
            }
            await ETTask.CompletedTask;

            //EventSystem.Instance.Publish<ShowOrHideHeadBar>();
            //EventSystem.Instance.Publish<SoundMuteEvent>();
        }

        //获得当前战场(当前塔位置),随机位置
        public static int BattleCenterIndex = 0;
        private Vector2 vecTemp = new Vector2();
        private Vector2 GetRandomPos()
        {
            Vector2[] TowerPos = { new Vector2(){ X = 103, Y = 197 }, new Vector2() { X = 190, Y = 133 }, new Vector2() { X = 104, Y = 69 } };

            var tower = TowerPos[BattleCenterIndex];
            var rand = new Random();
            float r;
            double ang;
            if(BattleCenterIndex == 0)
            {
                r = (float)Math.Sqrt(rand.Next(3025)) + 10;
                ang = rand.Next(22) / 180.0f * Math.PI + Math.PI * 77f / 180f + Math.PI;
            }
            else if(BattleCenterIndex == 1)
            {
                r = (float)Math.Sqrt(rand.Next(3136)) + 10;
                ang = rand.Next(20) / 180.0f * Math.PI + Math.PI * 76f / 180f;
            }
            else
            {
                r = (float)Math.Sqrt(rand.Next(2809)) + 10;
                ang = rand.Next(22) / 180.0f * Math.PI + Math.PI * 148f / 180f;
            }

            vecTemp.X = tower.X + (float)(r * Math.Cos(ang));
            vecTemp.Y = tower.Y - (float)(r * Math.Sin(ang));
            return vecTemp;
        }
    }

    //测试代码,检测当前战场中心
    [Event]
    public class Test1EventHandler : BEvent<CameraPlaybleEvent>
    {
        protected override async ETTask OnEvent(CameraPlaybleEvent a)
        {
            if (a.Playable == "Camera1_2")
            {
                BattleFuncHandler.BattleCenterIndex = 1;
            }
            else if (a.Playable == "Camera2_3")
            {
                BattleFuncHandler.BattleCenterIndex = 2;
            }
            await ETTask.CompletedTask;
        }
    }

    [Event]
    public class Test2EventHandler : BEvent<EventType.GameoverEvent>
    {
        protected override async ETTask OnEvent(EventType.GameoverEvent args)
        {
            BattleFuncHandler.BattleCenterIndex = 0;
            await ETTask.CompletedTask;
        }
    }

    //发送战斗服指令相关
    public partial class BattleMgr
    {
        private bool autoFight;
        public bool AutoFight
        {
            get
            {
                return autoFight;
            }
            set
            {
                if(value == autoFight) return;
                else
                {
                    autoFight = value;
                }
            }
        }

        public CommonAI.Zone.Action SetAutoFight(bool flag)
        {
            return new UnitGuardAction(UnitMgr.Instance.ActorId, flag, CommonAI.XmdsConstConfig.AUTO_GUARD_MODE_POINT);
        }
    }

}