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(UIClubCreateComponent))]
	public static class UIClubCreateComponentSystem
    {
		[ObjectSystem]
		public class UIClubCreateComponentAwakeSystem : AwakeSystem<UIClubCreateComponent>
		{
			protected override void Awake(UIClubCreateComponent self, params object[] param)
			{
                ReferenceCollector rc = self.GetParent<UI>().GameObject.GetComponent<ReferenceCollector>();
                self.returnBtn = rc.Get<GameObject>("returnBtn");
                self.createBtn = rc.Get<GameObject>("createBtn");
                self.joinBtn = rc.Get<GameObject>("joinBtn");
                self.inputClubObj = rc.Get<GameObject>("inputClubObj");
                self.inputClubCloseBtn = rc.Get<GameObject>("inputClubCloseBtn");
                self.inputClearBtn = rc.Get<GameObject>("inputClear");
                self.inputBackBtn = rc.Get<GameObject>("inputBack");
                self.showTxt = rc.Get<GameObject>("showTxt");
                self.createObj = rc.Get<GameObject>("createObj");
                self.createObjCloseBtn = rc.Get<GameObject>("createObjCloseBtn");
                self.sureBtn = rc.Get<GameObject>("sureBtn");
                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.returnBtn.GetComponent<Button>().onClick.AddListener(() => { self.OnReturnBtn(); });
                self.createBtn.GetComponent<Button>().onClick.AddListener(() => { self.OnCreateOrJoinBtn(0); });
                self.joinBtn.GetComponent<Button>().onClick.AddListener(() => { self.OnCreateOrJoinBtn(1); });
                self.inputClubCloseBtn.GetComponent<Button>().onClick.AddListener(() => { self.OnInputClubCloseBtn(); });
                self.createObjCloseBtn.GetComponent<Button>().onClick.AddListener(() => { self.OnCreateObjCloseOrSureBtn(0); });
                self.sureBtn.GetComponent<Button>().onClick.AddListener(() => { self.OnCreateObjCloseOrSureBtn(1); });
                self.inputBackBtn.GetComponent<Button>().onClick.AddListener(() => { self.OnInputBack(); });
                self.inputClearBtn.GetComponent<Button>().onClick.AddListener(() => { self.OnInputClear(); });

                self.Init();
            }
        }

        private static void Init(this UIClubCreateComponent self)
        {
            self.inputClubObj.SetActive(false);
            self.createObj.SetActive(false);
        }

        public static  async void OnCreateObjCloseOrSureBtn(this UIClubCreateComponent self,int index)
        {
            await SoundManager.Instance.PlaySound("clickbtnsound");
            self.createObj.SetActive(false);
        }

        public static void OnInputClubCloseBtn(this UIClubCreateComponent self)
        {
            self.inputClubObj.SetActive(false);
        }

        public static async void OnCreateOrJoinBtn(this UIClubCreateComponent self,int index)
        {
            await SoundManager.Instance.PlaySound("clickbtnsound");
            if (index == 0)
            {
                self.createObj.SetActive(true);
            }
            else
            {
                self.inputClubObj.SetActive(true);
                self.inputText = string.Empty;
            }
        }

        public static async void OnReturnBtn(this UIClubCreateComponent self)
        {
            await SoundManager.Instance.PlaySound("clickbtnsound");
            self.ReturnMain().Coroutine();
        }

        #region 输入面板信息
        public static async void OnInputNumber(this UIClubCreateComponent self, int index)
        {
            await SoundManager.Instance.PlaySound("clickbtnsound");
            self.updateShowText(index.ToString());
        }

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

        public static async void OnInputBack(this UIClubCreateComponent 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 UIClubCreateComponent self)
        {
            await SoundManager.Instance.PlaySound("clickbtnsound");
            self.inputText = string.Empty;
            self.updateShowText();
        }
        #endregion

        static async ETTask ReturnMain(this UIClubCreateComponent self)
        {
            var scene = self.ClientScene();
            await UIHelper.Remove(scene, UIType.UIClubCreate);
            await UIHelper.Create(scene, UIType.UIMain, UILayer.Hidden);
        }

        public static async void OnCloseAsync(this UIClubCreateComponent self)
        {
            await UIHelper.Remove(self.ClientScene(),UIType.UIClubCreate);
        }
    }
}