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

[CreateAssetMenu(menuName = "GameConfig")]
public class GameConfig : ConfigBase<GameConfig>
{
    public List<int> LevelSort;
    public List<LevelData> Levels;

    public LevelData GetLevelData(int pLevelID)
    {
        return pLevelID < Levels.Count ? Levels[pLevelID] : null;
    }
}

[Serializable]
public class LevelData
{
    public string LevelName;//only for editor display
    public string LevelColorCode;
    public int SlotCount;
    public List<EndingData> Endings;

    public int GetMatchingEndingID(string pEndingStr)
    {
        int tEndingID = -1;

        //first check accurately
        for (int i = 0; i < Endings.Count; i++)
        {
            if (Endings[i].IsMatching(pEndingStr))
            {
                tEndingID = i;
                break;
            }
        }

        //if no match,check with X
        if (tEndingID == -1)
        {
            for (int i = 0; i < Endings.Count; i++)
            {
                if (Endings[i].IsMatching(pEndingStr, true))
                {
                    tEndingID = i;
                    break;
                }
            }
        }

        return tEndingID;
    }
}

[Serializable]
public class EndingData
{
    public string EndingName;//only for editor display
    public bool IsGoodEnding;
    public string EndingCode;//eg: 12X;132;

    public bool IsMatching(string pEndingStr, bool pWithX = false)
    {
        bool tMatching = false;

        string[] tTemplateEndingStrs = EndingCode.Split(';');
        for (int i = 0; i < tTemplateEndingStrs.Length; i++)
        {
            if ((pWithX && !tTemplateEndingStrs[i].Contains("X")) || (!pWithX && tTemplateEndingStrs[i].Contains("X")))
            {
                continue;
            }

            if (CompareEndingStr(pEndingStr, tTemplateEndingStrs[i]))
            {
                tMatching = true;
                break;
            }
        }

        return tMatching;
    }

    private bool CompareEndingStr(string pRefStr, string pTemplateStr)
    {
        bool tMatching = true;

        for (int i = 0; i < pRefStr.Length; i++)
        {
            if (pTemplateStr[i] != 'X' && pRefStr[i] != pTemplateStr[i])
            {
                tMatching = false;
                break;
            }
        }

        return tMatching;
    }
}