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

namespace SystemX.Product.TRA.UIControl
{
    public partial class FrmOptionStationSearch : Form
    {
        TestHistorySearchOption Option { get; set; } = new TestHistorySearchOption();

        private bool bSelectedTime_DaySearched = false;

        public enum eExclusion
        {
            Time_SelectedTest,
            Time_Day,
            Time_WholeRange
        }

        public FrmOptionStationSearch(bool bDialog = true, bool bSelectTime_Day = false)
        {
            InitializeComponent();

            bSelectedTime_DaySearched = bSelectTime_Day;

            DispData();

            if(bDialog == false)
                SetOption();
        }

        public TestHistorySearchOption GetSelectedResult()
        {
            return Option;
        }

        void DispData()
        {
            string strTypeName = "SystemX.Product.TRA.UIControl.TestHistorySearchOption";

            try
            {
                Type typeObj = Type.GetType(strTypeName);

                foreach(PropertyInfo info in typeObj.GetProperties())
                {
                    string strProp = info.Name;
                    string strValue = info.GetValue(Option, null).ToString();
                    bool bState = Convert.ToBoolean(strValue);

                    checkedListBoxControlOption.Items.Add(strProp, bState ? CheckState.Checked : CheckState.Unchecked, true);

                    if (strProp == "StepVersion" || strProp == "FileVersion")
                        checkedListBoxControlOption.Items[strProp].Enabled = false;
                }

            }
            catch(Exception ex)
            {
                MessageBox.Show("Error during option display: " + ex.Message);
            }

            if (bSelectedTime_DaySearched)
            {
                checkedListBoxControlOption.Items.Where(x => x.Value.ToString() == eExclusion.Time_SelectedTest.ToString()).FirstOrDefault().CheckState = CheckState.Checked;
                checkedListBoxControlOption.Items.Where(x => x.Value.ToString() == eExclusion.Time_WholeRange.ToString()).FirstOrDefault().CheckState = CheckState.Unchecked;
                checkedListBoxControlOption.Items.Where(x => x.Value.ToString() == eExclusion.Time_Day.ToString()).FirstOrDefault().CheckState = CheckState.Unchecked;
            }
        }

        private void FrmOptionStationSearch_Load(object sender, EventArgs e)
        {
            DispData();

        }

        private void SetOption()
        {
            string strTypeName = "SystemX.Product.TRA.UIControl.TestHistorySearchOption";

            try
            {
                if (checkedListBoxControlOption.Items.Where(x => x.Value.ToString() == eExclusion.Time_SelectedTest.ToString()).FirstOrDefault().CheckState == CheckState.Unchecked &&
                    checkedListBoxControlOption.Items.Where(x => x.Value.ToString() == eExclusion.Time_Day.ToString()).FirstOrDefault().CheckState == CheckState.Unchecked &&
                    checkedListBoxControlOption.Items.Where(x => x.Value.ToString() == eExclusion.Time_WholeRange.ToString()).FirstOrDefault().CheckState == CheckState.Unchecked)
                    checkedListBoxControlOption.Items.Where(x => x.Value.ToString() == eExclusion.Time_SelectedTest.ToString()).FirstOrDefault().CheckState = CheckState.Checked;

                Type typeObj = Type.GetType(strTypeName);

                foreach (PropertyInfo info in typeObj.GetProperties())
                {
                    string strProp = info.Name;
                    bool bChecked = checkedListBoxControlOption.Items[strProp].CheckState == CheckState.Checked ? true : false;

                    info.SetValue(Option, bChecked);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error during option matching: " + ex.Message);
            }

        }

        private void simpleButtonOK_Click(object sender, EventArgs e)
        {
            SetOption();

            this.DialogResult = DialogResult.OK;

            this.Close();
        }

        private void checkedListBoxControlOption_ItemCheck(object sender, DevExpress.XtraEditors.Controls.ItemCheckEventArgs e)
        {
            bool bChecked = e.State == CheckState.Checked ? true : false;
            string strName = checkedListBoxControlOption.Items[e.Index].Value.ToString();

            if (strName == eExclusion.Time_SelectedTest.ToString() && bChecked)
            {
                checkedListBoxControlOption.Items.Where(x => x.Value.ToString() == eExclusion.Time_Day.ToString()).FirstOrDefault().CheckState = CheckState.Unchecked;
                checkedListBoxControlOption.Items.Where(x => x.Value.ToString() == eExclusion.Time_WholeRange.ToString()).FirstOrDefault().CheckState = CheckState.Unchecked;
            }
            else if (strName == eExclusion.Time_Day.ToString() && bChecked)
            {
                checkedListBoxControlOption.Items.Where(x => x.Value.ToString() == eExclusion.Time_SelectedTest.ToString()).FirstOrDefault().CheckState = CheckState.Unchecked;
                checkedListBoxControlOption.Items.Where(x => x.Value.ToString() == eExclusion.Time_WholeRange.ToString()).FirstOrDefault().CheckState = CheckState.Unchecked;
            }
            else if (strName == eExclusion.Time_WholeRange.ToString() && bChecked)
            {
                checkedListBoxControlOption.Items.Where(x => x.Value.ToString() == eExclusion.Time_SelectedTest.ToString()).FirstOrDefault().CheckState = CheckState.Unchecked;
                checkedListBoxControlOption.Items.Where(x => x.Value.ToString() == eExclusion.Time_Day.ToString()).FirstOrDefault().CheckState = CheckState.Unchecked;
            }
            //else if (!bChecked)
            //{
            //    checkedListBoxControlOption.Items.Where(x => x.Value.ToString() == eExclusion.Time_SelectedTest.ToString()).FirstOrDefault().CheckState = CheckState.Checked;
            //    checkedListBoxControlOption.Items.Where(x => x.Value.ToString() == eExclusion.Time_Day.ToString()).FirstOrDefault().CheckState = CheckState.Unchecked;
            //    checkedListBoxControlOption.Items.Where(x => x.Value.ToString() == eExclusion.Time_WholeRange.ToString()).FirstOrDefault().CheckState = CheckState.Unchecked;
            //}            
        }

        private void checkedListBoxControlOption_CheckMemberChanged(object sender, EventArgs e)
        {

        }

        private void checkedListBoxControlOption_SelectedValueChanged(object sender, EventArgs e)
        {

        }
    }
}
