using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using AR;

namespace Project.Dialog
{
	public partial class RegExTest : Form
	{
		Regex regex = null;

		public RegExTest()
		{
			InitializeComponent();
		}

		private void RegExTest_Load(object sender, EventArgs e)
		{


		}

		private void button1_Click(object sender, EventArgs e)
		{
			bool retval = false;


			//이 데이터가 정규식에 match 가되는지 확인하.ㄴㄷ
			var ucnt = 0;
			var MatchEx = textBox1.Text.Trim();
			var lines = this.richTextBox1.Text.Replace("\r", "").Split('\n');
			foreach (var line in lines)
			{
				var vdata = line.Split(':')[1];
				if (vdata.isEmpty()) continue;


				regex = new Regex(MatchEx);    //한글 3글자
				if (regex.IsMatch(vdata))
				{
					var MatchIndex = 0;
					var GroupIndex = 0;
					var matchlist = regex.Matches(vdata);
					var match0 = matchlist[MatchIndex];
					var grpval = match0.Groups[GroupIndex].Value;
					var result = grpval.Trim();
					//if (dr.ReplaceEx.isEmpty() == false && dr.ReplaceStr.isEmpty() == false)
					//{
					//	regex = new Regex(dr.ReplaceEx);
					//	if (regex.IsMatch(result)) result = regex.Replace(result, dr.ReplaceStr);
					//}
					//ucnt += 1;
					//switch (dr.varName)
					//{
					//	case "QTY0":
					//		vdata.QTY0 = result;
					//		vdata.QTYRQ = vdata.StartsWith("RQ");
					//		break;
					//	case "SID":
					//		vdata.SID = result;
					//		break;
					//	case "LOT":
					//		vdata.VLOT = result;
					//		break;
					//	case "PART":
					//		vdata.PARTNO = result;
					//		break;
					//	case "MFGDATE":
					//		vdata.MFGDATE = result;
					//		break;
					//	case "QTY":
					//		vdata.QTY = result;
					//		break;
					//	default:
					//		ucnt -= 1;
					//		PUB.log.AddAT($"RegEx 대상 VarName을 찾을 수 없습니다. 값:{dr.varName}");
					//		break;
					//}
				}
			}

		}

		private void button2_Click(object sender, EventArgs e)
		{
			var pattern = textBox1.Text.Trim();
			try
			{
				regex = new Regex(pattern);    //한글 3글자
				if (regex.IsMatch(textBox2.Text.Trim()))
				{
					setlog("match");
				}
				else setlog($"no match\npattern:{pattern}\nValue:{textBox2.Text}");
			}
			catch (Exception ex)
			{
				setlog(ex.Message);
			}
		}
		void addlog(string m)
		{
			this.rtLog.AppendText(m + "\r\n");
		}
		void setlog(string m)
		{
			this.rtLog.Text = m;
		}
	}
}
