using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AutoSellerNS
{
	static class Util
	{
		public enum LOG_TYPE
		{
			DEBUG,
			ERROR,
			VERBOSE,
			BUY,
			BUY_CONCLUSION,
			SELL,
			SELL_CONCLUSION,
		}
		static string m_strLogFile = null;
		static RichTextBox m_LogBox = null;

		static object m_LogLock = new object();

		public static void SetLogView(RichTextBox logBox)
		{
			m_LogBox = logBox;
		}

		public static void Clear()
		{
			m_LogBox = null;
		}

		static void InsertLog(RichTextBox LogBox, LOG_TYPE enType, string strLog)
		{
			if(IsDebugging() == false && enType == LOG_TYPE.DEBUG)
				return;

			Action update = delegate {
				Color LogColor;
				Color LogBackColor = LogBox.BackColor;
				switch (enType)
				{
					case LOG_TYPE.DEBUG:
						LogColor = Color.Gray;
						break;
					case LOG_TYPE.ERROR:
						LogColor = Color.DarkRed;
						break;
					case LOG_TYPE.VERBOSE:
						LogColor = Color.Black;
						break;
					case LOG_TYPE.SELL:
						LogColor = Color.FromArgb(0, 104, 232);
						break;
					case LOG_TYPE.SELL_CONCLUSION:
						LogColor = Color.Black;
						LogBackColor = Color.FromArgb(127, 179, 243);
						break;
					case LOG_TYPE.BUY:
						LogColor = Color.FromArgb(244, 45, 45);
						break;
					case LOG_TYPE.BUY_CONCLUSION:
						LogColor = Color.Black;
						LogBackColor = Color.FromArgb(249, 150, 150);
						break;
					default:
						LogColor = Color.Black;
						break;
				}

				LogBox.SelectionStart = LogBox.TextLength;
				LogBox.SelectionLength = 0;
				LogBox.SelectionColor = LogColor;
				LogBox.SelectionBackColor = LogBackColor;

				LogBox.AppendText(strLog);

				LogBox.SelectionColor = LogBox.ForeColor;
				LogBox.SelectionBackColor = LogBox.BackColor;

				LogBox.SelectionStart = LogBox.TextLength;
				LogBox.ScrollToCaret();
			};

			if (LogBox.InvokeRequired)
				LogBox.BeginInvoke(update);
			else
				update();
		}

		public static void Log(LOG_TYPE enType, string strLog)
		{
			if(Directory.Exists(GetLogPath()) == false)
				Directory.CreateDirectory(GetLogPath());

			if(m_strLogFile == null)
			{
				string strToday = DateTime.Now.ToString("yyyy-MM-dd");
				m_strLogFile = GetLogPath()+"/AutoSellerLog-"+strToday+".txt";
			}

			string strLogLevel = "["+enType+"] ";
			string strTime = DateTime.Now.ToString("[HH:mm:ss:fff] ");
			string strMessage = strTime+strLogLevel+strLog;

			lock(m_LogLock)
				File.AppendAllText(m_strLogFile, strMessage+Environment.NewLine, new UTF8Encoding(true));
			if(m_LogBox != null)
				InsertLog(m_LogBox, enType, strMessage+Environment.NewLine);

			Console.WriteLine(strMessage);
		}

		public static bool IsDebugging()
		{
			return Debugger.IsAttached;
		}

		public static string GetConfigPath()
		{
			string strPath = "";
			if(IsDebugging())
				strPath = Path.GetDirectoryName(Path.GetDirectoryName(Directory.GetCurrentDirectory()));
			else
				strPath = Directory.GetCurrentDirectory();
			strPath += "/configure";

			if(Directory.Exists(strPath) == false)
				Directory.CreateDirectory(strPath);

			return strPath;
		}

		public static string GetLogPath()
		{
			string strPath = "";
			if(IsDebugging())
				strPath = Path.GetDirectoryName(Path.GetDirectoryName(Directory.GetCurrentDirectory()));
			else
				strPath = Directory.GetCurrentDirectory();
			strPath += "/log";

			return strPath;
		}

		public static string GetSimulationPath()
		{
			string strPath = "";
			if(IsDebugging())
				strPath = Path.GetDirectoryName(Path.GetDirectoryName(Directory.GetCurrentDirectory()));
			else
				strPath = Directory.GetCurrentDirectory();
			strPath += "/simulation";

			if(Directory.Exists(strPath) == false)
				Directory.CreateDirectory(strPath);

			return strPath;
		}
	}
}
