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

public abstract class ListView<T> : MonoBehaviour, IListView where T : ListCell
{
    public abstract int Count
    {
        get;
    }

    [SerializeField] GameObject mTplCell;
    [SerializeField] Transform mCtnCells;

    protected List<T> mCellList;

    public virtual void Show(bool pShow)
    {
        gameObject.SetActive(pShow);
    }

    public virtual void LoadData()
    {
        if (mCellList == null)
        {
            mCellList = new List<T>();
        }

        for (int i = 0; i < Count; i++)
        {
            T tCell = null;
            if (i < mCellList.Count)
            {
                tCell = mCellList[i];
            }
            else
            {
                tCell = SpawnCell();
                mCellList.Add(tCell);
            }

            tCell.gameObject.SetActive(true);
            tCell.SetIndex(i);
            ConfigCell(tCell);
        }

        HideUnused();
    }

    public virtual void ClearSelection()
    {

    }

    protected abstract void ConfigCell(T pCell);

    private T SpawnCell()
    {
        GameObject tCellOb = Instantiate(mTplCell, mCtnCells);

        tCellOb.transform.localScale = Vector3.one;

        return tCellOb.GetComponent<T>();
    }

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

public interface IListView
{
    void Show(bool pShow);
    void LoadData();
    void ClearSelection();
}