using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Linq;

public class WorldMapSave
{
    public enum LevelState
    {
        UNLOCKED,
        LOCKED,
        SAVED
    }

    private LevelState[] levelUnlocked = {
        // World1
        LevelState.SAVED, LevelState.LOCKED, LevelState.LOCKED, LevelState.LOCKED, 
        LevelState.LOCKED, LevelState.LOCKED, LevelState.LOCKED, LevelState.LOCKED, LevelState.LOCKED, LevelState.LOCKED, LevelState.LOCKED,
        // World2
        LevelState.UNLOCKED, LevelState.LOCKED, LevelState.LOCKED, LevelState.LOCKED,
        LevelState.LOCKED, LevelState.LOCKED, LevelState.LOCKED, LevelState.LOCKED, LevelState.LOCKED, LevelState.LOCKED, LevelState.LOCKED,
    };

    private string filePath;

    public void WorldInitialition()
    {
        filePath = Application.persistentDataPath + "/data.dat";

        Debug.Log("Filepath world save: " + Application.persistentDataPath + " + \"/data.dat\"");

        if (File.Exists(filePath))
        {
            ReadData();
        }
        else
        {
            WriteData();
        }
    }

    public void WriteData()
    {
        BinaryFormatter formatter = new BinaryFormatter();
        FileStream fileStream = File.Create(filePath);
        formatter.Serialize(fileStream, levelUnlocked);
        fileStream.Close();
    }

    public void ReadData()
    {
        BinaryFormatter formatter = new BinaryFormatter();
        FileStream fileStream = File.Open(filePath, FileMode.Open);
        levelUnlocked = (LevelState[])formatter.Deserialize(fileStream);
        fileStream.Close();
    }

    private int GetSavedLevelIndex()
    {
        for (int i = 0; i < levelUnlocked.Length; i++)
        {
            if (levelUnlocked[i] == LevelState.SAVED)
            {
                return i;
            }
        }

        // If LevelState.SAVED not find
        return 0;
    }

    public void UnlockNextLevel()
    {
        filePath = Application.persistentDataPath + "/data.dat";
        ReadData();
        //int actuelLevel = PlayerPrefs.GetInt(LevelToLoad,NoLevel);
        //Debug.Log(actuelLevel);

        if (levelUnlocked.Length <= 0) 
        {
            Debug.LogError("Why is LevelUnlocked list empty?");
            return;
        }

        int savedLevelIndex = GetSavedLevelIndex();
        if (savedLevelIndex + 1 >= levelUnlocked.Length)
        {
            Debug.Log("All levels already unlocked! savedLevelIndex: " + savedLevelIndex);
            return;
        }

        levelUnlocked[savedLevelIndex + 1] = LevelState.SAVED;
        levelUnlocked[savedLevelIndex] = LevelState.UNLOCKED;
        WriteData();
    }

    public void WorldReinitialition()
    {
        if (File.Exists(filePath))
        {
            File.Delete(filePath);
            levelUnlocked = new LevelState[] { LevelState.SAVED, LevelState.LOCKED, LevelState.LOCKED, LevelState.LOCKED, LevelState.LOCKED, LevelState.LOCKED, LevelState.LOCKED, LevelState.LOCKED, LevelState.LOCKED, LevelState.LOCKED, LevelState.LOCKED };
        }
        WorldInitialition();

    }

    public LevelState GetLevelState(int index)
    {
        return levelUnlocked[index];
    }
}
