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

// 皇陵探宝UI
public class TombRaiderTipPanelCtr : MonoBehaviour {

    private static TombRaiderTipPanelCtr instance;
    public static TombRaiderTipPanelCtr Instance
    {
        get { return instance; }
    }

    private static string settingKey = "TombRaiderTipShow";         // 玩家设置:是否显示提示,-1不显示 1显示。
    private static bool hasShow = true;                             // 是否已经显示过,确保每次活动之弹出一次。
    public static bool HasShow
    {
        get { return hasShow; }
        set { hasShow = value; }
    }

    public int countDownTime;
    public UIBackRayBehind bgMask;
    public Button closeBtn;
    public RectTransform content;
    public Text tip;
    public Button confirmBtn;
    public Text btnDesc;
    public Toggle neverShowTipToggle;

    public static void ShowTip()
    {
        if(!PlayerPrefs.HasKey(settingKey + GameManager.gameManager.PlayerDataPool.MainPlayerBaseAttr.Guid))
        {
            PlayerPrefs.SetInt(settingKey + GameManager.gameManager.PlayerDataPool.MainPlayerBaseAttr.Guid, 1);
        }

        if (PlayerPrefs.GetInt(settingKey + GameManager.gameManager.PlayerDataPool.MainPlayerBaseAttr.Guid) == 1 && HasShow == false)
        {
            UIManager.ShowUI(UIInfo.TombRaiderTipCtr);
            HasShow = true;
        }
    }

    private void Awake()
    {
        closeBtn.onClick.AddListener(OnCloseBtnClick);
        confirmBtn.onClick.AddListener(OnConfirmBtnClick);
        neverShowTipToggle.onValueChanged.AddListener(OnToggerValueChange);
    }

    private void OnEnable()
    {
        StopAllCoroutines();

        Tab_HelpMessage tab = TableManager.GetHelpMessageByID(46, 0);
        tip.text = "";
        int tipCount = tab.getMessageCount();
        string temp;
        for(int i = 0; i < tipCount; ++i)
        {
            temp = tab.GetMessagebyIndex(i).Trim('"');
            if (string.IsNullOrEmpty(temp) || temp.Length < 2)
            {
                continue;
            }

            tip.text += temp + "\n";
        }

        StartCoroutine(CountDown(countDownTime));
    }

    //6751	皇陵探宝活动确认按钮 确认
    //6752	皇陵探宝活动按钮时间倒计时格式"({0}s)"
    private IEnumerator CountDown(float remainTime)
    {
        string defaultStr = StrDictionary.GetClientDictionaryString("#{6751}");
        btnDesc.text = defaultStr;
        if (remainTime > 0)
        {
            // 先减一次,可以让最后显示0的时候有1秒时间
            --remainTime;
            float j = 0.0f;
            for (; remainTime >= 0; --remainTime)
            {
                btnDesc.text = defaultStr + StrDictionary.GetClientDictionaryString("#{6752}", remainTime);
                for (; j < 1.0f; j += Time.deltaTime)
                {
                    yield return 0;
                }

                j -= 1.0f;
            }
        }

        OnConfirmBtnClick();
    }

    private void OnConfirmBtnClick()
    {
        UIManager.CloseUI(UIInfo.TombRaiderTipCtr);
    }

    private void OnCloseBtnClick()
    {
        UIManager.CloseUI(UIInfo.TombRaiderTipCtr);
    }

    private void OnToggerValueChange(bool setting)
    {
        int showSetting = setting ? -1 : 1;
        PlayerPrefs.SetInt(settingKey + GameManager.gameManager.PlayerDataPool.MainPlayerBaseAttr.Guid, showSetting);
    }
}