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

public class PanelLevel : BasePanel
{
    [SerializeField] Button mBtnBack;
    [SerializeField] Button mBtnBackToMain;
    [SerializeField] LevelList mLvList;

    private bool mOpenInLevel = false;

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

        mLvList.DelClickLevel = OnClickLevel;
    }

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

        mLvList.LoadData();
    }

    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();
        }
    }

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