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

namespace friction
{
	public static class Config
	{
		static string m_strPath = "";

		static Config()
		{
			
		}

		public static void Init()
		{
			string strPrjName = System.Reflection.Assembly.GetEntryAssembly().GetName().Name;
			string strFolder = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), strPrjName);
			m_strPath = strFolder + "\\config.ini";
			if (!Directory.Exists(strFolder))
				Directory.CreateDirectory(strFolder);

			Load();
		}

		[DllImport("kernel32")]
		private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
		[DllImport("kernel32")]
		private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);

		private static void Load()
		{
			StringBuilder temp = new StringBuilder(10240);
			int iRes = GetPrivateProfileString("Option", "recent", "", temp, 10240, m_strPath);

			if(temp.Length > 0)
			{
				string[] astrList = temp.ToString().Split(new string[] { "//" }, StringSplitOptions.None);
				foreach (string strFile in astrList)
					OPTION.AddRecentFile(strFile);
			}

			iRes = GetPrivateProfileString("Option", "bound", "", temp, 10240, m_strPath);
			if(temp.Length > 0)
			{
				string[] astrBound = temp.ToString().Split(',');

				int iLeft, iWidth, iTop, iHeight;

				int.TryParse(astrBound[0], out iLeft);
				int.TryParse(astrBound[1], out iWidth);
				int.TryParse(astrBound[2], out iTop);
				int.TryParse(astrBound[3], out iHeight);

				OPTION.WindowBound = new Rectangle(iLeft, iTop, iWidth, iHeight);
			}

			iRes = GetPrivateProfileString("Option", "checked-column", "", temp, 10240, m_strPath);
			if (temp.Length > 0)
			{
				string[] astrBound = temp.ToString().Split(',');
				OPTION.CheckedColumns = astrBound.ToList();
			}

			iRes = GetPrivateProfileString("Option", "unchecked-column", "", temp, 10240, m_strPath);
			if (temp.Length > 0)
			{
				string[] astrBound = temp.ToString().Split(',');
				OPTION.UncheckedColumns = astrBound.ToList();
			}
		}

		public static void Save()
		{
			WritePrivateProfileString("Option", "recent", OPTION.GetRecentAll(), m_strPath);
			WritePrivateProfileString("Option", "bound", OPTION.WindowBoundStr(), m_strPath);
			WritePrivateProfileString("Option", "checked-column", OPTION.CheckedColumnsStr(), m_strPath);
			WritePrivateProfileString("Option", "unchecked-column", OPTION.UncheckedColumnsStr(), m_strPath);
		}


		public struct COLUMN_NAME
		{
			public static string SPRING = "Material spring";
			public static string TABLE = "material 2 table";
			public static string RPN = "Risk priority number";
			public static string FORCE = "normal force";
			public static string TEMP = "Temperature";
			public static string HUMIDITY = "Relative humidity";
			public static string VELOCITY = "Relative velocity";
		}


		public static class OPTION
		{
			public static List<string> m_RecentList = new List<string>();
			public static Rectangle WindowBound { set; get; }
			public static List<string> CheckedColumns { set; get; }
			public static List<string> UncheckedColumns { set; get; }

			public static void AddRecentFile(string strPath)
			{
				if(m_RecentList.Find(s => s==strPath) != null)
					m_RecentList.Remove(strPath);

				m_RecentList.Add(strPath);
			}

			public static string GetRecentAll()
			{
				return string.Join("//", m_RecentList);
			}

			public static string WindowBoundStr()
			{
				return string.Format("{0}, {1}, {2}, {3}", WindowBound.Left, WindowBound.Width, WindowBound.Top, WindowBound.Height);
			}

			public static string CheckedColumnsStr()
			{
				string strResult = "";
				if(CheckedColumns != null)
				{
					foreach (var item in CheckedColumns)
					{
						if (strResult.Length > 0)
							strResult += ",";
						strResult += item;
					}
				}

				return strResult;
			}

			public static string UncheckedColumnsStr()
			{
				string strResult = "";

				if(UncheckedColumns != null)
				{
					foreach (var item in UncheckedColumns)
					{
						if (strResult.Length > 0)
							strResult += ",";
						strResult += item;
					}
				}

				return strResult;
			}
		}


		public static class ANALYSIS
		{
			public enum RISK
			{
				NO=3,
				POTENTIAL=5,
				HIGH=10
			}

			public enum DEPENDANCY
			{
				NO,
				POTENTIAL,
				HIGH,
				NOT_ENNOUGH_DATA
			}

			public static RISK GetRisk(float fAvg)
			{
				if (fAvg <= 3)
					return RISK.NO;
				else if (fAvg <= 5)
					return RISK.POTENTIAL;
				else
					return RISK.HIGH;
			}

			public static DEPENDANCY GetDependancy(float fStdev, int iTestCnt)
			{
				if (iTestCnt <= 3)
					return DEPENDANCY.NOT_ENNOUGH_DATA;
				else if (fStdev < 3.5f)
					return DEPENDANCY.NO;
				else if (fStdev < 5.5f)
					return DEPENDANCY.POTENTIAL;
				else
					return DEPENDANCY.HIGH;
			}

		}

	}
}
