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 void Awake()
    {
        UIUtils.BindBtn(mBtnAgain1, OnClickAgain, AudioType.Click_Normal);
        UIUtils.BindBtn(mBtnAgain2, OnClickAgain, AudioType.Click_Normal);
        UIUtils.BindBtn(mBtnNext, OnClickNext, AudioType.Click_Normal);
        UIUtils.BindBtn(mBtnChoose, OnClickChoose, AudioType.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 pHasNew, bool pNextLevelClear)
    {
        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);
        mBtnEndings.ShowNew(pHasNew);
    }

    private void OnClickAgain()
    {
        DelAgain?.Invoke();
    }

    private void OnClickNext()
    {
        GameManager.Instance.NextLevel();
    }

    private void OnClickChoose()
    {
        PanelLevel tUILevel = UIManager.Instance.OpenUI<PanelLevel>();
        tUILevel.Init(true);
    }

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