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

namespace Logic {
    public sealed class Dialog {
        private const string DefaultStartNodeId = "start";
        private const string DefaultEndNodeId = "end";

        [JsonProperty("start_id")]
        public string startNodeId = DefaultStartNodeId;
        [JsonProperty("end_id")]
        public string endNodeId = DefaultEndNodeId;

        [JsonProperty("nodes")]
        private Dictionary<string, Node> nodes;

        public Node GetNode(string nodeId) {
            if (nodes.ContainsKey(nodeId)) {
                return nodes[nodeId];
            }
            return null;
        }

        public void PatchShowingCheck(string unicalId) {
            foreach (var node in nodes) {
                for (int i = 0; i < node.Value.answers.Length; i++) {
                    var conditions = node.Value.answers[i].conditions;
                    if (conditions != null) {
                        foreach (var condition in conditions) {
                            if (condition.valueA == "showed") {
                                var unicalAnswerId = string.Format("{0}_{1}_{2}", unicalId, node.Key, i.ToString());
                                condition.valueA = unicalAnswerId;
                                if (node.Value.answers[i].actions == null) {
                                    node.Value.answers[i].actions = new ActionList();
                                }
                                node.Value.answers[i].actions.Add(new Action(Action.Type.set, unicalAnswerId, 1));
                            }
                        }
                    }
                }
            }
        }

        public static Dialog CreateDummy() {
            var dialog = new Dialog() {
                nodes = new Dictionary<string, Node> {
                    {DefaultStartNodeId, new Node("Hi there! I'm rabbit", new Answer[] { new Answer("How are you?", "second"), new Answer("Bye", DefaultEndNodeId) }, new ActionList() { new Action(Action.Type.set, "dummy", "5")}) },
                    {"second", new Node("I m fine", new Answer[] { new Answer("Thats your name?", DefaultStartNodeId), new Answer("Bye", DefaultEndNodeId) }) }
                }
            };
            return dialog;
        }

        public static Dialog LoadFromResources(string path) {
            var asset = Resources.Load<TextAsset>(path);
            if (asset == null) {
                Debug.LogError("No quest named " + path + " in resources");
            }
            var dialog = JsonConvert.DeserializeObject<Dialog>(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));
        }

        public static Dialog Load(string path) {
            if (!File.Exists(path)) {
                Debug.LogError("Path of dialog json not found");
                return null;
            }
            var dialog = JsonConvert.DeserializeObject<Dialog>(File.ReadAllText(path));
            return dialog;
        }
    }
}
