using ET.EventType;
using FairyGUI;

namespace ET.Client
{
    public class HUDComonent
    {
        public static GComponent Root;
        public static GProgressBar pgTower;
        public static GProgressBar pgBoss1;
        public static GProgressBar pgBoss2;
        public static GProgressBar pgBoss3;
        public static GProgressBar pgTKLike;
        public static GList listRank;
        public static GComponent[] listGift = new GComponent[6];
    }

    [Event]
    public class ShowHUDEventHandler : BEvent<ShowHUDEvent>
    {
        protected override async ETTask OnEvent(ShowHUDEvent _)
        {
            var view = await UIHelper.Create( "HUD" );
            HUDComonent.Root = view;

            HUDComonent.pgTower = view.GetChild("HPBarTower") as GProgressBar;
            HUDComonent.pgBoss1 = view.GetChild("HPBarBoss1") as GProgressBar;
            HUDComonent.pgBoss2 = view.GetChild("HPBarBoss2") as GProgressBar;
            HUDComonent.pgBoss3 = view.GetChild("HPBarBoss3") as GProgressBar;
            HUDComonent.pgTKLike = view.GetChild("EnergyBar") as GProgressBar;
            HUDComonent.listRank = view.GetChild("list_rank").asList;
            for( int i = 1; i <= 6; i++ )
            {
                HUDComonent.listGift[i - 1] = view.GetChild($"CompGift{i}").asCom;
            }

            HUDComonent.pgTower.visible = false;
            HUDComonent.pgBoss1.visible = false;
            HUDComonent.pgBoss2.visible = false;
            HUDComonent.pgBoss3.visible = false;
            HUDComonent.pgTKLike.visible = false;

            var compIcon = view.GetChild("CompGifticon");
            var compTips = view.GetChild("CompTips");
            var btn = view.GetChild("btn_start");
            btn.onClick.Set(() =>
            {
                btn.visible = false;
                compIcon.visible = false;
                compTips.visible = false;

                CameraMgr.PlayStartAnimation(() =>
                {
                    compIcon.visible = true;
                    EventSystem.Instance.Publish(BattleFunc.Clone((int)BattleFunc.FUNC.Start));
                }).Coroutine();
            });

            EventSystem.Instance.Publish(BattleFunc.Clone((int)BattleFunc.FUNC.ClientIsReady));
        }
    }

    /*[Event]
    public class SkillChangeEventHandler : BEvent<SkillChangeEvent>
    {
        protected override async ETTask OnEvent(SkillChangeEvent a)
        {
            Log.Debug("actor's skill changed.");
            await ETTask.CompletedTask;
        }
    }*/

    [Event]
    public class HPRefreshEventHandler : BEvent<HPRefresh>
    {
        protected override async ETTask OnEvent(HPRefresh a)
        {
            var view = UIHelper.GetUI("HUD") as GComponent;
            if (view != null)
            {
                GProgressBar progress = null;
                switch (a.HPIndex)
                {
                    case HPRefresh.Index.Tower:
                        progress = HUDComonent.pgTower;
                        break;
                    case HPRefresh.Index.Boss1:
                        progress = HUDComonent.pgBoss1;
                        break;
                    case HPRefresh.Index.Boss2:
                        progress = HUDComonent.pgBoss2;
                        break;
                    case HPRefresh.Index.Boss3:
                        progress = HUDComonent.pgBoss3;
                        break;
                    case HPRefresh.Index.TiktokLike:
                        progress = HUDComonent.pgTKLike;
                        break;
                }
                
                progress.visible = a.Visible;
                if (a.Visible)
                {
                    progress.value = a.Progress;
                    var name = progress.GetChild("name");
                    if (name != null)
                    {
                        name.text = a.Name;
                    }
                    var txt = progress.GetChild("title");
                    if (txt != null)
                    {
                        txt.text = a.Title;
                    }
                }
            }
            await ETTask.CompletedTask;
        }
    }

    [Event]
    public class RankEventHandler : BEvent<RankChangeEvent>
    {
        protected override async ETTask OnEvent(RankChangeEvent a)
        {
            var list = HUDComonent.listRank;
            if (list == null) return;

            list.visible = a.InfoList != null && a.InfoList.Count > 0;
            if(list.visible)
            {
                int i = 0;
                for (; i < a.InfoList.Count; i++)
                {
                    var chd = list.GetChildAt(i).asCom;
                    if(chd == null)
                    {
                        return;
                    }
                    var txt = chd.GetChild("text");
                    txt.text = a.InfoList[i].Name;
                    chd.visible = true;
                }
                for(; i < 3; i++)
                {
                    var chd = list.GetChildAt(i);
                    if(chd != null)
                    {
                        chd.visible = false;
                    }
                }
            }

            await ETTask.CompletedTask;
        }
    }
    [Event]
    public class ShowUIAnimationHandler : BEvent<ShowUIAnimation>
    {
        protected override async ETTask OnEvent(ShowUIAnimation a)
        {
            var view = HUDComonent.Root;
            if (view == null) return;

            switch(a.Type)
            {
                case ShowUIAnimation.AniType.Gift:
                    var trans = view.GetTransition($"t_gift_00{a.Param1}");
                    if (trans != null)
                    {
                        trans.Play();
                    }
                    else
                    {
                        Log.Error($"NotFound UIAni: {a.Type},{a.Param1}");
                    }
                    break;
                case ShowUIAnimation.AniType.Monster:
                    trans = view.GetTransition("t_warning");
                    trans.Play();

                    trans = view.GetTransition($"t_warning_00{a.Param1}");
                    if (trans != null)
                    {
                        trans.Play();
                    }
                    else
                    {
                        Log.Error($"NotFound UIAni: {a.Type},{a.Param1}");
                    }
                    break;
                case ShowUIAnimation.AniType.Tower:
                    if (a.Param1 == 0)
                    {
                        view.GetTransition("t_help").Play();
                    }
                    else if (a.Param1 == 1)
                    {
                        view.GetTransition("t_jiayuangaoji").Play();
                    }
                    break;
                case ShowUIAnimation.AniType.LikeEnergy:
                    HUDComonent.pgTKLike.visible = true;
                    HUDComonent.pgTKLike.value = a.Param1;
                    trans = HUDComonent.pgTKLike.GetTransition("t0");
                    trans.Play();
                    break;
                default:
                    Log.Error($"unknow ui animation: {a.Type}");
                    break;
            }

            await ETTask.CompletedTask;
        }
    }
    [Event]
    public class TiktokGiftEventHandler : BEvent<TiktokGiftEvent>
    {
        protected override async ETTask OnEvent(TiktokGiftEvent a)
        {
            var comp = HUDComonent.listGift[a.index - 1];
            comp.GetChild("Text1").text = a.nickname;
            comp.GetChild("Text2").text = "X" + a.num;
            var trans = HUDComonent.Root.GetTransition($"t_gift_00{a.index}");
            if (trans != null)
            {
                trans.Play();
            }
            await ETTask.CompletedTask;
        }
    }
    [Event]
    public class ShowOrHideHeadBarHandler : BEvent<ShowOrHideHeadBar>
    {
        protected override async ETTask OnEvent(ShowOrHideHeadBar a)
        {
            if(a != null)
            {
                GlobalViewMgr.Instance.HeadbarView.visible = a.Flag;
            }
            else
            {
                GlobalViewMgr.Instance.HeadbarView.visible = !GlobalViewMgr.Instance.HeadbarView.visible;
            }
            await ETTask.CompletedTask;
        }
    }
    [Event]
    public class SoundMuteEventHandler : BEvent<SoundMuteEvent>
    {
        protected override async ETTask OnEvent(SoundMuteEvent a)
        {
            bool isMute = SoundManager.Instance.UnityAudioSource.mute;
            if (a != null)
            {
                isMute = a.Flag;
            }
            else
            {
                isMute = !isMute;
            }
            SoundManager.Instance.UnityAudioSource.mute = isMute;
            GameSetting.Instance.SetBool(GameSetting.Sets.Mute_int, isMute);
            await ETTask.CompletedTask;
        }
    }

}