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

public class SaveManager : MonoBehaviour {

    private const string SAVE_DATA = "SAVE_DATA";

    private static SaveManager _instance;
    public static SaveManager Instance
    {
        get
        {
            if (_instance == null)
            {
                GameObject go = new GameObject("SaveManager");
                _instance = go.AddComponent<SaveManager>();
                _instance.Init();
            }
            return _instance;
        }
    }

    public PlayerData _data;
    public static PlayerData data
    {
        get
        {
            return Instance._data;
        }
        set
        {
            Instance._data = value;
        }
    }

	void Init()
    {
        DontDestroyOnLoad(gameObject);

        Load();
    }

    void Check()
    {

    }

    public static void Save()
	{
        Instance.Check();

        string jsonData = JsonUtility.ToJson(Instance._data);
        StreamWriter file = File.CreateText(Application.persistentDataPath + "/saveData.json");
        file.Write(jsonData);
        file.Close();

        /*BinaryFormatter bf = new BinaryFormatter ();
		FileStream file = File.Create (Application.persistentDataPath + "/playerInfo.dat");

		bf.Serialize (file, Instance._data);
		file.Close ();*/
	}

	public void Load()
	{
        if (File.Exists(Application.persistentDataPath + "/saveData.json"))
        {
            string jsonData = File.ReadAllText(Application.persistentDataPath + "/saveData.json");
            _data = JsonUtility.FromJson(jsonData, typeof(PlayerData)) as PlayerData;
        }
        else
        {
            LoadDefaultData();
        }

        /*if (File.Exists(Application.persistentDataPath + "/playerInfo.dat"))
        {
            BinaryFormatter bf = new BinaryFormatter();
            FileStream file = File.Open(Application.persistentDataPath + "/playerInfo.dat", FileMode.Open);
            _data = (PlayerData)bf.Deserialize(file);
            file.Close();
        } else
        {
            LoadDefaultData();
        }*/
    }

    public static void Delete()
    {
        if (File.Exists(Application.persistentDataPath + "/saveData.json"))
        {
            File.Delete(Application.persistentDataPath + "/saveData.json");
            Debug.Log("Save data deleted");
        }
        else
        {
            Debug.Log("Save data does not exist");
        }
            
    }

    protected static bool HasGUID(string[] array, string guid)
    {
        Instance.Init();
        if (array == null || array.Length == 0 || string.IsNullOrEmpty(guid))
            return false;

        int length = array.Length;
        for (int i = 0; i < length; i++)
        {
            if (array[i] == guid)
                return true;
        }

        return false;
    }

    protected static bool SaveGUID(ref string[] array, string guid)
    {
        Instance.Init();
        if (string.IsNullOrEmpty(guid))
            return false;

        if (array == null)
        {
            array = new string[] { guid };
            return true;
        }
        else
        {
            int length = array.Length;
            for (int i = 0; i < length; i++)
            {
                if (array[i] == guid)
                {
                    return false;
                }
            }

            System.Array.Resize<string>(ref array, length + 1);
            array[length] = guid;
        }

        return true;
    }

    /*public static bool SaveAchievement(Achievement achievement)
    {
        return SaveGUID(ref data.achievements, achievement.ID);
    }

    public static bool SaveAchievement(string GUID)
    {
        return SaveGUID(ref data.achievements, GUID);
    }

    public static bool HasAchievement(Achievement achievement)
    {
        return HasAchievement(achievement.ID);
    }

    public static bool HasAchievement(string guid)
    {
        return HasGUID(data.achievements, guid);
    }

    public static string[] GetAchievements()
    {
        return data.achievements;
    }*/

    public void LoadDefaultData()
    {
        _data = new PlayerData();

    }
}


[Serializable]
[PreferBinarySerialization]
public class PlayerData
{
    public bool actionPanelLocked;


}