using System;
using System.Collections.Generic;

namespace Logic {
    public sealed class ActionList : List<Action> {
        private const string Delimeter = ";";

        internal string SerializeCompact() {
            if (Count > 0) {
                List<string> actionsStrings = new List<string>();
                foreach (var action in this) {
                    if (action != null) {
                        var compact = action.ToCompactString();
                        if (!string.IsNullOrEmpty(compact)) {
                            actionsStrings.Add(compact);
                        }
                    }
                }
                return string.Join(Delimeter, actionsStrings.ToArray());
            }
            return String.Empty;
        }

        internal static ActionList DeserializeCompact(string jsonString) {
            ActionList actions = new ActionList();
            var actionStrings = jsonString.Split(new string[] { Delimeter }, new StringSplitOptions());
            foreach (var actionString in actionStrings) {
                Action action = Action.ParseFromCompactString(actionString);
                if (action != null) {
                    actions.Add(action);
                }
            }
            if (actions.Count == 0) {
                return null;
            }
            return actions;
        }
    }
}