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

namespace map {
    [System.Serializable]
    public class StarMap {
        public const string DefaultPath = "Configs/map";

        [JsonProperty("start_params")]
        internal Dictionary<string, int> startParams = new Dictionary<string, int>();
        [JsonProperty("default_planet_params")]
        internal Dictionary<string, int> defaultPlanetParams = new Dictionary<string, int>();
        [JsonProperty("start_point")]
        public Coord startPosition;
        [JsonProperty("ship_quest")]
        public string shipQuestId;
        [JsonProperty("start_quest")]
        public string startQuestId; 
        [JsonProperty("fail_quest")]
        public string loseQuestId; // TODO implement
        [JsonProperty("points")]
        public MapPoint[] points;

        public static StarMap LoadFromResources(string path = DefaultPath) {
            var asset = Resources.Load<TextAsset>(path);
            var dialog = JsonConvert.DeserializeObject<StarMap>(asset.text);
            return dialog;
        }
 
        public void Save(string path) {
            JsonSerializerSettings settings = new JsonSerializerSettings() {
                Formatting = Formatting.Indented,
                NullValueHandling = NullValueHandling.Ignore
            };
            File.WriteAllText(path, JsonConvert.SerializeObject(this, settings));
        }

        private static StarMap GenerateDummy() {
            return new StarMap() {
                startPosition = new Coord(5, 5),
                points = new MapPoint[] {
                    new MapPoint(0, 0, "star_0", "quest"),
                    new MapPoint(10, 0, "star_1", "quest"),
                    new MapPoint(0, 10, "star_2", "quest"),
                    new MapPoint(10, 10, "star_3", "quest"),
                    new MapPoint(5, 5, "star_3", "quest", "Start point"),
                }
            };
        }
    }
}