using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text.RegularExpressions;

namespace VICrawlerNS
{
	public class CodeList
	{
		public enum CODE_TYPE
		{
			NONE = 0,
			ORIGINAL = NONE,
			DENIAL = 1<<0,
			DUPLICATED = 1<<1,
			MANUAL = 1<<2,
		}

		public class CODE_VALUE
		{
			public CODE_TYPE m_enType;
			public string m_strCode;
			public string m_strName;

			override public string ToString()
			{
				return string.Format("{0}:{1}", m_strCode, m_strName);
			}
		}

		CPUTILLib.CpStockCode m_StockCode = new CPUTILLib.CpStockCode();
		List<CODE_VALUE> m_CodeList = new List<CODE_VALUE>();

		public CodeList()
		{
			MakeList();
			Test();
		}

		void Test()
		{
		}

		void MakeList()
		{
			CPUTILLib.CpCodeMgr CodeMgr = new CPUTILLib.CpCodeMgr();

			int iCnt = m_StockCode.GetCount();
			for(short i = 0; i<iCnt; i++)
			{
				CODE_VALUE Code = new CODE_VALUE();
				Code.m_enType = CODE_TYPE.ORIGINAL;
				Code.m_strCode = m_StockCode.GetData(0, i);
				Code.m_strName = m_StockCode.GetData(1, i);

				var CodeMarketKind = CodeMgr.GetStockMarketKind(Code.m_strCode);
				var CodeSectionKind = CodeMgr.GetStockSectionKind(Code.m_strCode);

				//if(Code.m_strCode[0] == 'A' &&
				//	(CodeMarketKind != CPUTILLib.CPE_MARKET_KIND.CPC_MARKET_FREEBOARD) &&
				//	(CodeSectionKind == CPUTILLib.CPE_KSE_SECTION_KIND.CPC_KSE_SECTION_KIND_NULL ||
				//	CodeSectionKind == CPUTILLib.CPE_KSE_SECTION_KIND.CPC_KSE_SECTION_KIND_ST))
				//{
				//	m_CodeList.Add(Code);
				//}

				if(Code.m_strCode[0] == 'A')
					m_CodeList.Add(Code);
			}

			m_CodeList.Sort((a, b) => b.m_strName.Length-a.m_strName.Length);
		}

		public CODE_VALUE GetCode(string strCode)
		{
			CODE_VALUE Result = m_CodeList.Find(s => s.m_strCode == strCode);
			if(Result == null)
			{
				Result = new CODE_VALUE();
				Result.m_strCode = strCode;
				Result.m_strName = "";
			}
			return Result;
		}

		public CODE_VALUE GetCodeByName(string strCodeName)
		{
			CODE_VALUE Result = m_CodeList.Find(s => s.m_strName == strCodeName);
			return Result;
		}
	}
}
