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

public class PageSpot : MonoBehaviour
{
    [SerializeField] GameObject mTplSpot;

    private Transform mCtn;
    private List<GameObject> mLightSpots;
    private List<GameObject> mSpotList;

    private int mSpotCount;

    private void Awake()
    {
        mCtn = transform;
    }

    public void Init(int pTotalCount)
    {
        mSpotCount = pTotalCount;
        mLightSpots = new List<GameObject>();

        for (int i = 0; i < pTotalCount; i++)
        {
            GameObject tSpot = GetSpot(i);
            mLightSpots.Add(tSpot.transform.GetChild(0).gameObject);
        }

        HideUnused();
    }

    public void SetPageIndex(int pCurrentIndex)
    {
        for(int i = 0; i < mLightSpots.Count; i++)
        {
            mLightSpots[i].SetActive(pCurrentIndex == i);
        }
    }

    private GameObject GetSpot(int pSpotIndex)
    {
        if (mSpotList == null)
        {
            mSpotList = new List<GameObject>();
        }

        GameObject tCell = null;
        if (pSpotIndex < mSpotList.Count)
        {
            tCell = mSpotList[pSpotIndex];
        }
        else
        {
            tCell = Instantiate(mTplSpot, mCtn);
            tCell.transform.localScale = Vector3.one;
            mSpotList.Add(tCell);
        }

        tCell.SetActive(true);

        return tCell;
    }

    private void HideUnused()
    {
        for (int i = mSpotCount; i < mSpotList.Count; i++)
        {
            mSpotList[i].SetActive(false);
        }
    }
}