//
// HostGraphConfiguration.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.Objects;
using Ntp.Config;
using Ntp.Analyzer.Config.Destination;
using Ntp.Analyzer.Graph.Interface;

namespace Ntp.Analyzer.Config.Graph
{
    /// <summary>
    /// Host graph configuration used when creating <seealso cref="Ntp.Analyzer.Graph.HostGraph" />.
    /// </summary>
    public sealed class HostGraphConfiguration : DispersionGraphConfiguration, IHostGraphConfiguration
    {
        private HostGraphConfiguration (
            ServerConfiguration server,
            string configName,
            int indentLevel,
            int frequency,
            bool initialRun,
            bool fixedRun,
            string title,
            int width,
            int height,
            int timespan,
            double filterFactor,
            double gfrequency,
            double stability,
            double jitter,
            double offset,
            DestinationCollection destinations,
            WebLinkCollection links)
            : base(
                server,
                configName,
                indentLevel,
                frequency,
                initialRun,
                fixedRun,
                title,
                width,
                height,
                timespan,
                filterFactor,
                offset,
                jitter,
                destinations,
                links)
        {
            this.stability = stability;
            this.gfrequency = gfrequency;
        }

        private static readonly String[] Params = new[] {
            "HostGraph",
            "Frequency",
            "InitialRun",
            "FixedRun",
            "Title",
            "Width",
            "Height",
            "Timespan",
            "FilterFactor",
            "GFrequency",
            "Stability",
            "Jitter",
            "Offset",
            FileCollection.Identifier,
            WebLinkCollection.Identifier
        };

        private readonly double gfrequency;
        private readonly double stability;
        /// <summary>
        /// Gets the stability multiplication factor.
        /// </summary>
        /// <value>The stability.</value>
        public double Stability {
            get { return stability; }
        }

        /// <summary>
        /// Gets the frequency factor.
        /// </summary>
        /// <remarks>
        /// Frequency factor is obtained from NTP hosts.
        /// </remarks>
        /// <value>The frequency.</value>
        public double Gfrequency {
            get { return gfrequency; }
        }

        protected override IEnumerable<Object> Items {
            get {
                return new object[] {
                    ConfigName,
                    Frequency,
                    InitialRun ? 1 : 0,
                    FixedRun ? 1 : 0,
                    Title,
                    Width,
                    Height,
                    Timespan,
                    FilterFactor,
                    Gfrequency,
                    Stability,
                    Jitter,
                    Offset,
                    Destinations,
                    links
                };
            }
        }

        public override string GetTitle (NamedObject namedObject)
        {
            return Title;
        }

        /// <summary>
        /// Gets the link to use when referred to from web pages.
        /// </summary>
        /// <returns>The link.</returns>
        /// <param name="owner">Owner.</param>
        /// <param name="postfix">Text to prepend</param>
        public override string GetLink (GraphSetConfiguration owner, string postfix)
        {
            int index = owner.LinkIndex;

            if (index <= 0 || index > links.Destinations.Count ())
                throw new ConfigurationException (
                    String.Format ("Index out of range while getting host graph link [{0}].", index));

            return WebPath + links.Destinations [index - 1].Link;
        }

        /// <summary>
        /// Gets the alternative name when referred to from web pages.
        /// </summary>
        /// <returns>The alternate name.</returns>
        /// <param name="owner">Owner.</param>
        /// <param name="postfix">Text to prepend</param>
        public override string GetAltName (GraphSetConfiguration owner, string postfix)
        {
            int index = owner.LinkIndex;

            if (index <= 0 || index > links.Destinations.Count ())
                throw new ConfigurationException (
                    String.Format ("Index out of range while getting host graph alt name [{0}].", index));

            string name = links.Destinations [index - 1].Link;

            // remove any path from name
            int pos = name.LastIndexOf ('/');
            name = pos != -1 ? name.Remove (0, pos + 1) : name;

            return name;
        }

        public override string GetPageLink (GraphSetConfiguration graphSet, string prefix, string postfix)
        {
            return WebPath + prefix + GetAltName (graphSet, postfix).Replace ('.', '-') + ".html";
        }

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

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

            // Create
            return new HostGraphConfiguration (
                server,
                GetOptionConfigName (options, Params [0], Params [0], true),
                config.IndentLevel,
                GetOptionInteger (options, Params [1], Params [0], true),
                GetOptionBoolean (options, Params [2], Params [0], false, false),
                GetOptionBoolean (options, Params [3], Params [0], false, false),
                GetOptionString (options, Params [4], Params [0], false, String.Empty),
                GetOptionInteger (options, Params [5], Params [0], false, 1024),
                GetOptionInteger (options, Params [6], Params [0], false, 800),
                GetOptionInteger (options, Params [7], Params [0], false, 60 * 12),
                GetOptionDouble (options, Params [8], Params [0], false),
                GetOptionDouble (options, Params [9], Params [0], false),
                GetOptionDouble (options, Params [10], Params [0], false),
                GetOptionDouble (options, Params [11], Params [0], false),
                GetOptionDouble (options, Params [12], Params [0], false),
                FileCollection.Create (server, config),
                WebLinkCollection.Create (server, config));
        }
    }
}