using ManualSoftwareSystem.Operation_Manual;
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;
using System.Xml;

namespace ManualSoftwareSystem.Inspection_Work
{
    public partial class FrmCondition : Form
    {
        private string condition = string.Empty;
        private string[] arrayCondition;
        private string conditionSave = string.Empty;
        public dynamic objLanguage;

        public FrmCondition(string condition)
        {
            InitializeComponent();

            this.Size = new System.Drawing.Size(692, 440);
            lblMassage.Visible = false;
            txtMassage.Visible = false;
            pnlCharacter.Visible = false;
            cbxInput.Visible = false;
            pnlInteger.Visible = false;
            this.condition = condition;
            this.objLanguage = FrmOperationManualOperator.objLanguage;
        }

        private void FrmCondition_Load(object sender, EventArgs e)
        {
            string strCondition = "Condition";
            string strType = "Type";
            string strMassage = "Message";
            string strJudgment = "Judgment";
            string strAnAsterisk = "An asterisk \" * \" can be used as a wildcard.";
            //string strIfYou = "If you don't want to set the number of characters,  don't enter anything.";
            string strIfYou = "If you don't want to set the number of characters,  don't input anything.";
            string strWordCount = "Word count";

            string strCheck = "Check to Upper case letters and Lower case letters";
            //string strInput = "Input Required";
            string strInput = "Required input";
            string strStandardValue = "Standard value";
            //string strFormula = "Formula";
            string strFormula = "Judgment method";
            string strMin = "Min";
            string strMax = "Max";
            string strUnit = "Unit";

            string strSave = "Save";
            string strCancel = "Cancel";

            lblCondition.Text = objLanguage.Translate(strCondition);
            lblType.Text = objLanguage.Translate(strType);
            lblMassage.Text = objLanguage.Translate(strMassage);
            lblJudgment.Text = objLanguage.Translate(strJudgment);
            lblAnAsterisk.Text = objLanguage.Translate(strAnAsterisk);
            lblIfYou.Text = objLanguage.Translate(strIfYou);
            lblWordCount.Text = objLanguage.Translate(strWordCount);

            cbxCheck.Text = objLanguage.Translate(strCheck);
            cbxInput.Text = objLanguage.Translate(strInput);
            lblStandardValue.Text = objLanguage.Translate(strStandardValue) + " (x)";
            lblFormula.Text = objLanguage.Translate(strFormula);
            lblMin.Text = objLanguage.Translate(strMin);
            lblMax.Text = objLanguage.Translate(strMax);
            lblUnit.Text = objLanguage.Translate(strUnit);

            btnSave.Text = objLanguage.Translate(strSave);
            btnCancel.Text = objLanguage.Translate(strCancel);


            arrayCondition = new string[8];

            cmbType.Items.Clear();
            cmbType.Items.Add(string.Empty);
            cmbType.Items.Add("Character input");
            cmbType.Items.Add("Integer input");
            cmbType.Items.Add("Decimal input");

            cbxLess.Items.Clear();
            cbxLess.Items.Add("<");
            cbxLess.Items.Add("<=");
            cbxLess.Items.Add("None");

            cbxMore.Items.Clear();
            cbxMore.Items.Add("<");
            cbxMore.Items.Add("<=");
            cbxMore.Items.Add("None");

            if (condition != string.Empty)
            {
                if (condition.Split("|").Length == 8)
                {
                    arrayCondition = condition.Split("|");
                    cmbType.Text = arrayCondition[0];
                    if (cmbType.Text == "Character input")
                    {
                        txtMassage.Text = arrayCondition[1];
                        txtJudgment.Text = arrayCondition[2];
                        txtWordCount.Text = arrayCondition[3];
                        if (arrayCondition[4] == "1")
                        {
                            cbxCheck.Checked = true;
                        }
                        else if (arrayCondition[4] == "0")
                        {
                            cbxCheck.Checked = false;
                        }
                        if (arrayCondition[5] == "1")
                        {
                            cbxInput.Checked = true;
                        }
                        else if (arrayCondition[5] == "0")
                        {
                            cbxInput.Checked = false;
                        }
                    }
                    else if (cmbType.Text == "Integer input")
                    {
                        txtMassage.Text = arrayCondition[1];
                        txtMin.Text = arrayCondition[2];
                        cbxLess.Text = arrayCondition[3];
                        cbxMore.Text = arrayCondition[4];
                        txtMax.Text = arrayCondition[5];
                        txtUnit.Text = arrayCondition[6];
                        if (arrayCondition[7] == "1")
                        {
                            cbxInput.Checked = true;
                        }
                        else if (arrayCondition[7] == "0")
                        {
                            cbxInput.Checked = false;
                        }
                        if (cbxLess.Text == "None")
                        {
                            txtMin.Text = string.Empty;
                            txtMin.Enabled = false;
                        }
                        else
                        {
                            txtMin.Enabled = true;
                        }
                        if (cbxMore.Text == "None")
                        {
                            txtMax.Text = string.Empty;
                            txtMax.Enabled = false;
                        }
                        else
                        {
                            txtMax.Enabled = true;
                        }

                    }
                    else if (cmbType.Text == "Decimal input")
                    {
                        txtMassage.Text = arrayCondition[1];
                        txtMin.Text = arrayCondition[2];
                        cbxLess.Text = arrayCondition[3];
                        cbxMore.Text = arrayCondition[4];
                        txtMax.Text = arrayCondition[5];
                        //txtUnit.Text = arrayCondition[6];
                        if (arrayCondition[7] == "1")
                        {
                            cbxInput.Checked = true;
                        }
                        else if (arrayCondition[7] == "0")
                        {
                            cbxInput.Checked = false;
                        }
                        if (cbxLess.Text == "None")
                        {
                            txtMin.Text = string.Empty;
                            txtMin.Enabled = false;
                        }
                        else
                        {
                            txtMin.Enabled = true;
                        }
                        if (cbxMore.Text == "None")
                        {
                            txtMax.Text = string.Empty;
                            txtMax.Enabled = false;
                        }
                        else
                        {
                            txtMax.Enabled = true;
                        }
                    }
                }
            }
            else
            {
                StartClear();
            }

        }

        private void StartClear()
        {
            lblMassage.Visible = false;
            txtMassage.Visible = false;
            pnlCharacter.Visible = false;
            pnlInteger.Visible = false;
            cbxInput.Visible = false;
            txtMassage.Text = string.Empty;
            txtJudgment.Text = string.Empty;
            txtWordCount.Text = string.Empty;
            cbxCheck.Checked = false;
            cbxInput.Checked = false;
            txtMin.Text = string.Empty;
            cbxLess.Text = string.Empty;
            cbxMore.Text = string.Empty;
            txtMax.Text = string.Empty;
            txtUnit.Text = string.Empty;
        }

        private void cmbType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbType.Text == "Character input")
            {
                if (txtMassage.Text.Trim() == "")
                {
                    txtMassage.Text = "Please enter Serial.";
                }
                lblMassage.Visible = true;
                txtMassage.Visible = true;
                pnlCharacter.Visible = true;
                pnlInteger.Visible = false;
                cbxInput.Visible = true;
                pnlInteger.SendToBack();
                pnlCharacter.BringToFront();
            }
            else if (cmbType.Text == "Integer input")
            {
                //pnlInteger.Location = new Point(7, 142);

                if (txtMassage.Text.Trim() == "")
                {
                    txtMassage.Text = "Please enter measured value.";
                }
                lblMassage.Visible = true;
                txtMassage.Visible = true;
                pnlInteger.Visible = true;
                pnlCharacter.Visible = false;
                cbxInput.Visible = true;
                lblUnit.Visible = true;
                txtUnit.Visible = true;
                pnlCharacter.SendToBack();
                pnlInteger.BringToFront();
            }
            else if (cmbType.Text == "Decimal input")
            {
                //pnlInteger.Location = new Point(7, 142);

                if (txtMassage.Text.Trim() == "")
                {
                    txtMassage.Text = "Please enter measured value.";
                }
                lblMassage.Visible = true;
                txtMassage.Visible = true;
                pnlInteger.Visible = true;
                pnlCharacter.Visible = false;
                cbxInput.Visible = true;
                lblUnit.Visible = true;
                txtUnit.Visible = true;
                pnlCharacter.SendToBack();
                pnlInteger.BringToFront();
            }
            else if (cmbType.Text == string.Empty)
            {
                StartClear();
            }

        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            string strCheck = "0";
            string strInput = "0";
            conditionSave = string.Empty;

            if (cmbType.Text == "Character input")
            {
                if (cbxCheck.Checked == true)
                {
                    strCheck = "1";
                }
                else if (cbxCheck.Checked == false)
                {
                    strCheck = "0";
                }
                if (cbxInput.Checked == true)
                {
                    strInput = "1";
                }
                else if (cbxCheck.Checked == false)
                {
                    strInput = "0";
                }
                //if (txtWordCount.Text.Replace(",", "").Replace("|", "").Trim().Length == 0)
                //{
                //    txtWordCount.Text = "0";
                //}
                conditionSave = cmbType.Text + "|" + txtMassage.Text.Replace(",", "").Replace("|", "")
                    + "|" + txtJudgment.Text.Replace(",", "").Replace("|", "")
                    + "|" + txtWordCount.Text.Replace(",", "").Replace("|", "")
                    + "|" + strCheck + "|" + strInput + "||";

                FrmMasterMaintenanceDetail.inputComment = txtMassage.Text.Replace(",", "").Replace("|", "");
                FrmMasterMaintenanceDetail.inputValue1 = string.Empty;
                FrmMasterMaintenanceDetail.inputValue2 = string.Empty;
                FrmMasterMaintenanceDetail.strUnit = string.Empty;

            }
            else if (cmbType.Text == "Integer input")
            {
                if (cbxInput.Checked == true)
                {
                    strInput = "1";
                }
                else if (cbxCheck.Checked == false)
                {
                    strInput = "0";
                }
                if (cbxLess.Text == string.Empty)
                {
                    cbxLess.Text = "None";
                    txtMin.Text = string.Empty;
                }
                if (cbxMore.Text == string.Empty)
                {
                    cbxMore.Text = "None";
                    txtMax.Text = string.Empty;
                }
                conditionSave = cmbType.Text + "|" + txtMassage.Text.Replace(",", "").Replace("|", "")
                    + "|" + txtMin.Text.Replace(",", "").Replace("|", "")
                    + "|" + cbxLess.Text.Replace(",", "").Replace("|", "")
                    + "|" + cbxMore.Text.Replace(",", "").Replace("|", "")
                    + "|" + txtMax.Text.Replace(",", "").Replace("|", "")
                    + "|" + txtUnit.Text.Replace(",", "").Replace("|", "")
                    + "|" + strInput;

                FrmMasterMaintenanceDetail.inputComment = txtMassage.Text.Replace(",", "").Replace("|", "");
                FrmMasterMaintenanceDetail.inputValue1 = txtMin.Text.Replace(",", "").Replace("|", "");
                FrmMasterMaintenanceDetail.inputValue2 = txtMax.Text.Replace(",", "").Replace("|", "");
                FrmMasterMaintenanceDetail.strUnit = txtUnit.Text.Replace(",", "").Replace("|", "");

            }
            else if (cmbType.Text == "Decimal input")
            {
                if (cbxInput.Checked == true)
                {
                    strInput = "1";
                }
                else if (cbxCheck.Checked == false)
                {
                    strInput = "0";
                }
                if (cbxLess.Text == string.Empty)
                {
                    cbxLess.Text = "None";
                    txtMin.Text = string.Empty;
                }
                if (cbxMore.Text == string.Empty)
                {
                    cbxMore.Text = "None";
                    txtMax.Text = string.Empty;
                }
                conditionSave = cmbType.Text + "|" + txtMassage.Text.Replace(",", "").Replace("|", "")
                    + "|" + txtMin.Text.Replace(",", "").Replace("|", "")
                    + "|" + cbxLess.Text.Replace(",", "").Replace("|", "")
                    + "|" + cbxMore.Text.Replace(",", "").Replace("|", "")
                    + "|" + txtMax.Text.Replace(",", "").Replace("|", "")
                    + "|" + txtUnit.Text.Replace(",", "").Replace("|", "")
                    + "|" + strInput;

                FrmMasterMaintenanceDetail.inputComment = txtMassage.Text.Replace(",", "").Replace("|", "");
                FrmMasterMaintenanceDetail.inputValue1 = txtMin.Text.Replace(",", "").Replace("|", "");
                FrmMasterMaintenanceDetail.inputValue2 = txtMax.Text.Replace(",", "").Replace("|", "");
                FrmMasterMaintenanceDetail.strUnit = txtUnit.Text.Replace(",", "").Replace("|", "");

            }
            else if (cmbType.Text == string.Empty)
            {
                conditionSave = string.Empty;

                FrmMasterMaintenanceDetail.inputComment = string.Empty;
                FrmMasterMaintenanceDetail.inputValue1 = string.Empty;
                FrmMasterMaintenanceDetail.inputValue2 = string.Empty;
                FrmMasterMaintenanceDetail.strUnit = string.Empty;
            }

            FrmMasterMaintenanceDetail.conditionForSave = conditionSave;
            FrmMasterMaintenanceDetail.inputComment = txtMassage.Text.Replace(",", "").Replace("|", "");

            FrmMasterMaintenanceDetail.isCancel = false;
            this.Close();

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            FrmMasterMaintenanceDetail.isCancel = true;
            this.Close();
        }

        private void txtWordCount_TextChanged(object sender, EventArgs e)
        {

        }

        private void txtWordCount_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == ((char)Keys.Back))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = !Char.IsDigit(e.KeyChar);
            }


        }

        private void txtMin_TextChanged(object sender, EventArgs e)
        {

        }

        private void txtMin_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (cmbType.Text == "Decimal input")
            {

                if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != '.')
                {
                    e.Handled = true;
                }

                // only allow one decimal point
                if (e.KeyChar == '.' && (sender as TextBox).Text.IndexOf('.') > -1)
                {
                    e.Handled = true;
                }

                if (!char.IsControl(e.KeyChar))
                {

                    TextBox textBox = (TextBox)sender;

                    if (textBox.Text.IndexOf('.') > -1 &&
                             textBox.Text.Substring(textBox.Text.IndexOf('.')).Length >= 3)
                    {
                        e.Handled = true;
                    }
                }
            }
            else if (cmbType.Text == "Integer input")
            {
                if (e.KeyChar == ((char)Keys.Back))
                {
                    e.Handled = false;
                }
                else
                {
                    e.Handled = !Char.IsDigit(e.KeyChar);
                }
            }
        }

        private void txtMax_TextChanged(object sender, EventArgs e)
        {

        }

        private void txtMax_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (cmbType.Text == "Decimal input")
            {

                if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != '.')
                {
                    e.Handled = true;
                }

                // only allow one decimal point
                if (e.KeyChar == '.' && (sender as TextBox).Text.IndexOf('.') > -1)
                {
                    e.Handled = true;
                }

                if (!char.IsControl(e.KeyChar))
                {

                    TextBox textBox = (TextBox)sender;

                    if (textBox.Text.IndexOf('.') > -1 &&
                             textBox.Text.Substring(textBox.Text.IndexOf('.')).Length >= 3)
                    {
                        e.Handled = true;
                    }
                }
            }
            else if (cmbType.Text == "Integer input")
            {
                if (e.KeyChar == ((char)Keys.Back))
                {
                    e.Handled = false;
                }
                else
                {
                    e.Handled = !Char.IsDigit(e.KeyChar);
                }
            }
        }

        private void cbxLess_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbxLess.Text == "None")
            {
                txtMin.Text = string.Empty;
                txtMin.Enabled = false;
            }
            else
            {
                txtMin.Enabled = true;
            }
        }

        private void cbxMore_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbxMore.Text == "None")
            {
                txtMax.Text = string.Empty;
                txtMax.Enabled = false;
            }
            else
            {
                txtMax.Enabled = true;
            }
        }

        private void pnlInteger_Paint(object sender, PaintEventArgs e)
        {

        }
    }
}
