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

public class PlayerData : StorageBase<PlayerData>
{
    public int CurrentLevel
    {
        get
        {
            return mCurrentLevel;
        }
        set
        {
            mCurrentLevel = Mathf.Clamp(value, 1, GameConfig.Instance.LevelSort.Count);
            Save();
        }
    }
    [SerializeField]private int mCurrentLevel = 1;

    public int UnlockedLevel
    {
        get
        {
            return mUnlockedLevel;
        }
    }
    [SerializeField] private int mUnlockedLevel = 1;

    public int CurrentLevelID
    {
        get
        {
            return GameConfig.Instance.LevelSort[mCurrentLevel - 1];
        }
    }

    public bool HasShowedReview
    {
        get
        {
            return mHasShowedReview;
        }
    }
    [SerializeField] private bool mHasShowedReview = false;

    [SerializeField] private Dictionary<int, List<int>> mEndingDic = new Dictionary<int, List<int>>();
    [SerializeField] private Dictionary<int, List<int>> mEndingAdTipDic = new Dictionary<int, List<int>>();

    public void UnlockNewLevel()
    {
        if (mCurrentLevel == mUnlockedLevel && mUnlockedLevel < GameConfig.Instance.LevelSort.Count)
        {
            mUnlockedLevel++;
            Save();
        }
    }

    public void UnlockAllLevel()//for debug
    {
        mUnlockedLevel = GameConfig.Instance.LevelSort.Count;
        Save();
    }

    public bool HasUnlockedAllLevel()
    {
        return mUnlockedLevel == GameConfig.Instance.LevelSort.Count;
    }

    public bool IsNextLevelPassed()
    {
        if (mCurrentLevel == GameConfig.Instance.LevelSort.Count)
        {
            return true;
        }

        int tNextLvID = GameConfig.Instance.LevelSort[mCurrentLevel];//+1 - 1
        return LevelReachGoodEnding(tNextLvID);
    }

    public bool AddEnding(int pLevelID, int pEndingID)
    {
        if (!mEndingDic.ContainsKey(pLevelID))
        {
            mEndingDic[pLevelID] = new List<int>();
        }

        if (!mEndingDic[pLevelID].Contains(pEndingID))
        {
            mEndingDic[pLevelID].Add(pEndingID);
            Save();
            return true;
        }
        
        return false;
    }

    public int EndingCountOfLevel(int pLevelID)
    {
        if (mEndingDic.ContainsKey(pLevelID))
        {
            return mEndingDic[pLevelID].Count;
        }

        return 0;
    }

    public bool LevelReachGoodEnding(int pLevelID)
    {
        if (mEndingDic.ContainsKey(pLevelID))
        {
            LevelData tLvData = GameConfig.Instance.GetLevelData(pLevelID);
            for (int i = 0; i < tLvData.Endings.Count; i++)
            {
                if (tLvData.Endings[i].IsGoodEnding && mEndingDic[pLevelID].Contains(i))
                {
                    return true;
                }
            }
        }

        return false;
    }

    public bool HasUnlockedEnding(int pLevelID, int pEndingID)
    {
        return mEndingDic.ContainsKey(pLevelID) && mEndingDic[pLevelID].Contains(pEndingID);
    }

    public void AddEndingAdTip(int pLevelID, int pEndingID)
    {
        if (!mEndingAdTipDic.ContainsKey(pLevelID))
        {
            mEndingAdTipDic[pLevelID] = new List<int>();
        }

        if (!mEndingAdTipDic[pLevelID].Contains(pEndingID))
        {
            mEndingAdTipDic[pLevelID].Add(pEndingID);
            Save();
        }
    }

    public int EndingAdTipCountOfLevel(int pLevelID)
    {
        if (mEndingAdTipDic.ContainsKey(pLevelID))
        {
            return mEndingAdTipDic[pLevelID].Count;
        }

        return 0;
    }

    public bool HasWatchedEndingAdTip(int pLevelID, int pEndingID)
    {
        return mEndingAdTipDic.ContainsKey(pLevelID) && mEndingAdTipDic[pLevelID].Contains(pEndingID);
    }

    public void MarkReviewShowed()
    {
        mHasShowedReview = true;
    }
}