using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace ET.Client
{
    [FriendOf(typeof(UIMainComponent))]
	public static class UIMainComponentSystem
	{
        [ObjectSystem]
		public class UIMainComponentAwakeSystem : AwakeSystem<UIMainComponent>
		{
			protected override async void Awake(UIMainComponent self, params object[] param)
			{
                ReferenceCollector rc = self.GetParent<UI>().GameObject.GetComponent<ReferenceCollector>();
				self.createRoomBtn = rc.Get<GameObject>("createRoomBtn");
                self.createRoomTxt = rc.Get<GameObject>("createRoomTxt");
                self.joinRoomBtn = rc.Get<GameObject>("joinRoomBtn");
                self.clubRoomBtn = rc.Get<GameObject>("clubBtn");
                self.playerIcon = rc.Get<GameObject>("playerIcon");
                self.playerId = rc.Get<GameObject>("playerId");
                self.playerName = rc.Get<GameObject>("playerName");
                self.playerLevel = rc.Get<GameObject>("playerLevel");
                self.vipVaule = rc.Get<GameObject>("vipVaule");
                self.expVaule = rc.Get<GameObject>("expVaule");

                self.createRoomBtn.OnClick(() => { self.OnCreateRoom(); });
                //self.createRoomBtn.GetComponent<Button>().onClick.AddListener(() => { self.OnCreateRoom(); });
                self.joinRoomBtn.GetComponent<Button>().onClick.AddListener(() => { self.OnJoinRoom(); });
                self.clubRoomBtn.GetComponent<Button>().onClick.AddListener(() => { self.OnClub(); });
                self.playerIcon.GetComponent<Button>().onClick.AddListener(() => { self.OnPlayer(); });
                var roleInfo = self.ClientScene().GetComponent<RoleInfoComponment>().GetRoleInfo();
                self.playerName.GetComponent<Text>().text = roleInfo.name;
                self.playerId.GetComponent<Text>().text = roleInfo.roleId.ToString();
                self.vipVaule.GetComponent<Text>().text = roleInfo.vip.ToString();
                self.playerLevel.GetComponent<Text>().text = "LV." + roleInfo.level.ToString();
                self.expVaule.GetComponent<Image>().fillAmount = roleInfo.exp / 100f;

                ///down btn////
                self.shopBtn = rc.Get<GameObject>("shopBtn");
                self.settingBtn = rc.Get<GameObject>("settingBtn");
                self.ruleBtn = rc.Get<GameObject>("ruleBtn");
                self.statsBtn = rc.Get<GameObject>("statsBtn");
                self.shareBtn = rc.Get<GameObject>("shareBtn");
                self.avtiveBtn = rc.Get<GameObject>("avtiveBtn");
                self.shopBtn.GetComponent<Button>().onClick.AddListener(() => { self.OnShop(); });
                self.settingBtn.GetComponent<Button>().onClick.AddListener(() => { self.OnSetting(); });
                self.ruleBtn.GetComponent<Button>().onClick.AddListener(() => { self.OnRule(); });
                self.statsBtn.GetComponent<Button>().onClick.AddListener(() => { self.OnStats(); });
                self.avtiveBtn.GetComponent<Button>().onClick.AddListener(() => { self.OnAvtive(); });
                self.shareBtn.GetComponent<Button>().onClick.AddListener(() => { self.OnShare(); });

                //leftBtn
                self.iphoneBtn = rc.Get<GameObject>("iphoneBtn");
                self.invitationCodeBtn = rc.Get<GameObject>("invitationCodeBtn");
                self.realNameBtn = rc.Get<GameObject>("realNameBtn");
                self.iphoneBtn.GetComponent<Button>().onClick.AddListener(() => { self.OnIphone(); });
                self.invitationCodeBtn.GetComponent<Button>().onClick.AddListener(() => { self.OnInvitationCode(); });
                self.realNameBtn.GetComponent<Button>().onClick.AddListener(() => { self.OnRealName(); });

                //up
                self.hourseCardAddBtn = rc.Get<GameObject>("hourseCradAddBtn");
                self.curServiceBtn = rc.Get<GameObject>("cusServiceBtn");
                self.messageBtn = rc.Get<GameObject>("messageBtn");
                self.hourseCardTxt = rc.Get<GameObject>("hourseCradTxt");
                self.hourseCardAddBtn.GetComponent<Button>().onClick.AddListener(() => { self.OnHourseCardAdd(); });
                self.curServiceBtn.GetComponent<Button>().onClick.AddListener(() => { self.OnCurService(); });
                self.messageBtn.GetComponent<Button>().onClick.AddListener(() => { self.OnMessage(); });
                
                //提示面板
                self.titlePanelObj = rc.Get<GameObject>("titlePanle");
                //每次进来都弹一次
                self.titlePanelObj.SetActive(true);
                self.closeTitleBtn = rc.Get<GameObject>("closeTitleBtn");
                self.closeTitleBtn.GetComponent<Button>().onClick.AddListener(() => { self.OnCloseTitle(); });

                self.createRoomType = 0;
                self.roomId = "";
                if (param != null && param.Length > 0)
                {
                    Dictionary<string, object> dic = param[0] as Dictionary<string, object>;
                    self.createRoomType = (int)dic["result"];
                    self.roomId = dic["roomId"] as string;
                }
                //self.createRoomTxt.GetComponent<Text>().text = self.createRoomType == 0 ? "创建房间" : "返回房间";

                await SoundManager.Instance.PlaySound("common_playBg",true);
            }
        }
        #region 中间按钮
        public static async void OnCreateRoom(this UIMainComponent self)
        {
            await SoundManager.Instance.PlaySound("clickbtnsound");
            if (self.createRoomType == 0)
            {
                self.CreateRoom().Coroutine();
            }
            else
            {
                self.ReturnRoom().Coroutine();
            }
        }

        public static async void OnJoinRoom(this UIMainComponent self)
        {
            await SoundManager.Instance.PlaySound("clickbtnsound");
            await UIHelper.Create(self.ClientScene(), UIType.UIJoinRoom, UILayer.High);
        }

        public static async void OnClub(this UIMainComponent self)
        {
            await SoundManager.Instance.PlaySound("clickbtnsound");
            var scene = self.ClientScene();
            await UIHelper.Remove(scene, UIType.UIMain);
            await UIHelper.Create(scene, UIType.UIClubCreate, UILayer.Mid);
        }
        #endregion

        #region 头像点击
        public static void OnPlayer(this UIMainComponent self)
        {

        }
        #endregion

        #region 下面按钮
        public static async void OnShop(this UIMainComponent self)
        {
            await SoundManager.Instance.PlaySound("clickbtnsound");
            await UIHelper.Create(self.ClientScene(), UIType.UIShop, UILayer.Mid);
        }

        public static async void OnSetting(this UIMainComponent self)
        {
            await SoundManager.Instance.PlaySound("clickbtnsound");
            await UIHelper.Create(self.ClientScene(), UIType.UISetting, UILayer.Mid);
        }

        public static async void OnRule(this UIMainComponent self)
        {
            await SoundManager.Instance.PlaySound("clickbtnsound");
            await UIHelper.Create(self.ClientScene(), UIType.UIRule, UILayer.Mid);
        }

        public static async void OnStats(this UIMainComponent self)
        {
            await SoundManager.Instance.PlaySound("clickbtnsound");
            await UIHelper.Create(self.ClientScene(), UIType.UIStats, UILayer.Mid);
        }

        public static async void OnShare(this UIMainComponent self)
        {
            await SoundManager.Instance.PlaySound("clickbtnsound");
            await UIHelper.Create(self.ClientScene(), UIType.UIShare, UILayer.Mid);
        }

        public static async void OnAvtive(this UIMainComponent self)
        {
            await SoundManager.Instance.PlaySound("clickbtnsound");
            await UIHelper.Create(self.ClientScene(), UIType.UIAvtive, UILayer.Mid);
        }
        #endregion

        #region 左边按钮
        public static async void OnIphone(this UIMainComponent self)
        {
            await SoundManager.Instance.PlaySound("clickbtnsound");
            await UIHelper.Create(self.ClientScene(), UIType.UIIphone, UILayer.Mid);
        }

        public static async void OnInvitationCode(this UIMainComponent self)
        {
            await SoundManager.Instance.PlaySound("clickbtnsound");
            await UIHelper.Create(self.ClientScene(), UIType.UIInvate, UILayer.Mid);
        }

        public static async void OnRealName(this UIMainComponent self)
        {
            await SoundManager.Instance.PlaySound("clickbtnsound");
            await UIHelper.Create(self.ClientScene(), UIType.UIRealNameAuthen, UILayer.Mid);
        }
        #endregion

        #region 上面按钮
        public static async void OnHourseCardAdd(this UIMainComponent self)
        {
            await SoundManager.Instance.PlaySound("clickbtnsound");
        }

        public static async void OnCurService(this UIMainComponent self)
        {
            await SoundManager.Instance.PlaySound("clickbtnsound");
            await UIHelper.Create(self.ClientScene(), UIType.UICustomerService, UILayer.Mid);
        }

        public static async void OnCloseTitle(this UIMainComponent self)
        {
            await SoundManager.Instance.PlaySound("clickbtnsound");
            self.titlePanelObj.SetActive(false);
        }

        public static async void OnMessage(this UIMainComponent self)
        {
            await SoundManager.Instance.PlaySound("clickbtnsound");
            await UIHelper.Create(self.ClientScene(), UIType.UIMessage, UILayer.Mid);
        }
        #endregion

        static async ETTask CreateRoom(this UIMainComponent self)
        {
            try
            {
                await RequestServerUtil.Instance.OpenRequestServer(OuterMessage.C2G_CreatRoom,null, async (messageObj,errorCode) =>
                {
                    if (errorCode != ErrorCode.ERR_Success)
                    {
                        await CommonUtil.Instance.OpenCommonServerMsgPanel($"创建失败,errCode={errorCode}");
                        return;
                    }
                    RoomInfo roomInfo = messageObj as RoomInfo;
                    var startFightRoomComponment = GameUtil.Instance.GetSceneComponent().GetComponent<StartFightRoomComponment>();
                    if (startFightRoomComponment == null)
                    {
                        startFightRoomComponment = GameUtil.Instance.GetSceneComponent().AddComponent<StartFightRoomComponment>();
                    }
                    startFightRoomComponment.ClearStartFightRoomInfo();
                    startFightRoomComponment.SetStartFightRoomInfoFromCreate(roomInfo);
                    //目前就用一个场景吧,后面看再单独分开。
                    //关闭主界面
                    await UIHelper.Remove(GameUtil.Instance.GetSceneComponent(), UIType.UIMain);
                    //打开开始打牌界面
                    await UIHelper.Create(GameUtil.Instance.GetSceneComponent(), UIType.UIStartFightRoom, UILayer.Mid);
                });
            }
            catch (Exception e)
            {
                Log.Error($"创建房间出错...{e.Message}");
            }
        }

        static async ETTask ReturnRoom(this UIMainComponent self)
        {
            try
            {
                if (string.IsNullOrEmpty(self.roomId))
                {
                    return;
                }
                Dictionary<string, object> dic = new Dictionary<string, object>()
                {
                    { "roomId",self.roomId}
                };
                await RequestServerUtil.Instance.OpenRequestServer(OuterMessage.C2G_JoinRoom, dic,async (messageObj, errorCode) =>
                {
                    if (errorCode != ErrorCode.ERR_Success)
                    {
                        await CommonUtil.Instance.OpenCommonServerMsgPanel($"返回房间失败,errCode={errorCode}");
                        return;
                    }
                    //关闭主界面
                    await UIHelper.Remove(GameUtil.Instance.GetSceneComponent(), UIType.UIMain);
                    //打开开始打牌界面
                    await UIHelper.Create(GameUtil.Instance.GetSceneComponent(), UIType.UIStartFightRoom, UILayer.Mid);
                });
            }
            catch (Exception e)
            {
                Log.Error($"返回房间出错...{e.Message}");
            }
        }
    }
}