using System;
using Newtonsoft.Json;

namespace Logic {
    public sealed class Answer {
        [JsonProperty("text")]
        public string text;

        [JsonProperty("next")]
        public string nextNodeId;

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

        [JsonProperty("conditions")]
        [JsonConverter(typeof(ConditionListConverter))]
        public ConditionList conditions;

        public Answer(string answerText = null, string nextId = null) {
            text = answerText;
            nextNodeId = nextId;
        }

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

        internal bool IsConditionsValid(Container container) {
            if (conditions != null) {
                foreach (var condition in conditions) {
                    if (condition != null) {
                        if (!condition.IsValid(container)) {
                            return false;
                        }
                    }
                }
            }
            return true;
        }
    }
}
