//
// BootstrapPeerInfoRender.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.Globalization;
using System.Net;
using System.Text;
using Ntp.Analyzer.Localize;
using Ntp.Analyzer.Objects;

namespace Ntp.Analyzer.Render.Peer
{
    public sealed class BootstrapPeerInfoRender : HtmlObjectRender
    {
        public BootstrapPeerInfoRender(
            string webPath,
            TimeServer server,
            DateTime now,
            DateTime next,
            string name,
            string ip)
            : base(webPath)
        {
            this.server = server;
            this.now = now;
            this.next = next;
            this.name = name;
            this.ip = ip;
        }

        private readonly string ip;
        private readonly string name;
        private readonly DateTime next;
        private readonly DateTime now;
        private readonly TimeServer server;

        private string Name
        {
            get
            {
                if (server == null || String.IsNullOrEmpty(server.Name))
                    return name;

                return server.Name;
            }
        }

        private string StratumText
        {
            get
            {
                if (server == null || server.Stratum == 0)
                    return null;

                switch (server.Stratum)
                {
                    case 1:
                        return PageText.Stratum1;
                    case 2:
                        return PageText.Stratum2;
                    case 3:
                        return PageText.Stratum3;
                    case 4:
                        return PageText.Stratum4;
                    case 5:
                        return PageText.Stratum5;
                    case 6:
                        return PageText.Stratum6;
                    case 7:
                        return PageText.Stratum7;
                    default:
                        return null;
                }
            }
        }

        public string Time
        {
            get
            {
                return String.Concat(
                    now.ToLongDateString(),
                    " ",
                    TimeZoneInfo.Local.IsDaylightSavingTime(now)
                        ? TimeZoneInfo.Local.DaylightName
                        : TimeZoneInfo.Local.StandardName,
                    " ",
                    now.ToLongTimeString()
                    );
            }
        }

        public string Location
        {
            get
            {
                if (server == null || String.IsNullOrEmpty(server.Location))
                    return PageText.HtmlBlank;

                return server.Location;
            }
        }

        private string ServerSync
        {
            get
            {
                if (server == null || String.IsNullOrEmpty(server.Server))
                    return PageText.HtmlBlank;

                return server.Server;
            }
        }

        private string IpV4Address
        {
            get
            {
                if (server == null || server.Address == null || server.Address == IPAddress.None)
                    return PageText.HtmlBlank;

                return server.Address.ToString();
            }
        }

        private string ServerAccess
        {
            get
            {
                if (server == null || String.IsNullOrEmpty(server.AccessPolicy))
                    return PageText.HtmlBlank;

                switch (server.AccessPolicy)
                {
                    case ConfigurationKeyword.NtpAccessPolicyOpen:
                        return PageText.AccessPolicyOpen;
                    default:
                        return PageText.AccessPolicyUnknown;
                }
            }
        }

        private string IpV6Address
        {
            get
            {
                if (server == null || String.IsNullOrEmpty(server.V6Address))
                    return PageText.HtmlBlank;

                return server.V6Address;
            }
        }

        private string ServiceArea
        {
            get
            {
                if (server == null || String.IsNullOrEmpty(server.ServiceArea))
                    return PageText.HtmlBlank;

                string description = server.ServiceArea.Trim();
                string firstLetter = description.Substring(0, 1);
                string rest = description.Substring(1);

                return firstLetter.ToUpper() + rest;
            }
        }

        private string PoolMemberStatus
        {
            get
            {
                if (server == null)
                    return PageText.HtmlBlank;

                return server.IsPoolMember
                    ? PageText.PoolMemberYes
                    : PageText.PoolMemberNo;
            }
        }

        private string PoolMemberLink
        {
            get
            {
                if (server == null || !server.IsPoolMember)
                    return null;

                string address =
                    (server.Address == null || server.Address == IPAddress.None)
                        ? ip
                        : server.Address.ToString();

                var builder = new StringBuilder();
                builder.Append(@"<a class=""btn btn-lg btn-primary"" href=""");
                builder.Append(PageText.PoolMemberLink);
                builder.Append(address);
                builder.Append(@""">View info on pool.ntp.org &raquo;</a>");
                return builder.ToString();
            }
        }

        private string ProviderLink
        {
            get
            {
                if (server == null || String.IsNullOrEmpty(server.ProviderPage))
                    return null;

                var builder = new StringBuilder();
                builder.Append(@"<a class=""btn btn-lg btn-primary"" href=""");
                builder.Append(server.ProviderUrl);
                builder.Append(@""">View info on ");
                builder.Append(server.ProviderPage);
                builder.Append("  &raquo;</a>");
                return builder.ToString();
            }
        }

        public override string RenderHead()
        {
            var builder = new StringBuilder();

            builder.AppendLine(@"  <div class=""container"">");
            builder.AppendLine(@"    <div class=""jumbotron"">");

            builder.Append("      <h1>");
            builder.Append(Name);
            builder.Append("</h1>");

            if (StratumText != null)
            {
                builder.Append("&nbsp;&nbsp;&nbsp;&nbsp;<h3>Stratum ");
                builder.Append(StratumText);
                builder.AppendLine(" server</h3>");
            }

            builder.Append("      <p>");
            builder.Append(Time);
            builder.Append(". ");
            builder.Append("Next refresh scheduled at ");
            builder.Append(next.ToShortTimeString());
            builder.AppendLine(".</p>");

            return builder.ToString();
        }

        public override string Render()
        {
            var builder = new StringBuilder();

            builder.AppendLine(@"      <div class=""row"">");
            builder.AppendLine(@"        <div class=""col-md-2""><b>Server location</b></div>");
            builder.Append(@"        <div class=""col-md-10"">");
            builder.Append(Location);
            builder.AppendLine("</div>");
            builder.AppendLine(@"      </div>");

            builder.AppendLine(@"      <div class=""row"">");
            builder.AppendLine(@"        <div class=""col-md-2""><b>Synchronization</b></div>");
            builder.Append(@"        <div class=""col-md-10"">");
            builder.Append(ServerSync);
            builder.AppendLine("</div>");
            builder.AppendLine(@"      </div>");

            builder.AppendLine(@"      <div class=""row"">");
            builder.AppendLine(@"        <div class=""col-md-2""><b>IP address</b></div>");
            builder.Append(@"        <div class=""col-md-4"">");
            builder.Append(IpV4Address);
            builder.AppendLine("</div>");

            builder.AppendLine(@"        <div class=""col-md-2""><b>Access policy</b></div>");
            builder.Append(@"        <div class=""col-md-4"">");
            builder.Append(ServerAccess);
            builder.AppendLine("</div>");
            builder.AppendLine(@"      </div>");

            builder.AppendLine(@"      <div class=""row"">");
            builder.AppendLine(@"        <div class=""col-md-2""><b>IPv6 address</b></div>");
            builder.Append(@"        <div class=""col-md-4"">");
            builder.Append(IpV6Address);
            builder.AppendLine(@"</div>");

            builder.AppendLine(@"        <div class=""col-md-2""><b>Service area</b></div>");
            builder.Append(@"        <div class=""col-md-4"">");
            builder.Append(ServiceArea);
            builder.AppendLine(@"</div>");
            builder.AppendLine(@"      </div>");

            builder.AppendLine(@"      <div class=""row"">");
            builder.AppendLine(@"        <div class=""col-md-2""><b>Pool member</b></div>");
            builder.Append(@"        <div class=""col-md-4"">");
            builder.Append(PoolMemberStatus);
            builder.AppendLine(@"</div>");

            builder.AppendLine(@"        <div class=""col-md-2""><b>Description</b></div>");
            builder.Append(@"        <div class=""col-md-4"">");
            builder.Append(server != null ? server.Updated.ToLongDateString() : PageText.HtmlBlank);
            builder.AppendLine(@"</div>");
            builder.AppendLine(@"      </div>");

            builder.AppendLine(@"      <p class=""pool"">");

            if (PoolMemberLink != null)
            {
                builder.Append(@"        ");
                builder.Append(PoolMemberLink);
                builder.AppendLine(@"&nbsp;&nbsp;&nbsp;");
            }

            if (ProviderLink != null)
            {
                builder.Append(@"        ");
                builder.Append(ProviderLink);
                builder.AppendLine(@"&nbsp;&nbsp;&nbsp;");
            }

            if (server != null && server.Id <= 10000)
            {
                builder.Append(@"        ");
                builder.Append(
                    @"<a class=""btn btn-lg btn-primary"" href=""http://support.ntp.org/bin/view/Servers/PublicTimeServer");
                builder.Append(server.Id.ToString(CultureInfo.InvariantCulture).PadLeft(6, '0'));
                builder.AppendLine(@""">View info on support.ntp.org &raquo;</a>");
            }

            builder.AppendLine(@"      </p>");

            return builder.ToString();
        }

        public override string RenderFooter()
        {
            var builder = new StringBuilder();

            builder.AppendLine("    </div>");
            builder.AppendLine("  </div>");

            return builder.ToString();
        }
    }
}