//
// Heartbeat.cs
//
// Author:
//       Carsten Sonne Larsen <cs@innolan.dk>
//
// Copyright (c) 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.Text;
using System.Timers;
using Ntp.Log;

namespace Ntp.Common
{
    public class Heartbeat
    {
        public Heartbeat(LogBase log, int minutes)
        {
            this.log = log;
            this.minutes = minutes;
            totalMinutes = 0;
        }

        private readonly LogBase log;
        private readonly int minutes;
        private int totalMinutes;

        public void Start()
        {
            var timer = new Timer(1000*60*minutes);
            timer.Elapsed += TimerElapsed;
            timer.AutoReset = true;
            timer.Enabled = true;

            log.WriteLine(String.Format(
                "Heartbeat started with {0} minutes interval.", minutes),
                Severity.Notice);
        }

        private void TimerElapsed(object sender, ElapsedEventArgs e)
        {
            log.WriteLine(String.Format(
                "Heartbeat: Uptime is {0}.", GetTimeString()),
                Severity.Notice);
        }

        private string GetTimeString()
        {
            totalMinutes += minutes;

            TimeSpan span = TimeSpan.FromMinutes(totalMinutes);

            var builder = new StringBuilder();

            if (span.Days != 0)
            {
                builder.Append(span.Days);
                builder.Append(" day");
            }

            if (span.Days > 1)
            {
                builder.Append("s");
            }

            if (span.Days >= 1 && span.Hours != 0 && span.Minutes != 0)
            {
                builder.Append(" ");
            }

            if (span.Hours != 0 && span.Minutes == 0)
            {
                builder.Append("and ");
            }
            else if (span.Hours != 0 && span.Minutes != 0)
            {
                builder.Append(", ");
            }

            if (span.Hours != 0)
            {
                builder.Append(span.Hours);
                builder.Append(" hour");
            }

            if (span.Hours > 1)
            {
                builder.Append("s");
            }

            if (span.Hours >= 1 && span.Minutes != 0)
            {
                builder.Append(" ");
            }

            if (span.Hours != 0 && span.Minutes != 0)
            {
                builder.Append("and ");
            }

            if (span.Minutes != 0)
            {
                builder.Append(span.Minutes);
                builder.Append(" minute");
            }

            if (span.Minutes > 1)
            {
                builder.Append("s");
            }

            return builder.ToString();
        }
    }
}