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

public class LevelPage : PageView
{
    public Action<int> DelClickLevel;

    private void Start()
    {
        OnScrollPage = PlayScrollPage;
        OnReachEdge = PlayReachEdge;
    }

    public override int Count
    {
        get
        {
            int tTotalCount = GameConfig.Instance.LevelSort.Count + 1;//add coming soon
            int tUnlockedCount = PlayerData.Instance.UnlockedLevel;
            int tEnoughCount = (tUnlockedCount / CountPerPage + 1) + (tUnlockedCount % CountPerPage == 0 ? -1 : 0);

            return Mathf.Min(tTotalCount, tEnoughCount * CountPerPage);
        }
    }

    protected override void ConfigCell(GameObject pCellGob, int pIndex)
    {
        LevelCell tCell = pCellGob.GetComponent<LevelCell>();

        if (pIndex < PlayerData.Instance.UnlockedLevel)
        {
            tCell.Init(pIndex);
            tCell.DelClick = OnClickLevel;
        }
        else
        {
            if (pIndex < GameConfig.Instance.LevelSort.Count)
            {
                tCell.SetEmpty();
            }
            else
            {
                tCell.SetComingSoon();
            }
        }
    }

    private void OnClickLevel(int pLevelIndex)
    {
        DelClickLevel?.Invoke(pLevelIndex);
    }

    private void PlayScrollPage()
    {
        AudioManager.Instance.PlaySound(AudioType.TurnPage);
    }

    private void PlayReachEdge()
    {
        AudioManager.Instance.PlaySound(AudioType.ReachEdge);
    }
}