//
// PeerGraphPageConfiguration.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.Objects;
using Ntp.Config;

namespace Ntp.Analyzer.Config.Page
{
    public sealed class PeerGraphPageConfiguration : GraphPageConfiguration
    {
        private PeerGraphPageConfiguration (
            ServerConfiguration server,
            string configName,
            int indentLevel,
            int frequency,
            bool initialRun,
            bool fixedRun,
            string theme,
            string title,
            PeerPageConfiguration page,
            int linkIndex,
            DestinationCollection destinations,
            string link)
            : base(
                server,
                configName,
                indentLevel,
                frequency,
                initialRun,
                fixedRun,
                theme,
                title,
                linkIndex,
                destinations,
                link)
        {
            this.page = page;

            if (page != null) {
                page.AttachGraphPages (this);
            }
        }

        private static readonly String[] Params = new[] {
            "PeerGraphPage",
            "Frequency",
            "InitialRun",
            "FixedRun",
            "Template",
            "PageTitle",
            "GraphPage",
            "LinkIndex",
            DirectoryCollection.Identifier,
            "Link"
        };
        private readonly PeerPageConfiguration page;

        public override IEnumerable<GraphSetConfiguration> GraphSets {
            get { return page.Graphs; }
        }

        protected override IEnumerable<Object> Items {
            get {
                return new object[] {
                    ConfigName,
                    Frequency,
                    InitialRun ? 1 : 0,
                    FixedRun ? 1 : 0,
                    Theme,
                    Title,
                    page != null ? page.ConfigName : null,
                    LinkIndex,
                    Destinations,
                    Link
                };
            }
        }

        public string GetFileName (Host host, TimeServer peer, GraphSetConfiguration graphSet, GraphBaseConfiguration graph)
        {
            return host.Name.Replace ('.', '-') + "_" +
            graph.GetAltName (graphSet, peer.Name).
                         Replace (".png", String.Empty).
                         Replace ('.', '-')
            + ".html";
        }

        public string GetLink (Host host, TimeServer peer, GraphSetConfiguration graphSet, GraphBaseConfiguration graph)
        {
            return WebPath + Link +
            host.Name.Replace ('.', '-') + "_" +
            graph.GetAltName (graphSet, peer.Name).
                         Replace (".png", String.Empty).
                         Replace ('.', '-')
            + ".html";
        }

        public override string ToString ()
        {
            return ConfigText (Params, Items);
        }

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

            string peerPageName = GetOptionString (options, "GraphPage", "PeerGraphPage", true);
            PeerPageConfiguration peerPage = peerPages.SingleOrDefault (pp => pp.ConfigName == peerPageName);

            if (peerPage == null) {
                ConfigError ("Cannot find GraphPage " + peerPageName + " specified in " + Params [0]);
            }

            return new PeerGraphPageConfiguration (
                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,
                GetOptionInteger (options, "LinkIndex", Params [0], true),
                DirectoryCollection.Create (server, config),
                GetOptionString (options, "Link", Params [0], true)
            );
        }
    }
}