using System;
using System.IO;
using System.Linq;
using System.Threading;
using System.Globalization;
using Ntp.Analyzer;
using Ntp.Analyzer.Page;
using Ntp.Analyzer.Data;
using Ntp.Analyzer.Graph;
using Ntp.Analyzer.Config;
using Ntp.Analyzer.Objects;
using Ntp.Analyzer.Statistics;

namespace Ntp.Analyzer.Web
{
	public class NtpUpate
	{
		public static void Main (string[] args)
		{
			string configFile = (args.Length > 0) ? args [0] : "/etc/ntpw.conf";

			config = new ConfigurationReader (configFile);
			config.ReadFile ();
					
			if (Settings.UpdateFrequency == 0) {
				Execute ();
				return;
			}
								
			double dfreq = Settings.UpdateFrequency;
			
			while (true) {
				DateTime next = DateTime.Now.AddMinutes (Settings.UpdateFrequency);
				
				int nextMin = Convert.ToInt32 (Math.Floor (next.Minute / dfreq) * dfreq);
				
				if (nextMin == 60)
					nextMin = 0;
				
				DateTime nextRun = new DateTime (
					next.Year, next.Month, next.Day,
					next.Hour, nextMin, 0);
					
				int wait = Convert.ToInt32 (nextRun.Subtract (DateTime.Now).TotalMilliseconds);
				
				Thread.Sleep (wait);
			
				Execute ();
			}
		}

		private static int peerCount = 0;
		private static ConfigurationReader config;

		private static void Execute ()
		{
			Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
		
			foreach (HtmlBuildConfiguration buildConfig in config.HtmlConfigurations) {
			
				// Build status objects
				StatusBuilder builder = new StatusBuilder (
					new ServerName (buildConfig.ServerName), 
					new FileInfo (Settings.ConfigDir + buildConfig.ConfigFile));
				builder.Build ();
				
				// Generate HTML pages
				HtmlStatusPageBuilder pageBuilder = new HtmlStatusPageBuilder (
					buildConfig.ServerName,
					builder.Entries.ToArray (),
					buildConfig.GraphLink,
					buildConfig.PoolLink,
					buildConfig.PeerLocation);
				
				// Save HTML
				string html = pageBuilder.Build ();
				File.WriteAllText (buildConfig.Destination, html);

			}
			
			// Generate host graphs
			foreach (HostGraphConfiguration graphConfig in config.HostGraphConfigurations) {
				HostGraph graph = new HostGraph (graphConfig);
				graph.Create ();
			}
			
			if (peerCount == 0) {
				// Generate peer graph & pages
				foreach (PeerGraphConfiguration graphConfig in config.PeerGraphConfigurations) {
					Host host = DataFace.Instance.Hosts.Single (h => h.Id == graphConfig.HostId);

					foreach (NtpConfEntry entry in DataFace.Instance.NtpConfigMapper[Settings.ConfigDir + graphConfig.ConfigFile].Entries) {
						Peer peer = DataFace.Instance.Peers.Single (p => p.OrgId == entry.OrgId.Value);
					
						PeerGraph graph = new PeerGraph (graphConfig, host, peer, false);
						graph.Create ();

						graph = new PeerGraph (graphConfig, host, peer, true);
						graph.Create ();
						
						HtmlPeerPageBuilder peerPageBuilder = new HtmlPeerPageBuilder (peer, graphConfig);
						string html = peerPageBuilder.Build ();
						File.WriteAllText (graphConfig.Destination + peer.OrgId.ToString () + ".html", html);	
					}
				}
			
				peerCount = 12 * 6;
			}
			
			peerCount--;
		}
	}
}