using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace NewsCrawler
{
	static class Config
	{
		static Dictionary<string, object> m_Data = new Dictionary<string, object>();

		static Random m_Random = new Random();
		static int m_iDartAPIKeyCnt = 0;
		static bool[] m_abDartAPIKeyLimit = null;

		public static void Init()
		{
			m_Data.Add("manual-price", 100000);
			m_Data.Add("buy-price", 1000000);
			m_Data.Add("ann-dart-api", true);
			m_Data.Add("ann-supply-contract", true);
			m_Data.Add("ann-supply-contract-rate", 50.0f);
			m_Data.Add("ann-revenue", true);
			m_Data.Add("ann-revenue-rate", 50.0f);
			m_Data.Add("ann-rights-issue", true);
			m_Data.Add("ann-patent", true);
			m_Data.Add("ann-patent-search-string", new Regex("(미국|중국)"));

			Load();
			Migration();
			Save();

			int iIdx = 1;
			while(true)
			{
				if(m_Data.ContainsKey("dart-api-key"+iIdx) == false)
					break;
				iIdx++;
			}
			m_iDartAPIKeyCnt = iIdx-1;

			m_abDartAPIKeyLimit = new bool[m_iDartAPIKeyCnt];
			for(int i = 0; i<m_iDartAPIKeyCnt; i++)
				m_abDartAPIKeyLimit[i] = false;
		}

		static void Load()
		{
			string strPath = Util.GetConfigPath() + "/config.ini";
			if(File.Exists(strPath) == false)
				return;

			string[] aLines = File.ReadAllLines(strPath);
			foreach(string strLine in aLines)
			{
				if(strLine.Trim().Length <= 0)
					continue;

				string[] aTokens = strLine.Trim().Split('=');
				if(aTokens.Length < 2)
					continue;

				if(aTokens[0] == "ann-patent-search-string")
				{
					m_Data[aTokens[0]] = new Regex(aTokens[1]);
				}
				else
				{
					if(m_Data.ContainsKey(aTokens[0]) == true)
						m_Data[aTokens[0]] = Convert.ChangeType(aTokens[1], m_Data[aTokens[0]].GetType());
					else
						m_Data.Add(aTokens[0], aTokens[1]);
				}
			}
		}

		static void Migration()
		{
			if(m_Data.ContainsKey("supply-contract-rate"))
			{
				float fValue;
				float.TryParse((string)m_Data["supply-contract-rate"], out fValue);
				m_Data["ann-supply-contract-rate"] = fValue;
				m_Data.Remove("supply-contract-rate");
			}

			if(m_Data.ContainsKey("revenue-rate"))
			{
				float fValue;
				float.TryParse((string)m_Data["revenue-rate"], out fValue);
				m_Data["ann-revenue-rate"] = fValue;
				m_Data.Remove("revenue-rate");
			}
		}

		static void Save()
		{
			string strContents = "";
			foreach(KeyValuePair<string, object> pair in m_Data)
				strContents += pair.Key + "=" + pair.Value.ToString() + Environment.NewLine;

			string strPath = Util.GetConfigPath()+"/config.ini";
			File.WriteAllText(strPath, strContents, new UTF8Encoding(true));
		}

		public static int GetManualPrice()
		{
			return (int)m_Data["manual-price"];
		}

		public static void SetManualPrice(int iPrice)
		{
			m_Data["manual-price"] = iPrice;
			Save();
		}

		public static int GetBuyPrice()
		{
			return (int)m_Data["buy-price"];
		}

		public static void SetBuyPrice(int iPrice)
		{
			m_Data["buy-price"] = iPrice;
			Save();
		}

		public static void SetAccount(string strAccount, string strAccountSub)
		{
			m_Data["account"] = strAccount;
			m_Data["sub-account"] = strAccountSub;
			Save();
		}

		public static string GetAccount()
		{
			return (string)m_Data["account"];
		}

		public static string GetSubAccount()
		{
			return (string)m_Data["sub-account"];
		}

		public static string GetDartAPIKey()
		{
			if(m_iDartAPIKeyCnt <= 0)
				return "";

			if(m_abDartAPIKeyLimit.All(s => s == true) == true)
				return "";

			int iNum = -1;
			while(iNum < 0 || m_abDartAPIKeyLimit[iNum-1] == true)
				iNum = m_Random.Next(0, 10000)%m_iDartAPIKeyCnt + 1;

			return (string)m_Data["dart-api-key"+iNum];
		}

		public static void SetDartAPIKeyLimit(string strKey)
		{
			for(int i = 0; i<m_iDartAPIKeyCnt; i++)
			{
				if((string)m_Data["dart-api-key"+(i+1).ToString()] == strKey)
				{
					m_abDartAPIKeyLimit[i] = true;
					break;
				}
			}
		}

		public static void SetAnnouncement(bool bDartAPI,
			bool bSupply, bool bRevenue, bool bRightsIssue, bool bPatent,
			float fSupplyContractRate, float fRevenueRate, string strPatent)
		{
			m_Data["ann-dart-api"] = bDartAPI;

			m_Data["ann-supply-contract"] = bSupply;
			m_Data["ann-supply-contract-rate"] = fSupplyContractRate;

			m_Data["ann-revenue"] = bRevenue;
			m_Data["ann-revenue-rate"] = fRevenueRate;

			m_Data["ann-rights-issue"] = bRightsIssue;

			m_Data["ann-patent"] = bPatent;
			m_Data["ann-patent-search-string"] = strPatent;

			Save();
		}

		#region Announcement
		public static bool CheckDartAPI()
		{
			return (bool)m_Data["ann-dart-api"];
		}

		public static bool CheckSupplyContract()
		{
			return (bool)m_Data["ann-supply-contract"];
		}

		public static float GetSupplyContractRate()
		{
			return (float)m_Data["ann-supply-contract-rate"];
		}

		public static bool CheckRevenue()
		{
			return (bool)m_Data["ann-revenue"];
		}

		public static float GetRevenueRate()
		{
			return (float)m_Data["ann-revenue-rate"];
		}

		public static bool CheckRightsIssue()
		{
			return (bool)m_Data["ann-ann-rights-issue"];
		}

		public static bool CheckPatent()
		{
			return (bool)m_Data["ann-patent"];
		}

		public static Regex GetPatentSearchString()
		{
			return (Regex)m_Data["ann-patent-search-string"];
		}
		#endregion
	}
}
