//
// PeerStatJob.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 System.Net;
using Ntp.Analyzer.Config.Stats;
using Ntp.Analyzer.Data;
using Ntp.Analyzer.Log;
using Ntp.Analyzer.Objects;
using Ntp.Analyzer.Objects.Live;
using Ntp.Process;

namespace Ntp.Analyzer.Process.Description
{
    /// <summary>
    /// Job which read statistics about peers and saves the result to a database.
    /// </summary>
    public sealed class PeerStatJob : JobDescription
    {
       public PeerStatJob (PeerStatConfiguration config, LogBase log)
            : base(config, log)
        {
            this.config = config;
            this.readings = new List<PeerReading> ();
        }

        private readonly PeerStatConfiguration config;
        private readonly List<PeerReading> readings;
        private Host host;
        private List<Peer> peers;

        public override bool SingleThread {
            get { return false; }
        }

        public override string JobType {
            get { return "Peer stats"; }
        }

        public override int Priority {
            get { return 2; }
        }

        protected override void InternalExecute ()
        {
            try {
                Initalize ();              
                Import ();             
                SaveResult ();
            } catch (Exception e) {
                Log.WriteLine ("Could not import peer data: " + e.Message, Severity.Warn);
                Log.WriteLine (e, Severity.Trace);
            }
        }

        /// <summary>
        /// Initalize database context.
        /// </summary>
        private void Initalize ()
        {
            // Clear any previouse readings
            readings.Clear();

            // Get peers from database.
            peers = DataFace.Instance.Peers.ToList ();

            // Find host in database.
            host = DataFace.Instance.Hosts.SingleOrDefault (h => h.Id == config.HostId);

           if (host == null) {
                Log.WriteLine (String.Format (
                    "Host with ID {0} was not found in database. ", config.HostId),
                    Severity.Warn);
            }
        }

        /// <summary>
        /// Import statistics.
        /// </summary>
        private void Import ()
        {
            // Generate statistics
            foreach (AssociationEntry entry in DataFace.Instance.NtpqCache[config.ServerName]) {
                // Find peer in database.
                IEnumerable<Peer> peerList = peers.Where (p => p.Ip == entry.Remote);
                Peer peer;

                if (peerList.Count () == 1) {
                    peer = peerList.Single ();
                } else if (peerList.Count () == 0) {
                    Log.WriteLine (String.Format (
                        "Peer with IP {0} was not found in database.", entry.Remote),
                        Severity.Warn);
                    continue;
                } else {
                    Log.WriteLine (String.Format (
                        "Found more than on peer with IP {0} in database.", entry.Remote),
                        Severity.Warn);
                    continue;
                }

                PeerReading reading = new PeerReading (
                                          host,
                                          peer,
                                          ReadingBulk.Current,
                                          entry.LastPoll,
                                          entry.Reach,
                                          entry.Delay,
                                          entry.Offset,
                                          entry.Jitter);

                readings.Add (reading);
            }
        }

        /// <summary>
        /// Saves the result of the import to database.
        /// </summary>
        private void SaveResult ()
        {
            foreach (PeerReading reading in readings) {
                DataFace.Instance.PeerReadings.Save (reading);
            }
        }
    }
}