using System;
using System.Windows.Forms;
//using BasicData;
using System.Text.RegularExpressions;
//using SystemFramework;
using System.Threading;
using DoWhatPlc;

namespace MisDataSaveDateSim
{
    public partial class WriteQualityValueForm : Form
    {
        QualityDataType SelectListData;
        Form MainForm;
        public WriteQualityValueForm(QualityDataType SelectData,Form  Mainform)
        {
            try
            {
                InitializeComponent();
                SelectListData = new QualityDataType();
                SelectListData = SelectData;
                SetForm(SelectData.tagTypeID, SelectData.tagValue);
                MainForm = Mainform;
                this.Parent = Mainform;


            }
            catch (Exception err)
            {
                //ApplicationLog.WriteLog(err, err.Message);
            }
        }

        private void button_OK_Click(object sender, EventArgs e)
        {
            try
            { 
                if (!TagTypeID_Confirm(SelectListData.tagTypeID)) return;

                this.Hide () ;

                QualityDataQueryStruct QualityStruct = new QualityDataQueryStruct(SelectListData.tagID , SelectListData.tagTypeID , comboBox_InputValue.SelectedIndex.ToString(), textBox_InputValue.Text,this);
                Thread t = new Thread(new ParameterizedThreadStart(WriteQualityDataByTagID_Thread));
                t.Start(QualityStruct);

            }
            catch (Exception err)
            {
                //ApplicationLog.WriteLog(err, err.Message);

            }
        }


        void WriteQualityDataByTagID_Thread(object obj)
        {
            try
            {
                QualityDataQueryStruct QualityStruct = (QualityDataQueryStruct)obj;
                Form ThisForm = (Form)QualityStruct.tempForm;


                if (QualityStruct.tagTypeID == 11)
                {
                    DtWebApi.ReadWritePLC.WritePLC(QualityStruct.tagID, QualityStruct.tagValue.ToString());
                }
                else
                {
                    DtWebApi.ReadWritePLC.WritePLC(SelectListData.tagID, QualityStruct.tagValue2.ToString());
                }
                MainForm.BeginInvoke (MisData.MainFormRefresh, null, null);
                Action ActionDelete = () => { this.Close();this.Dispose(); };
                if(this.InvokeRequired )
                {
                    ThisForm.BeginInvoke(ActionDelete);
                }
                else
                {
                    ActionDelete();
                }
                
                
            }
            catch (Exception err)
            {
                //ApplicationLog.WriteLog(err, err.Message);

            }

        }




        private void button_Cancle_Click(object sender, EventArgs e)
        {
            this.Close();
            this.Dispose();
        }



        void SetForm( int TagTypeID,object TagValue)
        {
            try
            {
                switch (TagTypeID)
                {
                    case 11://BOOL
                        comboBox_InputValue.Enabled = true;
                        comboBox_InputValue.Visible = true;
                        textBox_InputValue.Enabled = false;
                        textBox_InputValue.Visible = false;
                        label_Message.Text = "Data type: Boolean";
                        if (Convert.ToBoolean(TagValue))
                        {
                            comboBox_InputValue.SelectedIndex = 1;
                        }
                        else
                        {
                            comboBox_InputValue.SelectedIndex = 0;
                        }
                        
                        break;

                    case 2://INT.
                        label_Message.Text = "Data type: Int16 Range from -32768 to 32767";
                        comboBox_InputValue.Enabled = false;
                        comboBox_InputValue.Visible = false;
                        textBox_InputValue.Enabled = true;
                        textBox_InputValue.Visible = true;
                        textBox_InputValue.Text = TagValue.ToString();
                        
                        break;
                    case 17://BYTE
                        label_Message.Text = "Data type: Byte Range from 0 to 255";
                        comboBox_InputValue.Enabled = false;
                        comboBox_InputValue.Visible = false;
                        textBox_InputValue.Enabled = true;
                        textBox_InputValue.Visible = true;
                        textBox_InputValue.Text = TagValue.ToString();
                       
                        break;
                    case 8://STRING
                        label_Message.Text = "Data type: String";
                        comboBox_InputValue.Enabled = false;
                        comboBox_InputValue.Visible = false;
                        textBox_InputValue.Enabled = true;
                        textBox_InputValue.Visible = true;
                        textBox_InputValue.Text = TagValue.ToString();
                        
                        break;
                    case 18://Word
                        label_Message.Text = "Data type: UInt16 Range from 0 to 65535";
                        comboBox_InputValue.Enabled = false;
                        comboBox_InputValue.Visible = false;
                        textBox_InputValue.Enabled = true;
                        textBox_InputValue.Visible = true;
                        textBox_InputValue.Text = TagValue.ToString();
                        
                        break;
                    case 4://real
                        label_Message.Text = "Data type: Single Range from -3.402823E+38 to 3.402823E+38";
                        comboBox_InputValue.Enabled = false;
                        comboBox_InputValue.Visible = false;
                        textBox_InputValue.Enabled = true;
                        textBox_InputValue.Visible = true;
                        textBox_InputValue.Text = TagValue.ToString();
                        
                        break;
                    default://Unknow
                        label_Message.Text = "Data type: Unknow";
                        comboBox_InputValue.Enabled = false;
                        comboBox_InputValue.Visible = false;
                        textBox_InputValue.Enabled = true;
                        textBox_InputValue.Visible = true;
                        textBox_InputValue.Text = TagValue.ToString();
                        
                        break;

                }
            }
            catch (Exception err)
            {
                //ApplicationLog.WriteLog(err, err.Message);

            }


        }

        bool TagTypeID_Confirm( int TagTypeID)
        {
            try
            {
                switch (TagTypeID)
                {


                    case 2://INT
                           //TextBox存在非数字字符   报错
                        if (!Regex.IsMatch(textBox_InputValue.Text, @"^[-]?\d*$"))
                        {
                            MessageBox.Show("输入不是整数！");
                            return false;
                        }
                        if (textBox_InputValue.Text.Length > 9)
                        {
                            MessageBox.Show("输入数据不在范围内");
                            return false;
                        }
                        break;
                    case 17://BYTE
                            //TextBox存在非数字字符   报错
                        if (!Regex.IsMatch(textBox_InputValue.Text, @"^[-]?\d*$"))
                        {
                            MessageBox.Show("输入不是整数！");
                            return false;
                        }
                        if (Convert.ToInt32(textBox_InputValue.Text.Trim()) > 255)
                        {
                            MessageBox.Show("输入数据不在范围内");
                            return false;
                        }
                        break;
                    case 8://STRING

                        break;
                    case 18://Word
                            //TextBox存在非数字字符   报错
                        if (!Regex.IsMatch(textBox_InputValue.Text, @"^[-]?\d*$"))
                        {
                            MessageBox.Show("输入不是整数！");
                            return false;
                        }
                        if (textBox_InputValue.Text.Trim().Length > 4)
                        {
                            MessageBox.Show("输入数据不在范围内");
                            return false;
                        }
                        break;
                    case 4://real
                           //TextBox存在非数字字符   报错
                           //TextBox存在非数字字符   报错
                        if (!Regex.IsMatch(textBox_InputValue.Text, @"^-?\d+\.?\d*$"))
                        {
                            MessageBox.Show("输入不是实数！");
                            return false;
                        }

                        break;
                    default:
                        break;

                }

                return true;
            }
            catch (Exception err)
            {
                //ApplicationLog.WriteLog(err, err.Message);
                return true;
            }
        }

    }
}
