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

public class EndingCell : MonoBehaviour
{
    [SerializeField] Text mTxtEndingName;
    [SerializeField] GameObject mGobCheck;
    [SerializeField] GameObject mGobGood;
    [SerializeField] Button mBtnTip;
    [SerializeField] Button mBtnAdTip;
    [SerializeField] GameObject mGobPlayingTip;

    private int mLevelID;
    private int mEndingID;
    private EndingData mEdData;

    private List<AudioClip> mTipSoundList;
    private TimerUnit mTipPlayTimer;
    private int mTipIndex;

    private void Awake()
    {
        UIUtils.BindBtn(mBtnTip, OnClickTip);
        UIUtils.BindBtn(mBtnAdTip, OnClickAdTip);
    }

    private void OnDisable()
    {
        if (mTipPlayTimer != null)
        {
            mTipPlayTimer.Destroy();
            mTipPlayTimer = null;
        }
    }

    public void Init(int pLevelID, int pEndingID)
    {
        mLevelID = pLevelID;
        mEndingID = pEndingID;

        mEdData = GameConfig.Instance.GetLevelData(mLevelID).Endings[pEndingID];

        mTxtEndingName.text = mEdData.EndingName;
        mGobCheck.SetActive(PlayerData.Instance.HasUnlockedEnding(mLevelID, mEndingID));
        mGobGood.SetActive(mEdData.IsGoodEnding);

        bool tHasWatchedAdTip = PlayerData.Instance.HasWatchedEndingAdTip(mLevelID, mEndingID);
        mBtnTip.gameObject.SetActive(tHasWatchedAdTip);
        mBtnAdTip.gameObject.SetActive(!tHasWatchedAdTip);

        mGobPlayingTip.SetActive(false);
    }

    private void OnClickTip()
    {
        string[] tTemplateEndingStrs = mEdData.EndingCode.Split(';');

        mTipSoundList = new List<AudioClip>();
        for (int i = 0; i < tTemplateEndingStrs[0].Length; i++)
        {
            int tSoundIndex = tTemplateEndingStrs[0][i] == 'X' ? 0 : (int.Parse(tTemplateEndingStrs[0][i].ToString()) - 1);

            mTipSoundList.Add(ResourceManager.Instance.LoadRes<AudioClip>(Const.Path.GetSound(mLevelID, tSoundIndex)));
        }

        if (mTipPlayTimer == null)
        {
            mTipPlayTimer = TimerManager.Instance.CreateTimerUnit();
        }

        mTipIndex = 0;
        mTipPlayTimer.CancelTimer();
        mTipPlayTimer.StartTimer(PlayOneTip, 0.3f);
        mGobPlayingTip.SetActive(true);
    }

    private void PlayOneTip()
    {
        AudioManager.Instance.PlaySound(mTipSoundList[mTipIndex]);
        mTipIndex++;

        if (mTipIndex < mTipSoundList.Count)
        {
            mTipPlayTimer.StartTimer(PlayOneTip, 1f);
        }
        else
        {
            mGobPlayingTip.SetActive(false);
        }
    }

    private void OnClickAdTip()
    {
        ToukaSDKManager.Instance.PlayRewardAd(Const.AdsEvtValue.UnlockSound, WatchAdSucceed, () =>
        {
            UIManager.Instance.OpenUI<PanelNoAdsTip>();
        });
    }

    private void WatchAdSucceed()
    {
        PlayerData.Instance.AddEndingAdTip(mLevelID, mEndingID);
        mBtnTip.gameObject.SetActive(true);
        mBtnAdTip.gameObject.SetActive(false);
        OnClickTip();

        ToukaSDKManager.Instance.LogEventByUmeng(Const.AdsEvent.UnlockTip, Const.AdsEvtID.Level, PlayerData.Instance.CurrentLevel.ToString());
    }
}