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

namespace Ntp.Analyzer.Render.Peer
{
    public sealed class BootstrapPeerInfoRender : HtmlObjectRender
    {
        private readonly DateTime next;
        private readonly DateTime now;
        private readonly TimeServer server;

        public BootstrapPeerInfoRender(string webPath, TimeServer server, DateTime now, DateTime next)
            : base(webPath)
        {
            this.server = server;
            this.now = now;
            this.next = next;
        }

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

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

            builder.Append("      <h1>");
            builder.Append(server.Name);
            builder.Append("</h1>&nbsp;&nbsp;&nbsp;&nbsp;<h3>Stratum ");
            builder.Append(GetStratumText());
            builder.AppendLine(" server</h3>");

            builder.Append("      <p>");
            builder.Append(now.ToLongDateString() + " CEST ");
            builder.Append(now.ToLongTimeString() + ". ");
            builder.Append("Next refresh scheduled at ");
            builder.Append(next.ToShortTimeString());
            builder.AppendLine(".</p>");

            return builder.ToString();
        }

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

            builder.AppendLine(@"      <div class=""row"">");
            builder.AppendLine(@"        <div class=""col-md-2""><b>Server location</b></div>");
            builder.AppendLine(String.Format(@"        <div class=""col-md-10"">{0}</div>", server.Location));
            builder.AppendLine(@"      </div>");

            builder.AppendLine(@"      <div class=""row"">");
            builder.AppendLine(@"        <div class=""col-md-2""><b>Synchronization</b></div>");
            builder.AppendFormat(@"        <div class=""col-md-10"">{0}</div>", GetServerSync());
            builder.AppendLine(@"      </div>");

            builder.AppendLine(@"      <div class=""row"">");
            builder.AppendLine(@"        <div class=""col-md-2""><b>IP address</b></div>");
            builder.AppendLine(String.Format(@"        <div class=""col-md-4"">{0}</div>", server.Address));
            builder.AppendLine(@"        <div class=""col-md-2""><b>Access policy</b></div>");
            builder.AppendLine(String.Format(@"        <div class=""col-md-4"">{0}</div>", GetServerAccess()));
            builder.AppendLine(@"      </div>");

            builder.AppendLine(@"      <div class=""row"">");
            builder.AppendLine(@"        <div class=""col-md-2""><b>IPv6 address</b></div>");
            builder.AppendLine(String.Format(@"        <div class=""col-md-4"">{0}</div>", server.V6Address ?? "N/A"));
            builder.AppendLine(@"        <div class=""col-md-2""><b>Service area</b></div>");
            builder.AppendLine(String.Format(@"        <div class=""col-md-4"">{0}</div>", GetServiceArea()));
            builder.AppendLine(@"      </div>");

            builder.AppendLine(@"      <div class=""row"">");
            builder.AppendLine(@"        <div class=""col-md-2""><b>Pool member</b></div>");
            builder.AppendLine(String.Format(@"        <div class=""col-md-4"">{0}</div>", GetPoolMemberStatus()));
            builder.AppendLine(@"        <div class=""col-md-2""><b>Description</b></div>");
            builder.AppendLine(String.Format(@"        <div class=""col-md-4"">{0}</div>", server.Updated.ToLongDateString()));
            builder.AppendLine(@"      </div>");

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

            if (server.IsPoolMember)
            {
                builder.AppendLine(String.Format(@"        {0}&nbsp;&nbsp;&nbsp;", GetPoolMemberLink()));
            }

            if (server.ProviderPage != null)
            {
                builder.AppendLine(String.Format(@"        {0}&nbsp;&nbsp;&nbsp;", GetProviderLink()));
            }

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

            return builder.ToString();
        }

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

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

            return builder.ToString();
        }

        #region Translators

        private string GetStratumText()
        {
            switch (server.Stratum)
            {
                case 1:
                    return "one";
                case 2:
                    return "two";
                case 3:
                    return "three";
                case 4:
                    return "four";
                case 5:
                    return "five";
                case 6:
                    return "six";
                case 7:
                    return "seven";
                default:
                    return "Unknown";
            }
        }

        private string GetServerSync()
        {
            return server.Server.Trim() != String.Empty ? server.Server : "&nbsp;";
        }

        private string GetServerAccess()
        {
            switch (server.AccessPolicy)
            {
                case "OpenAccess":
                    return "Open access";
                default:
                    return "Unknown";
            }
        }

        private string GetServiceArea()
        {
            string description = server.ServiceArea.Trim();

            if (description == String.Empty)
                return "&nbsp;";

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

            return firstLetter.ToUpper() + rest;
        }

        private string GetPoolMemberStatus()
        {
            return server.IsPoolMember
                       ? "Server is pool member"
                       : "Server is not a pool member";
        }

        private string GetPoolMemberLink()
        {
            if (!server.IsPoolMember)
                return "Server is not a pool member";

            StringBuilder builder = new StringBuilder();
            builder.Append(@"<a class=""btn btn-lg btn-primary"" href=""http://www.pool.ntp.org/scores/");
            builder.Append(server.Address);
            builder.Append(@""">View info on pool.ntp.org &raquo;</a>");
            return builder.ToString();
        }

        private string GetProviderLink()
        {
            if (server.ProviderPage == null)
                return "Not found";

            StringBuilder 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();
        }

        #endregion
    }
}