using CommonAI.Zone;
using CommonLang.Geometry;
using ET.Client;
using ET.EventType;
using ET.Server;
using System;
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)
        {
            Log.Debug($"Battle Func:{a.FuncIndex}");

            var session = PlayerComponent.Instance.ClientScene().GetComponent<SessionComponent>().Session;
            switch (a.FuncIndex)
            {
                case (int)BattleFunc.FUNC.Start:
                    session.Call(new C2G_BattleNotify() { Message = BattleNotify.ClientIsReady.ToString() }).Coroutine();
                    session.Call(new C2G_BattleNotify()
                    {
                        Message = BattleNotify.StartRefreshMonster.ToString()
                    }).Coroutine();
                    break;
                case (int)KeyCode.F1:
                    var units = new int[] { 101, 111, 121, 131 };
                    var centerpos = GetCurBattleCenter();
                    var rand = new Random();
                    foreach (var id in units)
                    {
                        float r = rand.Next(1000) / 30.0f + 8;
                        double ang = rand.Next(120) / 180.0f * Math.PI + Math.PI;
                        //Log.Debug($"rand unit r({r}), ang({ang})");
                        float x = centerpos.X + (float)(r * Math.Cos(ang));
                        float y = centerpos.Y - (float)(r * Math.Sin(ang));
                        //Log.Debug($"born unit: r:{r}, ang:{ang}---({x},{y})");

                        session.Call(new C2G_AddUnitsToMap()
                        {
                            UnitId = id,
                            Force = 1,
                            X = (int)x,
                            Y = (int)y
                        }).Coroutine();
                    }
                    //EventSystem.Instance.Publish(BattleMsgEvent.Static.Clone("'<font color=#fe3824>宝宝x</font>'进入了游戏,和大家一起守护家园!"));
                    break;
                case (int)KeyCode.F2:
                    centerpos = GetCurBattleCenter();
                    var units2 = new int[] { 102, 112, 122, 132 };
                    rand = new Random();
                    for (int i = 0; i < 2; i++)
                    {
                        var id = units2[rand.Next(units2.Length)];
                        float r = rand.Next(1000) / 30.0f + 8;
                        double ang = rand.Next(120) / 180.0f * Math.PI + Math.PI;
                        float x = centerpos.X + (float)(r * Math.Cos(ang));
                        float y = centerpos.Y - (float)(r * Math.Sin(ang));

                        session.Call(new C2G_AddUnitsToMap()
                        {
                            UnitId = id,
                            Force = 1,
                            X = (int)x,
                            Y = (int)y
                        }).Coroutine();
                    }
                    //EventSystem.Instance.Publish(BattleMsgEvent.Static.Clone("'<font color=#fe2c55>宝宝x</font>'使用了'<font color=#fe3824>甜甜圈</font>',成功变形为'二级宝宝'"));
                    break;
                case (int)KeyCode.F3:
                    centerpos = GetCurBattleCenter();
                    var units3 = new int[] { 103, 113, 123, 133 };
                    rand = new Random();
                    for (int i = 0; i < 1; i++)
                    {
                        var id = units3[rand.Next(units3.Length)];
                        float r = rand.Next(1000) / 30.0f + 8;
                        double ang = rand.Next(120) / 180.0f * Math.PI + Math.PI;
                        float x = centerpos.X + (float)(r * Math.Cos(ang));
                        float y = centerpos.Y - (float)(r * Math.Sin(ang));

                        session.Call(new C2G_AddUnitsToMap()
                        {
                            UnitId = id,
                            Force = 1,
                            X = (int)x,
                            Y = (int)y
                        }).Coroutine();
                    }
                    //EventSystem.Instance.Publish(BattleMsgEvent.Static.Clone("'<font color=#fe2c55>宝宝x</font>'使用了'<font color=#fe3824>甜甜圈</font>',成功变形为'三级宝宝'"));
                    break;

                case (int)KeyCode.F4:
                    session.Call(new C2G_BattleNotify()
                    {
                        Message = BattleNotify.TiktokGift_1.ToString()
                    }).Coroutine();
                    break;
                case (int)KeyCode.F5:
                    session.Call(new C2G_BattleNotify()
                    {
                        Message = BattleNotify.TiktokGift_10.ToString()
                    }).Coroutine();
                    break;
                case (int)KeyCode.F6:
                    session.Call(new C2G_BattleNotify()
                    {
                        Message = BattleNotify.TiktokGift_52.ToString()
                    }).Coroutine();
                    break;
                case (int)KeyCode.F7:
                    session.Call(new C2G_BattleNotify()
                    {
                        Message = BattleNotify.TiktokGift_99.ToString()
                    }).Coroutine();
                    break;
                case (int)KeyCode.F8:
                    session.Call(new C2G_BattleNotify()
                    {
                        Message = BattleNotify.TiktokGift_199.ToString()
                    }).Coroutine();
                    break;
                case (int)KeyCode.F9:
                    session.Call(new C2G_BattleNotify()
                    {
                        Message = BattleNotify.TiktokGift_520.ToString()
                    }).Coroutine();
                    break;
                case (int)KeyCode.F10:
                    break;
            }
            await ETTask.CompletedTask;
        }

        //获得当前战场focus中心(当前塔位置)
        private Vector2 vecTemp = new Vector2();
        private Vector2 GetCurBattleCenter()
        {
            foreach(var tid in ConstGame.TowerTemplateIDs)
            {
                var tower = BattleMgr.Instance.GetUnitByTemplateID(tid);
                if(tower != null && !tower.IsDead)
                {
                    vecTemp.X = tower.ZUnit.X;
                    vecTemp.Y = tower.ZUnit.Y;
                    return vecTemp;
                }
            }
            return new Vector2(0, 0);
        }
    }

    //发送战斗服指令相关
    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);
        }
    }

}