//
// SummaryCollection.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.Localize;
using Ntp.Config;

namespace Ntp.Analyzer.Config.Page.Collection
{
    public sealed class SummaryCollection : ConfigurationBase
    {
        private SummaryCollection(
            string configName,
            int indentLevel,
            IEnumerable<PeerSummaryPageConfiguration> pages)
            : base(configName, indentLevel)
        {
            this.pages = new List<PeerSummaryPageConfiguration>(pages);
        }

        public const string Identifier = ConfigurationKeyword.Summaries;
        private readonly List<PeerSummaryPageConfiguration> pages;

        protected override IEnumerable<Object> Items
        {
            get { return pages; }
        }

        public IEnumerable<PeerSummaryPageConfiguration> Pages
        {
            get { return pages; }
        }

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

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

            var settings = new List<string> {Identifier};

            int num = names.Count();

            for (int i = 0; i < num; i++)
                settings.Add(PeerSummaryPageConfiguration.Identifier);

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

        public static SummaryCollection Create(
            HostConfiguration server,
            IEnumerable<PeerSummaryPageConfiguration> peerSummaries,
            ConfigurationBlock config,
            string configPath)
        {
            var summaries = new List<PeerSummaryPageConfiguration>();

            ConfigurationBlock block = FindSingleBlock(Identifier, config);
            if (block != null)
            {
                foreach (string name in GetMultiOption(PeerSummaryPageConfiguration.Identifier, block))
                {
                    PeerSummaryPageConfiguration summary = peerSummaries.SingleOrDefault(
                        ps => ps.ConfigName == name);

                    if (summary != null)
                        summaries.Add(summary);
                    else
                    {
                        ConfigError(String.Format(
                            ConfigurationMessage.MissingReference,
                            PeerSummaryPageConfiguration.Identifier, name, Identifier
                            ));

                        ConfigError(ConfigurationMessage.ConfiguredPeerSummaryPages);
                        foreach (PeerSummaryPageConfiguration page in peerSummaries)
                            ConfigError(page.ConfigName);
                    }
                }
            }

            return new SummaryCollection(String.Empty, config.IndentLevel + 1, summaries);
        }
    }
}