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

[Serializable]
public class LevelList {
    public List<LevelData> gameLevels;
    public List<LevelData> userLevels;

    public LevelList()
    {
        gameLevels = new List<LevelData>();
        userLevels = new List<LevelData>();
    }

    public void RemoveLevel(String name)
    {
        gameLevels.RemoveAll(l => l.levelName == name);
        userLevels.RemoveAll(l => l.levelName == name);
    }

    public void AddGameLevel(LevelData level)
    {
        if (level.levelName != null || level.levelName == "")
        {
            gameLevels.RemoveAll(l => l.levelName == level.levelName);
            gameLevels.Add(level);
        }
    }

    public void AddUserLevel(LevelData level)
    {
        if (level.levelName != null || level.levelName == "")
        {
            userLevels.RemoveAll(l => l.levelName == level.levelName);
            userLevels.Add(level);
        }
    }

    public void SaveToFile()
    {
        string levelsJson = JsonUtility.ToJson(this);

        PlayerPrefs.SetString("Levels", levelsJson);
        PlayerPrefs.Save();
    }

    public static LevelList LoadFromFile()
    {
        LevelList list;
        if (PlayerPrefs.HasKey("Levels"))
        {
            list = JsonUtility.FromJson<LevelList>(PlayerPrefs.GetString("Levels"));
        }
        else
        {
            list = new LevelList();
        }

        return list;
    }
}