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

namespace Rokid.MRC
{
    public enum ConfirmType
    {
        None,
        OneConfirm,
        TwoConfirmCancel,
        OneCancel,
    }

    //弹窗界面
    public class UIConfirmPanel : UIPanelBase
    {
        private Text txtTitle;

        private RectTransform rectConfirm;
        private Text txtButtonConfrim;

        private RectTransform rectCancel;
        private Text txtButtonCancel;

        private Text txtContent;

        private Action confirmCallBack;
        private Action cancelCallBack;

        private float XOffest = 170;

        private ConfirmType curConfirmType;

        public override void OnInit()
        {
            base.OnInit();

            txtTitle = transform.Find("DisplayView/TxtTittle").GetComponent<Text>();

            rectConfirm = transform.Find("DisplayView/BtnConfirm").GetComponent<RectTransform>();
            txtButtonConfrim = transform.Find("DisplayView/BtnConfirm/Text").GetComponent<Text>();
            rectConfirm.GetComponent<Button>().onClick.AddListener(() =>
            {
                if(confirmCallBack != null)
                {
                    confirmCallBack();
                }

                gameObject.SetActive(false);
            });

            rectCancel = transform.Find("DisplayView/BtnCancel").GetComponent<RectTransform>();
            txtButtonCancel = transform.Find("DisplayView/BtnCancel/Text").GetComponent<Text>();
            rectCancel.GetComponent<Button>().onClick.AddListener(() =>
            {
                if(cancelCallBack != null)
                {
                    cancelCallBack();
                }

                gameObject.SetActive(false);
            });

            txtContent = transform.Find("DisplayView/TxtInfo").GetComponent<Text>();

            //语言表
            txtButtonConfrim.text = LocalizationMgr.Instance.GetTextByKey("ButtonConfirm");
            txtButtonCancel.text = LocalizationMgr.Instance.GetTextByKey("ButtonCancel");
        }

        public override void OnOpen()
        {
            base.OnOpen();

            //TODO，暂时这样处理
            MessageCenter.AddMsgListener(GlobalDefine.SpaceBuildEnd, HandSpaceBuild);
        }

        public override void OnClose()
        {
            base.OnClose();

            MessageCenter.RemoveMsgListener(GlobalDefine.SpaceBuildEnd, HandSpaceBuild);

            StopAllCoroutines();
        }

        private void HandSpaceBuild(object val)
        {
            if(curConfirmType == ConfirmType.None)
            {
                gameObject.SetActive(false);
            }
        }

        public void SetContent(string title, string content, float closeTime, ConfirmType confirmType = ConfirmType.TwoConfirmCancel, Action confirmCall = null, Action cancelCall = null)
        {
            txtTitle.text = title;
            txtContent.text = content;

            curConfirmType = confirmType;

            switch(confirmType)
            {
                case ConfirmType.None:
                    rectCancel.gameObject.SetActive(false);
                    rectConfirm.gameObject.SetActive(false);
                    break;
                case ConfirmType.OneConfirm:
                    rectCancel.gameObject.SetActive(false);
                    rectConfirm.gameObject.SetActive(true);
                    rectConfirm.anchoredPosition = new Vector2(0, rectConfirm.anchoredPosition.y);
                    break;
                case ConfirmType.TwoConfirmCancel:
                    rectCancel.gameObject.SetActive(true);
                    rectConfirm.gameObject.SetActive(true);
                    rectConfirm.anchoredPosition = new Vector2(XOffest, rectConfirm.anchoredPosition.y);
                    rectCancel.anchoredPosition = new Vector2(-XOffest, rectConfirm.anchoredPosition.y);
                    break;
                case ConfirmType.OneCancel:
                    rectCancel.gameObject.SetActive(true);
                    rectConfirm.gameObject.SetActive(false);
                    rectCancel.anchoredPosition = new Vector2(0, rectConfirm.anchoredPosition.y);
                    break;
                default:
                    break;
            }

            confirmCallBack = confirmCall;
            cancelCallBack = cancelCall;

            if(closeTime != 0)
            {
                StartCoroutine(WaitAndClose(closeTime));
            }
        }

        private IEnumerator WaitAndClose(float time)
        {
            yield return new WaitForSeconds(time);

            UIManager.Instance.ClosePanel(UIType.Confirm);
        }

    }
}