//
// AssociationEntry.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;

namespace Ntp.Analyzer.Objects
{
    /// <summary>
    /// Ntpq peer entry.
    /// </summary>
    /// <remarks>
    /// From man page of FreeBSD 9.1, dated May 17, 2006
    /// ntpq -p
    /// Obtains a current list peers of the server, along with a summary
    /// of each peer's state.  Summary information includes the address
    /// of the remote peer, the reference ID (0.0.0.0 if this is
    /// unknown), the stratum of the remote peer, the type of the peer
    /// (local, unicast, multicast or broadcast), when the last packet
    /// was received, the polling interval, in seconds, the reachability
    /// register, in octal, and the current estimated delay, offset and
    /// dispersion of the peer, all in milliseconds.  The character at
    /// the left margin of each line shows the synchronization status of
    /// the association and is a valuable diagnostic tool.  The encoding
    /// and meaning of this character, called the tally code, is given
    /// later in this page.
    /// </remarks>
    public sealed class AssociationEntry : PersistentObject
    {
        public AssociationEntry(
            int id, int hostId, char tallyChar,
            string remote, string refid, int stratus, char peerType,
            int lastPoll, int poll, int reach,
            double delay, double offset, double jitter)
            : base(id)
        {
            this.hostId = hostId;
            this.tallyChar = tallyChar;
            this.remote = remote;
            this.refid = refid;
            this.stratus = stratus;
            this.peerType = peerType;
            this.lastPoll = lastPoll;
            this.poll = poll;
            this.reach = reach;
            this.delay = delay;
            this.offset = offset;
            this.jitter = jitter;
        }

        public AssociationEntry(
            int hostId, char tallyChar,
            string remote, string refid, int stratus, char peerType,
            int lastPoll, int poll, int reach,
            double delay, double offset, double jitter)
        {
            this.hostId = hostId;
            this.tallyChar = tallyChar;
            this.remote = remote;
            this.refid = refid;
            this.stratus = stratus;
            this.peerType = peerType;
            this.lastPoll = lastPoll;
            this.poll = poll;
            this.reach = reach;
            this.delay = delay;
            this.offset = offset;
            this.jitter = jitter;
        }

        private readonly double delay;
        private readonly int hostId;
        private readonly double jitter;
        private readonly int lastPoll;
        private readonly double offset;
        private readonly char peerType;
        private readonly int poll;
        private readonly int reach;
        private readonly string refid;
        private readonly string remote;
        private readonly int stratus;
        private readonly char tallyChar;

        /// <summary>
        /// Gets the tally code of this association.
        /// <seealso cref="Ntp.Analyzer.Objects.AssociationEntry.TallyChar" />
        /// </summary>
        /// <value>
        /// The tally code.
        /// </value>
        public TallyCode TallyCode
        {
            get
            {
                switch (tallyChar)
                {
                    case ' ':
                        return TallyCode.Reject;
                    case 'x':
                        return TallyCode.FalseTick;
                    case '.':
                        return TallyCode.Excess;
                    case '-':
                        return TallyCode.Outlyer;
                    case '+':
                        return TallyCode.Candidat;
                    case '#':
                        return TallyCode.Selected;
                    case '*':
                        return TallyCode.SysPeer;
                    case 'o':
                        return TallyCode.PpsPeer;
                    default:
                        throw new ApplicationException("Tally code not known.");
                }
            }
        }

        /// <summary>
        /// Gets the tally char.
        /// </summary>
        /// <remarks>
        /// tally char representing the tally code shows the fate of each association
        /// in the clock selection process.
        /// </remarks>
        /// <value>The tally char.</value>
        public char TallyChar
        {
            get { return tallyChar; }
        }

        /// <summary>
        /// Gets the host ID of this association entry.
        /// </summary>
        /// <value>The host ID.</value>
        public int HostId
        {
            get { return hostId; }
        }

        /// <summary>
        /// Gets the address of the remote peer.
        /// </summary>
        /// <value>The address.</value>
        public string Remote
        {
            get { return remote; }
        }

        /// <summary>
        /// Gets the reference ID (0.0.0.0 if this is unknown)
        /// </summary>
        /// <value>The reference ID.</value>
        public string Refid
        {
            get { return refid ?? "0.0.0.0"; }
        }

        /// <summary>
        /// Gets the stratum of the remote peer.
        /// </summary>
        /// <value>The stratus.</value>
        public int Stratus
        {
            get { return stratus; }
        }

        /// <summary>
        /// Gets the type of the peer (local, unicast, multicast or broadcast)
        /// </summary>
        /// <value>l, u, m or b</value>
        public char PeerType
        {
            get { return peerType; }
        }

        /// <summary>
        /// Gets when the last packet was received, the polling interval, in seconds.
        /// </summary>
        /// <value>The last poll in seconds.</value>
        public int LastPoll
        {
            get { return lastPoll; }
        }

        /// <summary>
        /// Gets the the polling interval in seconds.
        /// </summary>
        /// <value>The polling interval.</value>
        public int Poll
        {
            get { return poll; }
        }

        /// <summary>
        /// Gets the reachability register in octal.
        /// </summary>
        /// <value>The reachability register.</value>
        public int Reach
        {
            get { return reach; }
        }

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

        /// <summary>
        /// Gets the offset of the peer in milliseconds.
        /// </summary>
        /// <value>The offset.</value>
        public double Offset
        {
            get { return offset; }
        }

        /// <summary>
        /// Gets the dispersion of the peer in milliseconds.
        /// </summary>
        /// <value>The dispersion.</value>
        public double Jitter
        {
            get { return jitter; }
        }
    }
}