using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.UI;
using static System.Net.Mime.MediaTypeNames;

namespace ET.Client
{
	[FriendOf(typeof(UIJoinRoomComponent))]
	public static class UIJoinRoomComponentSystem
    {
		[ObjectSystem]
		public class UIJoinRoomComponentAwakeSystem : AwakeSystem<UIJoinRoomComponent>
		{
			protected override void Awake(UIJoinRoomComponent self, params object[] param)
			{
                ReferenceCollector rc = self.GetParent<UI>().GameObject.GetComponent<ReferenceCollector>();
                self.inputBackBtn = rc.Get<GameObject>("inputBack");
                self.inputClearBtn = rc.Get<GameObject>("inputClear");
                self.closeBtn = rc.Get<GameObject>("closeBtn");
                self.showTxt = rc.Get<GameObject>("showTxt");
                for (int i = 0;i <= 9;i++)
                {
                    var tempInput = rc.Get<GameObject>("input" + i);
                    int index = i;
                    tempInput.GetComponent<Button>().onClick.AddListener(() => { self.OnInputNumber(index); });
                }
                self.inputBackBtn.GetComponent<Button>().onClick.AddListener(() => { self.OnInputBack(); });
                self.inputClearBtn.GetComponent<Button>().onClick.AddListener(() => { self.OnInputClear(); });
                self.closeBtn.GetComponent<Button>().onClick.AddListener(() => { self.OnCloseAsync(); });
                self.showTxt.GetComponent<UnityEngine.UI.Text>().text = "";
            }
        }

        private static void updateShowText(this UIJoinRoomComponent self, string add = "")
        {
            if (!string.IsNullOrEmpty(add) && self.inputText.Length < 6)
            {
                self.inputText = self.inputText + add;
            }
            if (self.inputText.Length == 6)
            {
                //已经6位了。。。
                //加入房间请求。。
                self.JoinRoom().Coroutine();
            }
            self.showTxt.GetComponent<UnityEngine.UI.Text>().text = self.inputText;
        }

        public static async void OnInputNumber(this UIJoinRoomComponent self,int index)
        {
            await SoundManager.Instance.PlaySound("clickbtnsound");
            self.updateShowText(index.ToString());
        }

        public static async void OnInputBack(this UIJoinRoomComponent self)
        {
            await SoundManager.Instance.PlaySound("clickbtnsound");
            if (self.inputText.Length > 0)
            {
                self.inputText = self.inputText.Substring(0, self.inputText.Length - 1);
                self.showTxt.GetComponent<UnityEngine.UI.Text>().text = self.inputText;
            }
        }

        public static async void OnInputClear(this UIJoinRoomComponent self)
        {
            await SoundManager.Instance.PlaySound("clickbtnsound");
            self.inputText = string.Empty;
            self.updateShowText();
        }

        static async ETTask JoinRoom(this UIJoinRoomComponent self)
        {
            if (string.IsNullOrEmpty(self.inputText) || self.inputText.Length < 6)
            {
                await CommonUtil.Instance.OpenCommonServerMsgPanel("输入房间号");
                return;
            }
            try
            {
                Dictionary<string, object> dic = new Dictionary<string, object>()
                {
                    { "roomId",self.inputText}
                };
                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.Remove(GameUtil.Instance.GetSceneComponent(), UIType.UIJoinRoom);
                    //打开开始打牌界面
                    await UIHelper.Create(GameUtil.Instance.GetSceneComponent(), UIType.UIStartFightRoom, UILayer.Low);
                });
            }
            catch (Exception e)
            {
                Log.Error($"创建房间出错...{e.Message}");
            }
        }

        public static async void OnCloseAsync(this UIJoinRoomComponent self)
        {
            await SoundManager.Instance.PlaySound("clickbtnsound");
            await UIHelper.Remove(self.ClientScene(),UIType.UIJoinRoom);
        }
    }
}