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

namespace Logic {
    public sealed class Node {

        [JsonProperty("text")]
        public string text;

        [JsonProperty("answers")]
        public Answer[] answers;

        [JsonProperty("actions")]
        [JsonConverter(typeof(ActionListConverter))]
        public ActionList actions;

        [JsonProperty("auto")]
        public bool isAutoSwitch = false;

        public bool IsAutoSwitch { get { return isAutoSwitch;}}

        public Node(string nodeText = null, Answer[] nodeAnswers = null, ActionList nodeActions = null) {
            text = nodeText;
            answers = nodeAnswers;
            actions = nodeActions;
        }

        internal void ProcessActions(Container container) {
            if (actions != null) {
                foreach(var action in actions) {
                    if (action != null) {
                        action.Process(container);
                    }
                }
            }
        }

        internal List<Answer> GetAvailableAnswers(Container container) {
            var availableAnswers = new List<Answer>();
            foreach (var answer in answers) {
                if (answer.IsConditionsValid(container)) {
                    availableAnswers.Add(answer);
                }
            }
            return availableAnswers;
        }
    }
}
