//
// TrafficGraphConfiguration.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
{
    public sealed class TrafficGraphConfiguration : GraphBaseConfiguration, ITrafficGraphConfiguration
    {
        public TrafficGraphConfiguration (
            ServerConfiguration server,
            string configName,
            int indentLevel,
            int frequency,
            bool initialRun,
            bool fixedRun,
            string title,
            int width,
            int height,
            int timespan,
            double received,
            double ignored,
            double dropped,
            double sent,
            double notSent,
            double receivedAvg,
            double ignoredAvg,
            double droppedAvg,
            double sentAvg,
            double notSentAvg,
            int plotInterval,
            int packetRate,
            DestinationCollection destinations,
            WebLinkCollection links)
                : base(
                    server,
                    configName,
                    indentLevel,
                    frequency,
                    initialRun,
                    fixedRun,
                    title,
                    width,
                    height,
                    timespan,
                    0.0,
                    destinations,
                    links)
        {
            this.received = received;
            this.ignored = ignored;
            this.dropped = dropped;
            this.sent = sent;
            this.notSent = notSent;
            this.receivedAvg = receivedAvg;
            this.ignoredAvg = ignoredAvg;
            this.droppedAvg = droppedAvg;
            this.sentAvg = sentAvg;
            this.notSentAvg = notSentAvg;
            this.plotInterval = plotInterval;
            this.packetRate = packetRate;
        }

        private static readonly String[] Params = new[] {
            "TrafficGraph",
            "Frequency",
            "InitialRun",
            "FixedRun",
            "Title",
            "Width",
            "Height",
            "Timespan",
            "Received",
            "Ignored",
            "Dropped",
            "Sent",
            "NotSent",
            "AvgReceived",
            "AvgIgnored",
            "AvgDropped",
            "AvgSent",
            "AvgNotSent",
            "PlotInterval",
            "PacketRate",
            FileCollection.Identifier,
            WebLinkCollection.Identifier
        };

        private readonly double received;
        private readonly double ignored;
        private readonly double dropped;
        private readonly double sent;
        private readonly double notSent;
        private readonly double receivedAvg;
        private readonly double ignoredAvg;
        private readonly double droppedAvg;
        private readonly double sentAvg;
        private readonly double notSentAvg;
        private readonly int plotInterval;
        private readonly int packetRate;

        public double Received {
            get { return received; }
        }

        public double Ignored {
            get { return ignored; }
        }

        public double Dropped {
            get { return dropped; }
        }

        public double Sent {
            get { return sent; }
        }

        public double NotSent {
            get { return notSent; }
        }

        public double ReceivedAvg {
            get { return receivedAvg; }
        }

        public double IgnoredAvg {
            get { return ignoredAvg; }
        }

        public double DroppedAvg {
            get { return droppedAvg; }
        }

        public double SentAvg {
            get { return sentAvg; }
        }

        public double NotSentAvg {
            get { return notSentAvg; }
        }

        public int PlotInterval {
            get { return plotInterval; }
        }

        public int PacketRate {
            get { return packetRate; }
        }

        protected override IEnumerable<Object> Items {
            get {
                return new object[] {
                    ConfigName,
                    Frequency,
                    InitialRun ? 1 : 0,
                    FixedRun ? 1 : 0,
                    Title,
                    Width,
                    Height,
                    Timespan,
                    Received,
                    Ignored,
                    Dropped,
                    Sent,
                    NotSent,
                    ReceivedAvg,
                    IgnoredAvg,
                    DroppedAvg,
                    SentAvg,
                    NotSentAvg,
                    PlotInterval,
                    PacketRate,
                    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 traffic 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 traffic 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 TrafficGraphConfiguration Create (
            ServerConfiguration server,
            ConfigurationBlock config)
        {
            Dictionary<String, String> options = GetOptions (Params, config);

            // Create
            return new TrafficGraphConfiguration (
                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),
                GetOptionDouble (options, Params [13], Params [0], false),
                GetOptionDouble (options, Params [14], Params [0], false),
                GetOptionDouble (options, Params [15], Params [0], false),
                GetOptionDouble (options, Params [16], Params [0], false),
                GetOptionDouble (options, Params [17], Params [0], false),
                GetOptionInteger (options, Params [18], Params [0], false, 15),
                GetOptionInteger (options, Params [19], Params [0], false, 1),
                FileCollection.Create (server, config),
                WebLinkCollection.Create (server, config));
        }
    }
}
