//
// TimeServer.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.Net;

namespace Ntp.Analyzer.Objects
{
    public sealed class TimeServer : PersistentObject
    {
        private readonly string accessDetails;
        private readonly string accessPolicy;
        private readonly IPAddress address;
        private readonly string autoKey;
        private readonly string contact;
        private readonly string country;
        private readonly string displayLocation;
        private readonly string geo;
        private readonly string location;
        private readonly string name;
        private readonly bool? notification;
        private readonly string organization;
        private readonly bool? poolMember;
        private readonly string providerPage;
        private readonly string providerUrl;
        private readonly string server;
        private readonly string serviceArea;
        private readonly int stratum;
        private readonly string symKey;
        private readonly string symUrl;
        private readonly DateTime updated;
        private readonly bool? useDns;
        private readonly string v6Address;

        public TimeServer(string name, IPAddress address, int orgId)
        {
            this.name = name;
            this.address = address;
            SetId(orgId);
        }

        public TimeServer(
            int id, int stratum, string country,
            string name, IPAddress address, string v6Address,
            bool? useDns, bool? poolMember, string location,
            string displayLocation, string organization, string geo, string server,
            string serviceArea, string accessDetails, string accessPolicy,
            bool? notification, string autoKey, string symKey, string symUrl,
            string contact, string providerPage, string providerUrl, DateTime updated)
            : base(id)
        {
            this.stratum = stratum;
            this.country = country;
            this.name = name;
            this.address = address;
            this.v6Address = Scrub(v6Address);
            this.useDns = useDns;
            this.poolMember = poolMember;
            this.location = location;
            this.displayLocation = displayLocation;
            this.organization = Scrub(organization);
            this.geo = Scrub(geo);
            this.location = location;
            this.server = server;
            this.serviceArea = serviceArea;
            this.accessDetails = accessDetails;
            this.accessPolicy = Scrub(accessPolicy);
            this.notification = notification;
            this.autoKey = Scrub(autoKey);
            this.symKey = Scrub(symKey);
            this.symUrl = Scrub(symUrl);
            this.contact = contact;
            this.providerPage = providerPage;
            this.providerUrl = providerUrl;
            this.updated = updated;
        }

        public int Stratum
        {
            get { return stratum; }
        }

        public string Country
        {
            get { return country; }
        }

        public string Name
        {
            get { return name; }
        }

        public IPAddress Address
        {
            get { return address; }
        }

        public string V6Address
        {
            get { return v6Address; }
        }

        public bool ShouldUseDns
        {
            get { return useDns ?? false; }
        }

        public bool IsPoolMember
        {
            get { return poolMember ?? false; }
        }

        public string Location
        {
            get { return location; }
        }

        public string DisplayLocation
        {
            get { return displayLocation; }
        }

        public string Organization
        {
            get { return organization; }
        }

        public string Geo
        {
            get { return geo; }
        }

        public string Server
        {
            get { return server; }
        }

        public string ServiceArea
        {
            get { return serviceArea; }
        }

        public string AccessPolicy
        {
            get { return accessPolicy; }
        }

        public string AccessDetails
        {
            get { return accessDetails; }
        }

        public bool ShouldNotify
        {
            get { return notification ?? false; }
        }

        public string AutoKey
        {
            get { return autoKey; }
        }

        public string SymKey
        {
            get { return symKey; }
        }

        public string SymUrl
        {
            get { return symUrl; }
        }

        public string Contact
        {
            get { return contact; }
        }

        public string ProviderPage
        {
            get { return providerPage; }
        }

        public string ProviderUrl
        {
            get { return providerUrl; }
        }

        public DateTime Updated
        {
            get { return updated; }
        }

        private string Scrub(string input)
        {
            if (input == null || input.Trim() == String.Empty)
                return null;

            return input.Trim();
        }
    }
}