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

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

		static Random m_Random = new Random();

		public static void Init()
		{
			m_Data.Add("account", "");
			m_Data.Add("sub-account", "");
			m_Data.Add("bid-count", 5);
			m_Data.Add("trailing-rate", 1.0f);
			m_Data.Add("trailing-count", 2);

			m_Data.Add("list-size", 20);
			m_Data.Add("fast-sd", 0.75);
			m_Data.Add("fast-up", 0.05);
			m_Data.Add("fast-down", 0.05);
			m_Data.Add("slow-sd", 0.2);
			m_Data.Add("slow-up", 0.4);
			m_Data.Add("slow-down", 0.4);
			m_Data.Add("time-limit", 5);
			m_Data.Add("time-down", 0.02);
			m_Data.Add("ignore-price", 50000);
			m_Data.Add("mock-trading", false);

			Load();
		}

		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(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 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 void SetAccount(string strAccount, string strAccountSub)
		{
			if(strAccount != null)
				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 void SetBidCount(int iCount)
		{
			m_Data["bid-count"] = iCount;
			Save();
		}

		public static int GetBidCount()
		{
			return (int)m_Data["bid-count"];
		}

		public static void SetTrailing(float fTrailingRate, int iTrailingCnt)
		{
			m_Data["trailing-rate"] = fTrailingRate;
			m_Data["trailing-count"] = iTrailingCnt;
			Save();
		}

		public static float GetTrailingRate()
		{
			return (float)m_Data["trailing-rate"];
		}

		public static int GetTrailingCnt()
		{
			return (int)m_Data["trailing-count"];
		}

		public static void SetVolatility(int iListSize, 
			double dFastSD, double dFastUp, double dFastDown, 
			double dSlowSD, double dSlowUp, double dSlowDown, 
			int iTimeLimit, double dTimeDown,
			int iIgnorePrice)
		{
			m_Data["list-size"] = iListSize;
			m_Data["fast-sd"] = dFastSD;
			m_Data["fast-up"] = dFastUp;
			m_Data["fast-down"] = dFastDown;
			m_Data["slow-sd"] = dSlowSD;
			m_Data["slow-up"] = dSlowUp;
			m_Data["slow-down"] = dSlowDown;
			m_Data["time-limit"] = iTimeLimit;
			m_Data["time-down"] = dTimeDown;
			m_Data["ignore-price"] = iIgnorePrice;
			Save();
		}

		public static int GetListSize() { return (int)m_Data["list-size"]; }
		public static double GetFastSD() { return (double)m_Data["fast-sd"]; }
		public static double GetFastUp() { return (double)m_Data["fast-up"]; }
		public static double GetFastDown() { return (double)m_Data["fast-down"]; }
		public static double GetSlowSD() { return (double)m_Data["slow-sd"]; }
		public static double GetSlowUp() { return (double)m_Data["slow-up"]; }
		public static double GetSlowDown() { return (double)m_Data["slow-down"]; }
		public static int GetTimeLimit() { return (int)m_Data["time-limit"]; }
		public static double GetTimeDown() { return (double)m_Data["time-down"]; }
		public static int GetIgnorePrice() { return (int)m_Data["ignore-price"]; }
		public static void SetMockTrading(bool bMockTrading) { m_Data["mock-trading"] = bMockTrading; Save(); }
		public static bool GetMockTrading() { return (bool)m_Data["mock-trading"]; }

		public static Dictionary<string, object> GetAllConfig()
		{
			return m_Data;
		}
	}
}
