using System;
using System.Collections.Generic;

namespace Logic {
    public sealed class ConditionList : List<Condition> {
        private const string Delimeter = ";";
        
        internal string SerializeCompact() {
            if (Count > 0) {
                List<string> conditionStrings = new List<string>();
                foreach (var condition in this) {
                    if (condition != null) {
                        var compact = condition.ToCompactString();
                        if (!string.IsNullOrEmpty(compact)) {
                            conditionStrings.Add(compact);
                        }
                    }
                }
                return string.Join(Delimeter, conditionStrings.ToArray());
            }
            return String.Empty;
        }

        internal static ConditionList DeserializeCompact(string jsonString) {
            ConditionList conditions = new ConditionList();
            var conditionStrings = jsonString.Split(new string[] { Delimeter }, new StringSplitOptions());
            foreach (var conditionString in conditionStrings) {
                var condition = Condition.ParseFromCompactString(conditionString);
                if (condition != null) {
                    conditions.Add(condition);
                }
            }
            if (conditions.Count == 0) {
                return null;
            }
            return conditions;
        }
    }
}
