//
// GraphSetConfiguration.cs
//
// Author:
//       Carsten Sonne Larsen <cs@innolan.dk>
//
// Copyright (c) 2013-2016 Carsten Sonne Larsen
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

using System;
using System.Collections.Generic;
using System.Linq;
using Ntp.Analyzer.Config.Graph;
using Ntp.Analyzer.Localize;
using Ntp.Config;

namespace Ntp.Analyzer.Config.Destination
{
    public sealed class GraphSetConfiguration : ConfigurationBase
    {
        public GraphSetConfiguration(
            string configName,
            int indentLevel,
            string title,
            int linkIndex,
            IEnumerable<GraphBaseConfiguration> graphs)
            : base(configName, indentLevel)
        {
            this.title = title;
            this.linkIndex = linkIndex;
            this.graphs = new List<GraphBaseConfiguration>(graphs);
        }

        public const string Identifier = ConfigurationKeyword.GraphSet;

        private const int DefaultLinkIndex = 1;

        private static readonly String[] Params =
        {
            ConfigurationKeyword.GraphSet,
            ConfigurationKeyword.GraphSetTitle,
            ConfigurationKeyword.LinkIndex
        };

        private readonly List<GraphBaseConfiguration> graphs;
        private readonly int linkIndex;
        private readonly string title;

        public string Title
        {
            get { return title; }
        }

        public int LinkIndex
        {
            get { return linkIndex; }
        }

        public IEnumerable<GraphBaseConfiguration> Graphs
        {
            get { return graphs; }
        }

        protected override IEnumerable<Object> Items
        {
            get
            {
                return new object[]
                {
                    ConfigName,
                    Title,
                    LinkIndex
                };
            }
        }

        public override string ToString()
        {
            var values = new List<Object> {ConfigName, Title};

            if (ShowDefault || (!ShowDefault && LinkIndex != DefaultLinkIndex))
                values.Add(LinkIndex);

            IEnumerable<String> names = Graphs.Select(g => g.ConfigName);
            values.AddRange(names);

            var settings = new List<String>
            {
                ConfigurationKeyword.GraphSet,
                ConfigurationKeyword.GraphSetTitle
            };

            if (ShowDefault || (!ShowDefault && LinkIndex != DefaultLinkIndex))
                settings.Add(ConfigurationKeyword.LinkIndex);

            int num = names.Count();
            for (int i = 0; i < num; i++)
                settings.Add(ConfigurationKeyword.GraphSetItem);

            return ConfigText(settings.ToArray(), values.ToArray());
        }

        public static IEnumerable<GraphSetConfiguration> Create(
            HostConfiguration server,
            IEnumerable<GraphBaseConfiguration> graphs,
            ConfigurationBlock config,
            string configPath)
        {
            var sets = new List<GraphSetConfiguration>();

            foreach (ConfigurationBlock imageBlock in FindBlocks(Identifier, config))
            {
                Dictionary<String, String> options = GetOptions(Params, imageBlock);
                string title = GetOptionString(options, ConfigurationKeyword.GraphSetTitle, configPath, false);
                int linkIndex = GetOptionInteger(options, ConfigurationKeyword.LinkIndex, configPath, false,
                    DefaultLinkIndex);

                var selectedGraphs = new List<GraphBaseConfiguration>();
                foreach (string name in GetMultiOption(ConfigurationKeyword.GraphSetItem, imageBlock))
                {
                    GraphBaseConfiguration graph = graphs.SingleOrDefault(p => p.GraphName == name);
                    if (graph != null)
                    {
                        selectedGraphs.Add(graph);
                    }
                    else
                    {
                        ConfigError(String.Format(
                            ConfigurationMessage.MissingReference,
                            ConfigurationKeyword.GraphSetItem, name, Identifier));
                        ConfigError(ConfigurationMessage.ConfiguredGraphs);
                        foreach (GraphBaseConfiguration graphConfig in graphs)
                            ConfigError(graphConfig.ConfigName);
                    }
                }

                sets.Add(new GraphSetConfiguration(String.Empty, config.IndentLevel + 1, title, linkIndex,
                    selectedGraphs));
            }

            return sets;
        }
    }
}