//
// PeerGraphConfiguration.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.Interface;
using Ntp.Analyzer.Localize;
using Ntp.Analyzer.Objects;
using Ntp.Config;

namespace Ntp.Analyzer.Config.Graph
{
    /// <summary>
    /// Peer graph configuration used when creating <seealso cref="Ntp.Analyzer.Graph.PeerGraph" />.
    /// </summary>
    public sealed class PeerGraphConfiguration : DispersionGraphConfiguration, IPeerGraphConfiguration
    {
        private PeerGraphConfiguration(
            HostConfiguration server,
            string configName,
            int indentLevel,
            int frequency,
            bool initialRun,
            bool fixedRun,
            string title,
            int width,
            int height,
            int timespan,
            double filterFactor,
            double jitter,
            double offset,
            double balance,
            double delay,
            DestinationCollection destinations,
            WebLinkCollection links)
            : base(
                server,
                configName,
                indentLevel,
                frequency,
                initialRun,
                fixedRun,
                title,
                width,
                height,
                timespan,
                filterFactor,
                offset,
                jitter,
                destinations,
                links)
        {
            this.balance = balance;
            this.delay = delay;
        }

        public const string Identifier = ConfigurationKeyword.PeerGraph;

        private static readonly String[] Params =
        {
            ConfigurationKeyword.PeerGraph,
            ConfigurationKeyword.Frequency,
            ConfigurationKeyword.InitialRun,
            ConfigurationKeyword.FixedRun,
            ConfigurationKeyword.GraphTitle,
            ConfigurationKeyword.GraphWidth,
            ConfigurationKeyword.GraphHeight,
            ConfigurationKeyword.GraphTimespan,
            ConfigurationKeyword.GraphFilterFactor,
            ConfigurationKeyword.GraphJitter,
            ConfigurationKeyword.GraphOffset,
            ConfigurationKeyword.PeerGraphBalance,
            ConfigurationKeyword.PeerGraphDelay,
            DirectoryCollection.Identifier,
            WebLinkCollection.Identifier
        };

        private readonly double balance;
        private readonly double delay;

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

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

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

                if (ShowDefault || (!ShowDefault && Title != DefaultTitle))
                    items.Add(Title);

                if (ShowDefault || (!ShowDefault && Width != DefaultWidth))
                    items.Add(Width);

                if (ShowDefault || (!ShowDefault && Height != DefaultHeight))
                    items.Add(Height);

                if (ShowDefault || (!ShowDefault && Timespan != DefaultTimespan))
                    items.Add(Timespan);

                if (ShowDefault || (!ShowDefault && FilterFactor != DefaultValue))
                    items.Add(FilterFactor);

                if (ShowDefault || (!ShowDefault && Balance != DefaultValue))
                    items.Add(Balance);

                if (ShowDefault || (!ShowDefault && Delay != DefaultValue))
                    items.Add(Delay);

                if (ShowDefault || (!ShowDefault && Jitter != DefaultValue))
                    items.Add(Jitter);

                if (ShowDefault || (!ShowDefault && Offset != DefaultValue))
                    items.Add(Offset);

                items.Add(Destinations);

                if (ShowDefault || (!ShowDefault &&
                                    Destinations.Count() == 1 && Links.Count() == 1 &&
                                    Destinations.First().Link != Links.First().Link))
                    items.Add(Links);

                return items.ToArray();
            }
        }

        /// <summary>
        /// Gets the balance multiplication factor.
        /// </summary>
        /// <value>The balance.</value>
        public double Balance
        {
            get { return balance; }
        }

        /// <summary>
        /// Gets the delay multiplication factor.
        /// </summary>
        /// <value>The delay.</value>
        public double Delay
        {
            get { return delay; }
        }

        public override string GetTitle(NamedObject namedObject)
        {
            return String.Concat(namedObject.Name, " ", Title);
        }

        public override string GetLink(GraphSetConfiguration owner, string postfix)
        {
            int linkIndex = owner.LinkIndex;
            string peerName = postfix;

            if (linkIndex <= 0 || linkIndex > Links.Destinations.Count)
                throw new ConfigurationException(
                    String.Format(ConfigurationMessage.PeerGraphLinkIndex, linkIndex));

            return WebPath + Links.Destinations[linkIndex - 1].Link + peerName.Replace('.', '-') + ".png";
        }

        public override string GetAltName(GraphSetConfiguration owner, string postfix)
        {
            string peerName = postfix;

            return GraphName + "-" + peerName.Replace('.', '-') + ".png";
        }

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

        public override string ToString()
        {
            var list = new List<String> {ConfigurationKeyword.PeerGraph, ConfigurationKeyword.Frequency};

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

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

            if (ShowDefault || (!ShowDefault && Title != DefaultTitle))
                list.Add(ConfigurationKeyword.GraphTitle);

            if (ShowDefault || (!ShowDefault && Width != DefaultWidth))
                list.Add(ConfigurationKeyword.GraphWidth);

            if (ShowDefault || (!ShowDefault && Height != DefaultHeight))
                list.Add(ConfigurationKeyword.GraphHeight);

            if (ShowDefault || (!ShowDefault && Timespan != DefaultTimespan))
                list.Add(ConfigurationKeyword.GraphTimespan);

            if (ShowDefault || (!ShowDefault && FilterFactor != DefaultValue))
                list.Add(ConfigurationKeyword.GraphFilterFactor);

            if (ShowDefault || (!ShowDefault && Balance != DefaultValue))
                list.Add(ConfigurationKeyword.PeerGraphBalance);

            if (ShowDefault || (!ShowDefault && Delay != DefaultValue))
                list.Add(ConfigurationKeyword.PeerGraphDelay);

            if (ShowDefault || (!ShowDefault && Jitter != DefaultValue))
                list.Add(ConfigurationKeyword.GraphJitter);

            if (ShowDefault || (!ShowDefault && Offset != DefaultValue))
                list.Add(ConfigurationKeyword.GraphOffset);

            list.Add(FileCollection.Identifier);

            if (ShowDefault || (!ShowDefault &&
                                Destinations.Count() == 1 && Links.Count() == 1 &&
                                Destinations.First().Link != Links.First().Link))
                list.Add(WebLinkCollection.Identifier);

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

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

            return new PeerGraphConfiguration(
                server,
                GetOptionConfigName(options, ConfigurationKeyword.PeerGraph, Identifier, true),
                config.IndentLevel,
                GetOptionInteger(options, ConfigurationKeyword.Frequency, Identifier, true),
                GetOptionBoolean(options, ConfigurationKeyword.InitialRun, Identifier, false, DefaultInitialRun),
                GetOptionBoolean(options, ConfigurationKeyword.FixedRun, Identifier, false, DefaultFixedRun),
                GetOptionString(options, ConfigurationKeyword.GraphTitle, Identifier, false, DefaultTitle),
                GetOptionInteger(options, ConfigurationKeyword.GraphWidth, Identifier, false, DefaultWidth),
                GetOptionInteger(options, ConfigurationKeyword.GraphHeight, Identifier, false, DefaultHeight),
                GetOptionInteger(options, ConfigurationKeyword.GraphTimespan, Identifier, false, DefaultTimespan),
                GetOptionDouble(options, ConfigurationKeyword.GraphFilterFactor, Identifier, false),
                GetOptionDouble(options, ConfigurationKeyword.GraphJitter, Identifier, false),
                GetOptionDouble(options, ConfigurationKeyword.GraphOffset, Identifier, false),
                GetOptionDouble(options, ConfigurationKeyword.PeerGraphBalance, Identifier, false),
                GetOptionDouble(options, ConfigurationKeyword.PeerGraphDelay, Identifier, false),
                DirectoryCollection.Create(server, config),
                WebLinkCollection.Create(server, config));
        }
    }
}