//
// PeerSummeryPageConfiguration.cs
//
// Author:
//       Carsten Sonne Larsen <cs@innolan.dk>
//
// Copyright (c) 2013 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.Config;

namespace Ntp.Analyzer.Config.Page
{
    public sealed class PeerSummeryPageConfiguration : PageConfigurationBase, ILinkable
    {
        private PeerSummeryPageConfiguration (
            ServerConfiguration 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);
        }

        private static readonly String[] Params = new[] {
            "PeerSummeryPage",
            "Frequency",
            "InitialRun",
            "FixedRun",
            "Template",
            "PageTitle",
            "PeerPages",
            GraphSetConfiguration.Identifier,
            DirectoryCollection.Identifier,
            "Link"
        };
        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 {
                return new object[] {
                    ConfigName,
                    Frequency,
                    InitialRun ? 1 : 0,
                    FixedRun ? 1 : 0,
                    Theme,
                    Title,
                    peerPage != null ? peerPage.ConfigName : null,
                    Graphs,
                    Destinations,
                    Link
                };
            }
        }

        /// <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 ()
        {
            return ConfigText (Params, Items);
        }

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

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

            if (peerPage == null) {
                ConfigError ("Cannot find PeerPages " + peerPageName + " specified in " + Params [0]);
                ConfigError ("Known peer pages: ");

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

            return new PeerSummeryPageConfiguration (
                server,
                GetOptionConfigName (options, Params [0], Params [0], true),
                config.IndentLevel,
                GetOptionInteger (options, "Frequency", Params [0], true),
                GetOptionBoolean (options, "InitialRun", Params [0], false, true),
                GetOptionBoolean (options, "FixedRun", Params [0], false, true),
                GetOptionString (options, "Template", Params [0], false, "default"),
                GetOptionString (options, "PageTitle", Params [0], false, String.Empty),
                peerPage,
                GraphSetConfiguration.Create (server, peerGraphs, config, Params [0]),
                FileCollection.Create (server, config),
                GetOptionString (options, "Link", Params [0], true)
            );
        }
    }
}