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

public class LevelCell : UIListCell
{
    public Action<int> DelClick;

    [SerializeField] Button mBtnBg;
    [SerializeField] ImageNum mInmLevel;
    [SerializeField] Image mImgIconBg;
    [SerializeField] Image mImgIcon;
    [SerializeField] GameObject mGobDone;
    [SerializeField] GameObject mGobComplete;
    [SerializeField] Image mImgProgress;

    private int mLevelID;

    private void Awake()
    {
        UIUtils.BindBtn(mBtnBg, OnClick);
    }

    public void Init()
    {
        mLevelID = GameConfig.Instance.LevelSort[mIndex];
        LevelData tLvData = GameConfig.Instance.GetLevelData(mLevelID);

        mInmLevel.SetNum(mIndex + 1);
        if (ColorUtility.TryParseHtmlString(tLvData.LevelColorCode, out Color tBgColor))
        {
            mImgIconBg.color = tBgColor;
        }

        mImgIcon.sprite = ResourceManager.Instance.LoadRes<Sprite>(Const.Path.GetLevelIconSmall(mLevelID));

        int tUnlockedEndingCount = PlayerData.Instance.EndingCountOfLevel(mLevelID);
        bool tIsAllEndingsDone = tUnlockedEndingCount == tLvData.Endings.Count;
        mGobDone.SetActive(tIsAllEndingsDone);
        mGobComplete.SetActive(tIsAllEndingsDone);

        mImgProgress.fillAmount = (float)tUnlockedEndingCount / tLvData.Endings.Count;
    }

    private void OnClick()
    {
        DelClick?.Invoke(mIndex);
    }
}