using System.Collections.Generic;
using map;
using UnityEngine;
using Logic;
using Newtonsoft.Json;
using System.IO;
using System;

public class PlayerData {
    protected static PlayerData instance = null;

    public static PlayerData Instance {
        get {
            return instance;
        }
    }

    [JsonProperty("pos")]
    public Coord Position { get; set; }
    [JsonProperty("quest")]
    public string QuestId { get; private set; }
    [JsonProperty("items")]
    public Container Container { get; private set; }
    [JsonProperty("start_quest")]
    public bool StartQuestShowed { get; set; }
    [JsonProperty("lose_quest")]
    public string LostQuestId { get; set; }

    public const string CREW = "crew";
    public const string FUEL = "fuel";
    private Dictionary<string, string> textParams = null;
    static string savePath = Application.persistentDataPath + "/save.json";

    public PlayerData() {
        Position = new Coord();
        Container = new Container();
        QuestId = string.Empty;
        StartQuestShowed = false;
    }

    public PlayerData(StarMap startConfigs) {
        Position = startConfigs.startPosition;
        LostQuestId = startConfigs.loseQuestId;
        Container = new Container();
        Container.SetItems(startConfigs.startParams);
        QuestId = string.Empty;
        StartQuestShowed = false;
    }

    public static void LoadPlayer(StarMap startConfigs) {
        instance = Load();
        if (instance == null) {
            instance = new PlayerData(startConfigs);
        }
    }

    public static void CreateNewPlayer(StarMap startConfigs) {
        instance = new PlayerData(startConfigs);
    }

    public static PlayerData Load() {
        if (File.Exists(savePath)) {
            return JsonConvert.DeserializeObject<PlayerData>(File.ReadAllText(savePath));
        }
        return null;
    }

    public void Save() {
        JsonSerializerSettings settings = new JsonSerializerSettings() {
            Formatting = Formatting.Indented,
            NullValueHandling = NullValueHandling.Ignore
        };
        File.WriteAllText(savePath, JsonConvert.SerializeObject(this, settings));
    }

    internal void StartQuest(string questId) {
        QuestId = questId;
        SetDialogConsts(Container);
        State.Show<DialogState>().InitQuest(QuestId, Container, textParams);
    }

    internal void FlyToQuest(MapPoint point) {
        Instance.Position = point.position;
        Instance.QuestId = point.questId;
        Container[FUEL] -= 1;
        if (Container[FUEL] >= 0) {
            State.Show<HyperJumpState>();
        } else {
            StartQuest(LostQuestId);
        }
    }

    internal static void ShowQuest() {
        Instance.StartQuest(Instance.QuestId);
    }

    private void SetDialogConsts(Container container) {
        container["coord_x"] = Position.x;
        container["coord_y"] = Position.y;
        container["id"] = Position.x + Position.y * 10000;
    }

    internal static void EndQuest() {
        Instance.QuestId = string.Empty;
        State.Show<PixelMapState>();
    }

    internal void SetTexts(Dictionary<string, string> textVariables) {
        textParams = textVariables;
    }
}
