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

public class PanelResult : BasePanel
{
    public Action DelAgain;

    [SerializeField] Text mTxtEndingName;

    [SerializeField] GameObject mGobWinTitle;
    [SerializeField] GameObject mGobLoseTitle;

    [SerializeField] GameObject mGobWinFace;
    [SerializeField] GameObject mGobLoseFace;

    [SerializeField] GameObject mGobWinBtns;
    [SerializeField] GameObject mGobOtherBtns;

    [SerializeField] GameObject mGobFx;

    [SerializeField] Button mBtnAgain1;
    [SerializeField] Button mBtnAgain2;
    [SerializeField] Button mBtnNext;
    [SerializeField] Button mBtnChoose;

    [SerializeField] ButtonEndings mBtnEndings;

    private bool mReviewShowed = false;
    private const int REVIEW_LEVEL = 1;

    private void Awake()
    {
        UIUtils.BindBtn(mBtnAgain1, OnClickAgain, AudioClipType.Click_Normal);
        UIUtils.BindBtn(mBtnAgain2, OnClickAgain, AudioClipType.Click_Normal);
        UIUtils.BindBtn(mBtnNext, OnClickNext, AudioClipType.Click_Normal);
        UIUtils.BindBtn(mBtnChoose, OnClickChoose, AudioClipType.Click_Normal);

        mBtnEndings.DelClick = OnClickEndings;
    }

    public override void OnFocus(bool pFocus)
    {
        base.OnFocus(pFocus);

        mGobFx.SetActive(pFocus);
    }

    public void Init(int pLevelID, string pEndingName, bool pIsWin, bool pNextLevelClear, bool pHasNew = false, bool pHasComplete = false)
    {
        mTxtEndingName.text = pEndingName;

        mGobWinTitle.SetActive(pIsWin);
        mGobLoseTitle.SetActive(!pIsWin);

        mGobWinFace.SetActive(pIsWin);
        mGobLoseFace.SetActive(!pIsWin);

        mGobWinBtns.SetActive(!pNextLevelClear && pIsWin);
        mGobOtherBtns.SetActive(pNextLevelClear || !pIsWin);

        LevelData tLvData = GameConfig.Instance.GetLevelData(pLevelID);
        mBtnEndings.Init(pLevelID, PlayerData.Instance.EndingCountOfLevel(pLevelID), tLvData.Endings.Count);

        if (pHasNew)
        {
            if (pHasComplete)
            {
                TimerManager.Instance.Schedule(() => mBtnEndings.ShowComplete(true), 0.7f);
            }
            else
            {
                TimerManager.Instance.Schedule(() => mBtnEndings.ShowNew(true), 0.7f);
            }
        }
        else
        {
            mBtnEndings.ShowTip(!pIsWin && !pHasComplete);
        }

        if (!PlayerData.Instance.HasShowedReview && PlayerData.Instance.CurrentLevel == REVIEW_LEVEL)
        {
            PlayerData.Instance.MarkReviewShowed();
            ToukaUtils.OpenSystemReviewPop();
            mReviewShowed = true;
        }
        else
        {
            mReviewShowed = false;
        }
    }

    private void OnClickAgain()
    {
        ToukaSDKManager.Instance.LogEventByUmeng(Const.AdsEvent.EndRetry, Const.AdsEvtID.Level, PlayerData.Instance.CurrentLevel.ToString());

        DelAgain?.Invoke();

        if (!mReviewShowed)
        {
            ToukaSDKManager.Instance.PlayInterstitialAd(Const.AdsEvtValue.EndRetry);
        }
    }

    private void OnClickNext()
    {
        ToukaSDKManager.Instance.LogEventByUmeng(Const.AdsEvent.EndNext, Const.AdsEvtID.Level, PlayerData.Instance.CurrentLevel.ToString());

        GameManager.Instance.NextLevel();

        if (!mReviewShowed)
        {
            ToukaSDKManager.Instance.PlayInterstitialAd(Const.AdsEvtValue.EndNext);
        }
    }

    private void OnClickChoose()
    {
        ToukaSDKManager.Instance.LogEventByUmeng(Const.AdsEvent.EndSelect, Const.AdsEvtID.Level, PlayerData.Instance.CurrentLevel.ToString());

        PanelLevel tUILevel = UIManager.Instance.OpenUI<PanelLevel>();
        tUILevel.Init(true);

        if (!mReviewShowed)
        {
            ToukaSDKManager.Instance.PlayInterstitialAd(Const.AdsEvtValue.EndSelect);
        }
    }

    private void OnClickEndings()
    {
        PanelEndings tUIEndings = UIManager.Instance.OpenUI<PanelEndings>();
        tUIEndings.Init(PlayerData.Instance.CurrentLevelID);
    }
}