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

public class PanelLevel : BasePanel
{
    [SerializeField] Button mBtnBack;
    [SerializeField] Button mBtnBackToMain;
    [SerializeField] LevelPage mLvPage;

    private bool mOpenInLevel = false;

    private void Awake()
    {
        UIUtils.BindBtn(mBtnBack, Close);
        UIUtils.BindBtn(mBtnBackToMain, OnClickBackToMain, AudioType.Click_Normal);

        mLvPage.DelClickLevel = OnClickLevel;
    }

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

        mLvPage.LoadData();
    }

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

        AudioManager.Instance.PlaySound(AudioType.UIClose);
    }

    public void Init(bool pOpenInLevel)
    {
        mOpenInLevel = pOpenInLevel;
        mBtnBackToMain.gameObject.SetActive(pOpenInLevel);
    }

    private void OnClickLevel(int pLevelIndex)
    {
        if (mOpenInLevel && PlayerData.Instance.CurrentLevel == (pLevelIndex + 1))
        {
            Close();
        }
        else
        {
            PlayerData.Instance.CurrentLevel = pLevelIndex + 1;
            GameManager.Instance.LoadGame();

            AudioManager.Instance.PlaySound(AudioType.StartGame);
        }
    }

    private void OnClickBackToMain()
    {
        GameManager.Instance.LoadMain();
    }
}