//
// HostPageConfiguration.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.Analyzer.Config.Page.Collection;
using Ntp.Config;

namespace Ntp.Analyzer.Config.Page
{
    /// <summary>
    /// Configuration for a HTML page showing status and graphs for an hosted NTP server.
    /// </summary>
    public sealed class HostPageConfiguration : PageConfigurationBase, ILinkable
    {
        private HostPageConfiguration (
            ServerConfiguration server,
            string configName,
            int indentLevel,
            int frequency,
            bool initialRun,
            bool fixedRun,
            string theme,
            string title,
            bool poolMember,
            PeerPageConfiguration peerPage,
            IEnumerable<GraphSetConfiguration> graphs,
            SummeryCollection summeries,
            FileCollection destinations,
            string link)
            : base(
                server,
                configName,
                indentLevel,
                frequency,
                initialRun,
                fixedRun,
                theme,
                title,
                destinations,
                link)
        {
            this.poolMember = poolMember;
            this.peerPage = peerPage;
            this.graphs = new List<GraphSetConfiguration> (graphs);
            this.summeries = summeries;
            graphPages = new List<HostGraphPageConfiguration> ();
        }

        /// <summary>
        /// The parameters used in configuration files.
        /// </summary>
        private static readonly String[] Params = new[] {
            "HostPage",
            "Frequency",
            "InitialRun",
            "FixedRun",
            "Template",
            "PageTitle",
            "PoolMember",
            "PeerPages",
            GraphSetConfiguration.Identifier,
            SummeryCollection.Identifier,
            FileCollection.Identifier,
            "Link"
        };

        private readonly List<HostGraphPageConfiguration> graphPages;
        private readonly List<GraphSetConfiguration> graphs;
        private readonly PeerPageConfiguration peerPage;
        private readonly bool poolMember;
        private readonly SummeryCollection summeries;

        /// <summary>
        /// Gets a value indicating whether there should be shown a link to pool.ntp.org.
        /// </summary>
        /// <value>
        ///     <c>true</c> if pool member; otherwise, <c>false</c>.
        /// </value>
        public bool PoolMember {
            get { return poolMember; }
        }

        /// <summary>
        /// Gets the peer pages linked to this page.
        /// </summary>
        /// <value>The peer page.</value>
        public PeerPageConfiguration PeerPage {
            get { return peerPage; }
        }

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

        /// <summary>
        /// Gets the peer summeries to be linked from this page.
        /// </summary>
        /// <value>The peer summeries.</value>
        public SummeryCollection PeerSummeries {
            get { return summeries; }
        }

        /// <summary>
        /// Gets the host graph pages to be linked from this page.
        /// </summary>
        /// <value>The host graph pages.</value>
        public IEnumerable<HostGraphPageConfiguration> GraphPages {
            get { return graphPages; }
        }

        /// <summary>
        /// Gets the values of settings in this configuration.
        /// </summary>
        /// <value>The items.</value>
        protected override IEnumerable<Object> Items {
            get {
                return new object[] {
                    ConfigName,
                    Frequency,
                    InitialRun ? 1 : 0,
                    FixedRun ? 1 : 0,
                    Theme,
                    Title,
                    PoolMember ? 1 : 0,
                    PeerPage.ConfigName,
                    Graphs,
                    PeerSummeries,
                    Destinations,
                    Link
                };
            }
        }

        /// <summary>
        /// Attachs the host graph pages to this page.
        /// </summary>
        /// <remarks>
        /// Only use from <see cref="HostGraphPageConfiguration" />
        /// </remarks>
        /// <param name="page">Page.</param>
        internal void AttachGraphPages (HostGraphPageConfiguration page)
        {
            graphPages.Add (page);
        }

        /// <summary>
        /// Returns a <see cref="System.String" /> that represents the current
        ///     <see
        ///         cref="Ntp.Analyzer.Config.Page.HostPageConfiguration" />
        /// .
        /// </summary>
        /// <returns>
        /// A <see cref="System.String" /> that represents the current <see cref="Ntp.Analyzer.Config.Page.HostPageConfiguration" />.
        /// </returns>
        public override string ToString ()
        {
            return ConfigText (Params, Items);
        }

        /// <summary>
        /// Factory method.
        /// </summary>
        /// <param name="server">Server.</param>
        /// <param name="config">Config.</param>
        /// <param name="peerPages">Peer pages.</param>
        /// <param name="hostGraphs">Host graphs.</param>
        /// <param name="peerSummeries">Peer summeries.</param>
        public static HostPageConfiguration Create (
            ServerConfiguration server,
            ConfigurationBlock config,
            IEnumerable<PeerPageConfiguration> peerPages,
            IEnumerable<GraphBaseConfiguration> hostGraphs,
            IEnumerable<PeerSummeryPageConfiguration> peerSummeries)
        {
            OptionSet options = GetOptions (Params, config);

            string peerPageName = GetOptionString (options, Params [7], Params [0], false);
            PeerPageConfiguration peerPage = null;

            if (peerPageName != String.Empty) {
                peerPage = peerPages.SingleOrDefault (pp => pp.ConfigName == peerPageName);

                if (peerPage == null) {
                    ConfigError ("Cannot find PeerPages " + peerPageName + " specified in HostPage");
                }
            }

            return new HostPageConfiguration (
                server,
                GetOptionConfigName (options, Params [0], Params [0], true),
                config.IndentLevel,
                GetOptionInteger (options, Params [1], Params [0], true),
                GetOptionBoolean (options, Params [2], Params [0], false, true),
                GetOptionBoolean (options, Params [3], Params [0], false, true),
                GetOptionString (options, Params [4], Params [0], false, "default"),
                GetOptionString (options, Params [5], Params [0], false, String.Empty),
                GetOptionBoolean (options, Params [6], Params [0], false),
                peerPage,
                GraphSetConfiguration.Create (server, hostGraphs, config, Params [0]),
                SummeryCollection.Create (server, peerSummeries, config, Params [0]),
                FileCollection.Create (server, config),
                GetOptionString (options, Params [11], Params [0], false, String.Empty)
            );
        }
    }
}