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

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 (
            ServerConfiguration 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;
        }

        private static readonly String[] Params = new[] {
            "PeerGraph",
            "Frequency",
            "InitialRun",
            "FixedRun",
            "Title",
            "Width",
            "Height",
            "Timespan",
            "FilterFactor",
            "Jitter",
            "Offset",
            "Balance",
            "Delay",
            DirectoryCollection.Identifier,
            WebLinkCollection.Identifier
        };

        private readonly double balance;
        private readonly double delay;
        /// <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; }
        }

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

        public override string GetTitle (NamedObject namedObject)
        {
            return 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 ("Index out of range while getting peer graph link [{0}].", 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 ()
        {
            return ConfigText (Params, Items);
        }

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

            return new PeerGraphConfiguration (
                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),
                DirectoryCollection.Create (server, config),
                WebLinkCollection.Create (server, config));
        }
    }
}