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 VICrawlerNS
{
	static class Util
	{
		public enum LOG_TYPE
		{
			DEBUG,
			ERROR,
			VERVOSE,
			BUY,
			SELL,
		}
		static string m_strLogFile = null;
		static RichTextBox m_LogBox = null;

		delegate void InsertLogDelegate(RichTextBox LogBox, LOG_TYPE enType, string strLog);
		static InsertLogDelegate m_InsertLogDelegate = new InsertLogDelegate(InsertLog);

		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(LogBox.InvokeRequired)
			{
				LogBox.Invoke(m_InsertLogDelegate, LogBox, enType, strLog);
			}
			else
			{
				Color LogColor;
				switch(enType)
				{
					case LOG_TYPE.DEBUG:
						LogColor = Color.Gray;
						break;
					case LOG_TYPE.ERROR:
						LogColor = Color.DarkRed;
						break;
					case LOG_TYPE.VERVOSE:
						LogColor = Color.Black;
						break;
					case LOG_TYPE.SELL:
						LogColor = Color.Blue;
						break;
					case LOG_TYPE.BUY:
						LogColor = Color.Red;
						break;
					default:
						LogColor = Color.Black;
						break;
				}

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

				LogBox.AppendText(strLog);

				LogBox.SelectionColor = LogBox.ForeColor;

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

		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()+"/VICrawlerLog-"+strToday+".txt";
			}

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

			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;
		}
	}
}
