//
// HostLineHtmlRender.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.Text;
using Ntp.Analyzer.Config.Page;
using Ntp.Analyzer.Objects.Live;
using Ntp.Analyzer.Objects;

namespace Ntp.Analyzer.Render.Host
{
    public sealed class HostLineRender : HtmlObjectRender
    {
        private readonly PeerPageConfiguration peerPages;
        private readonly StatusLine statusLine;

        public HostLineRender(StatusLine statusLine, PeerPageConfiguration peerPages)
            : base(peerPages.WebPath)
        {
            this.statusLine = statusLine;
            this.peerPages = peerPages;
        }

        private AssociationEntry Peer
        {
            get { return statusLine.Peer; }
        }

        private NtpConfEntry Conf
        {
            get { return statusLine.Conf; }
        }

        private TimeServer Server
        {
            get { return statusLine.Server; }
        }

        public override string RenderHead()
        {
            return "<tr>";
        }

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

            string name = Conf.Name.Substring(0, Conf.Name.Length > 18 ? 18 : Conf.Name.Length);
            string country = (Server != null ? Server.Country : String.Empty);
            string location = (Server != null ? Server.DisplayLocation : "Unknown");

            builder.Append("<td>" + Peer.TallyChar + "</td>");
            builder.Append("<td>" + name + "</td>");
            builder.Append(@"<td class=""aright"">" + Peer.Stratus + "</td>");
            builder.Append(@"<td class=""aright"">" + Peer.LastPoll + "</td>");
            builder.Append(@"<td class=""aright2"">" + Peer.Poll + "</td>");
            builder.Append(@"<td class=""aright"">" + Peer.Reach + "</td>");
            builder.Append(@"<td class=""aright"">" + Peer.Delay.ToString("0.000") + "</td>");
            builder.Append(@"<td class=""aright"">" + Peer.Offset.ToString("0.000") + "</td>");
            builder.Append(@"<td class=""aright"">" + Peer.Jitter.ToString("0.000") + "</td>");
            builder.Append(@"<td class=""aright"">" + country + "</td>");
            builder.Append(@"<td><a href=""");
            builder.Append(peerPages.GetLink(Server.Id));
            builder.Append(@""">");
            builder.Append(location);
            builder.AppendLine("</a></td>");

            return builder.ToString();
        }

        public override string RenderFooter()
        {
            return "</tr>";
        }
    }
}