using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using skyscraper8.Skyscraper.Plugins;

namespace DomainObjects
{
	internal class Logger
	{
		private static Logger _instance;
		public static Logger Instance
		{
			get
			{
				if(_instance == null)
					_instance = new Logger();
				return _instance;
			}
		}

		public static string ProtocolIndent
		{
			get
			{
				return new string(' ', indentation);
			}
		}

		private class EPGCollector
		{

		}

		private PluginLogger _realLogger;

		private Logger()
		{
			_realLogger = PluginLogManager.GetLogger(typeof(EPGCollector));
		}

		public void Write(string s)
		{
			_realLogger.Log(PluginLogLevel.All, s);
		}

		/// <summary>
		/// Write a log separator line.
		/// </summary>
		/// <param name="identity">The text of the separator.</param>
		public void WriteSeparator(string identity)
		{
			Write("");
			Write("============================ " + identity + " ==============================");
			Write("");
		}

		private static int indentation;
		public static void IncrementProtocolIndent()
		{
			indentation++;
		}

		public static void DecrementProtocolIndent()
		{
			indentation--;
			if (indentation <= -1)
			{
				indentation = 0;
			}
		}

		private class EPGCollectorProtocolLogger
		{

		}

		private static Logger _protocolLogger;
		public static Logger ProtocolLogger
		{
			get
			{
				if (_protocolLogger == null)
				{
					_protocolLogger = new Logger();
					_protocolLogger._realLogger = PluginLogManager.GetLogger(typeof(EPGCollectorProtocolLogger));
				}

				return _protocolLogger;
			}
		}

		public void Dump(string identity, byte[] buffer, int length)
		{
			Dump(identity, buffer, 0, length);
		}

		public void Dump(string identity, byte[] buffer, int offset, int length)
		{
			Write("============================ " + identity + " starting ==============================");

			StringBuilder loggerLineHex = new StringBuilder("0000 ");
			StringBuilder loggerLineChar = new StringBuilder();
			int lineIndex = 0;

			for (int index = 0; index < length; index++)
			{
				if (lineIndex == 16)
				{
					Write(loggerLineHex.ToString() + "  " + loggerLineChar.ToString());
					loggerLineHex.Remove(0, loggerLineHex.Length);
					loggerLineChar.Remove(0, loggerLineChar.Length);
					loggerLineHex.Append(index.ToString("0000 "));
					lineIndex = 0;
				}

				loggerLineHex.Append(getHex(buffer[index + offset] >> 4));
				loggerLineHex.Append(getHex(buffer[index + offset] & 0x0f));
				loggerLineHex.Append(' ');

				if (buffer[index + offset] > ' ' - 1 && buffer[index + offset] < 0x7f)
					loggerLineChar.Append((char)buffer[index + offset]);
				else
					loggerLineChar.Append('.');

				lineIndex++;
			}

			if (loggerLineHex.Length != 0)
				Write(loggerLineHex.ToString() + "  " + loggerLineChar.ToString());

			Write("============================ " + identity + " ending ==============================");
		}

		private char getHex(int dataChar)
		{
			if (dataChar < 10)
				return ((char)('0' + dataChar));

			return ((char)('a' + dataChar - 10));
		}
	}
}
