//
// PeerSummaryPageConfiguration.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.Destination;
using Ntp.Analyzer.Config.Graph;
using Ntp.Analyzer.Config.Navigation;
using Ntp.Analyzer.Localize;
using Ntp.Config;

namespace Ntp.Analyzer.Config.Page
{
    public sealed class PeerSummaryPageConfiguration : PageConfigurationBase, ILinkable
    {
        private PeerSummaryPageConfiguration(
            HostConfiguration server,
            string configName,
            int indentLevel,
            int frequency,
            bool initialRun,
            bool fixedRun,
            string theme,
            string title,
            PeerPageConfiguration peerPage,
            IEnumerable<GraphSetConfiguration> graphs,
            DestinationCollection destinations,
            string link)
            : base(
                server,
                configName,
                indentLevel,
                frequency,
                initialRun,
                fixedRun,
                theme,
                title,
                destinations,
                link)
        {
            this.peerPage = peerPage;
            this.graphs = new List<GraphSetConfiguration>(graphs);
        }

        public const string Identifier = ConfigurationKeyword.PeerSummaryPage;

        private new const bool DefaultInitialRun = true;
        private new const bool DefaultFixedRun = false;
        private const int DefaultFrequency = 0;

        private static readonly String[] Params =
        {
            ConfigurationKeyword.PeerSummaryPage,
            ConfigurationKeyword.Frequency,
            ConfigurationKeyword.InitialRun,
            ConfigurationKeyword.FixedRun,
            ConfigurationKeyword.PageTemplate,
            ConfigurationKeyword.PageTitle,
            PeerPageConfiguration.Identifier,
            GraphSetConfiguration.Identifier,
            DirectoryCollection.Identifier,
            ConfigurationKeyword.PageLink
        };

        private readonly List<GraphSetConfiguration> graphs;
        private readonly PeerPageConfiguration peerPage;

        /// <summary>
        /// Gets the graphs to show on this page.
        /// </summary>
        /// <value>The graphs.</value>
        public IEnumerable<GraphSetConfiguration> Graphs
        {
            get { return graphs; }
        }

        protected override IEnumerable<Object> Items
        {
            get
            {
                var items = new List<object> {ConfigName};

                if (ShowDefault || (!ShowDefault && Frequency != DefaultFrequency))
                    items.Add(Frequency);

                if (ShowDefault || (!ShowDefault && InitialRun != DefaultInitialRun))
                    items.Add(InitialRun);

                if (ShowDefault || (!ShowDefault && FixedRun != DefaultFixedRun))
                    items.Add(FixedRun);

                if (ShowDefault || (!ShowDefault && Theme.ToLower() != DefaultTheme.ToLower()))
                    items.Add(Theme);

                items.Add(Title);

                if (peerPage != null)
                    items.Add(peerPage.ConfigName);

                items.Add(Graphs);
                items.Add(Destinations);

                if (ShowDefault || (!ShowDefault && Link != DefaultLink))
                    items.Add(Link);

                return items.ToArray();
            }
        }

        /// <summary>
        /// Gets the link related to specified peer.
        /// </summary>
        /// <returns>The peer link.</returns>
        /// <param name="peerId">Peer identifier.</param>
        public string GetPeerLink(int peerId)
        {
            return peerPage.GetLink(peerId);
        }

        public override string ToString()
        {
            var list = new List<string> {ConfigurationKeyword.PeerSummaryPage};

            if (ShowDefault || (!ShowDefault && Frequency != DefaultFrequency))
                list.Add(ConfigurationKeyword.Frequency);

            if (ShowDefault || (!ShowDefault && InitialRun != DefaultInitialRun))
                list.Add(ConfigurationKeyword.InitialRun);

            if (ShowDefault || (!ShowDefault && FixedRun != DefaultFixedRun))
                list.Add(ConfigurationKeyword.FixedRun);

            if (ShowDefault || (!ShowDefault && Theme.ToLower() != DefaultTheme.ToLower()))
                list.Add(ConfigurationKeyword.PageTemplate);

            list.Add(ConfigurationKeyword.PageTitle);

            if (peerPage != null)
                list.Add(PeerPageConfiguration.Identifier);

            list.Add(GraphSetConfiguration.Identifier);
            list.Add(DirectoryCollection.Identifier);

            if (ShowDefault || (!ShowDefault && Link != DefaultLink))
                list.Add(ConfigurationKeyword.PageLink);

            return ConfigText(list.ToArray(), Items);
        }

        public static PeerSummaryPageConfiguration Create(
            HostConfiguration server,
            ConfigurationBlock config,
            IEnumerable<PeerPageConfiguration> peerPages,
            IEnumerable<PeerGraphConfiguration> peerGraphs)
        {
            Dictionary<String, String> options = GetOptions(Params, config);

            string peerPageName = options[PeerPageConfiguration.Identifier];
            PeerPageConfiguration peerPage = peerPages.SingleOrDefault(pp => pp.ConfigName == peerPageName);

            if (peerPage == null)
            {
                ConfigError(String.Format(
                    ConfigurationMessage.MissingReference,
                    PeerPageConfiguration.Identifier, peerPageName, Identifier));

                ConfigError(ConfigurationMessage.ConfiguredPeerPages);
                foreach (PeerPageConfiguration peerPageConfig in peerPages)
                    ConfigError(peerPageConfig.ConfigName);
            }

            return new PeerSummaryPageConfiguration(
                server,
                GetOptionConfigName(options, ConfigurationKeyword.PeerSummaryPage, Identifier, true),
                config.IndentLevel,
                GetOptionInteger(options, ConfigurationKeyword.Frequency, Identifier, false, DefaultFrequency),
                GetOptionBoolean(options, ConfigurationKeyword.InitialRun, Identifier, false, DefaultInitialRun),
                GetOptionBoolean(options, ConfigurationKeyword.FixedRun, Identifier, false, DefaultFixedRun),
                GetOptionString(options, ConfigurationKeyword.PageTemplate, Identifier, false, DefaultTheme),
                GetOptionString(options, ConfigurationKeyword.PageTitle, Identifier, false, String.Empty),
                peerPage,
                GraphSetConfiguration.Create(server, peerGraphs, config, Identifier),
                FileCollection.Create(server, config),
                GetOptionString(options, ConfigurationKeyword.PageLink, Identifier, false, null)
                );
        }
    }
}