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

using DataBaseConnection.Control;

using DevExpress.XtraBars.Docking;
using DevExpress.XtraEditors;

using SystemX.Product.ALIS.Interface;
using static SystemX.Product.CP.TRA.Commons;

namespace SystemX.Product.ALIS.UI.Subs
{
    public partial class ConnectForm : DevExpress.XtraEditors.XtraForm
    {
        private enum eCehckType
        {
            TestListServer = 1,
            DataLogServer1 = 2,
            DataLogServer2 = 3
        }

        private IDataController ctrlDB;

        //TL Server
        public string strInputTextTL;

        private string strSetIPAddressTL;
        public string strGetIPAddressTL { get { return strSetIPAddressTL; } private set { strSetIPAddressTL = value; } }

        private int nSetConnPortTL;
        public int nGetConnPortTL { get { return nSetConnPortTL; } private set { nSetConnPortTL = value; } }

        //DT Server 1
        public string strInputTextDT1;

        private string strSetIPAddressDT1;
        public string strGetIPAddressDT1 { get { return strSetIPAddressDT1; } private set { strSetIPAddressDT1 = value; } }

        private int nSetConnPortDT1;
        public int nGetConnPortDT1 { get { return nSetConnPortDT1; } private set { nSetConnPortDT1 = value; } }
        //
        //DT Server 2
        public bool bSelectUseDataServer2;

        public string strInputTextDT2;

        private string strSetIPAddressDT2;
        public string strGetIPAddressDT2 { get { return strSetIPAddressDT2; } private set { strSetIPAddressDT2 = value; } }

        private int nSetConnPortDT2;
        public int nGetConnPortDT2 { get { return nSetConnPortDT2; } private set { nSetConnPortDT2 = value; } }
        //
        public int nOverviewModelC1;
        public int nOverviewModelC2;

        public DialogResult TestListServerCheckResult;
        public DialogResult DataServerCheckResult1;
        public DialogResult DataServerCheckResult2;


        public ConnectForm(IDataController ctrlDB)
        {
            InitializeComponent();

            //this.MinimumSize = new Size(400, 250);
            //this.MaximumSize = new Size(400, 250);

            this.ctrlDB = ctrlDB;

            //maskedTextBoxIP.Mask = "###.###.###.###";
            //maskedTextBoxIP.ValidatingType = typeof(System.Net.IPAddress);

            this.BringToFront();
            this.Focus();

            DialogResult = DialogResult.None;

            comboBoxOverviewModel1.SelectedIndex = 0;
            comboBoxOverviewModel2.SelectedIndex = 1;

            ConnectInfoINICtrl CCtrl = new ConnectInfoINICtrl();
            string strGetConnectInfo1 = CCtrl.GetValue("LastestConnect", "Info1", "");
            string strGetConnectInfo2 = CCtrl.GetValue("LastestConnect", "Info2", "");
            string strGetConnectInfo3 = CCtrl.GetValue("LastestConnect", "Info3", "");
            string strGetConnectInfo4 = CCtrl.GetValue("LastestConnect", "UseDataServer2", "False");

            string strGetConnectInfo5 = CCtrl.GetValue("LastestConnect", "OverviewModelInfoC1", "0");
            string strGetConnectInfo6 = CCtrl.GetValue("LastestConnect", "OverviewModelInfoC2", "1");

            int nOverInfoC1 = 0;
            int nOverInfoC2 = 1;

            if(int.TryParse(strGetConnectInfo5, out nOverInfoC1))
                comboBoxOverviewModel1.SelectedIndex = nOverInfoC1;
            if(int.TryParse(strGetConnectInfo6, out nOverInfoC2))
                comboBoxOverviewModel2.SelectedIndex = nOverInfoC2;

            if (strGetConnectInfo1.Length > 0)
                maskedTextBoxTLIP.Text = strGetConnectInfo1;

            if (strGetConnectInfo2.Length > 0)
                maskedTextBoxDT1IP.Text = strGetConnectInfo2;

            if (strGetConnectInfo3.Length > 0)
                maskedTextBoxDT2IP.Text = strGetConnectInfo3;

            if (strGetConnectInfo4.Length > 0)
            {
                bool bGetUseState = false;

                if (bool.TryParse(strGetConnectInfo4, out bGetUseState))
                    checkBoxUseDT2.Checked = bGetUseState;
                else
                    checkBoxUseDT2.Checked = false;
            }

            for (int i = 0; i < INICtrl.MAX_INFORMATION; i++)
            {
                string strGetInfo1 = CCtrl.GetValue("HistoryConnect" + i.ToString(), "Info1", "");
                string strGetInfo2 = CCtrl.GetValue("HistoryConnect" + i.ToString(), "Info2", "");
                string strGetInfo3 = CCtrl.GetValue("HistoryConnect" + i.ToString(), "Info3", "");

                if (strGetInfo1.Length > 0)
                    maskedTextBoxTLIP.Properties.Items.Add(strGetInfo1);
                if (strGetInfo2.Length > 0)
                    maskedTextBoxDT1IP.Properties.Items.Add(strGetInfo2);
                if (strGetInfo3.Length > 0)
                    maskedTextBoxDT2IP.Properties.Items.Add(strGetInfo3);
            }
        }

        private void InvaildIPAlarm()
        {
            MessageBox.Show("Invalid IP. Enter it in the normal format. (An empty string or [localhost] will attempt to connect to the local server.)", "[SystemX.Product.ALIS.UI]", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        private DialogResult CheckInformation(eCehckType ChkTyp, object sender, string strText)
        {
            DialogResult CheckResult = DialogResult.None;

            IPAddress getIPAddress = null;

            string strGetText = strText;
            string[] strGetSplitCommaText = strGetText.Split(',');
            string[] strGetSplitDotText = strGetText.Split('.');

            if (ChkTyp == eCehckType.TestListServer)
            {
                strGetIPAddressTL = string.Empty;

                nGetConnPortTL = DatabaseConnControl.CatalogConnPort;

                strInputTextTL = strText;
            }
            else if (ChkTyp == eCehckType.DataLogServer1)
            {
                strGetIPAddressDT1 = string.Empty;

                nGetConnPortDT1 = DatabaseConnControl.CatalogConnPort;

                strInputTextDT1 = strText;
            }
            else if (ChkTyp == eCehckType.DataLogServer2)
            {
                strGetIPAddressDT2 = string.Empty;

                nGetConnPortDT2 = DatabaseConnControl.CatalogConnPort;

                strInputTextDT2 = strText;
            }

            if (IPAddress.TryParse(strText, out getIPAddress) == false)
            {
                if (strGetSplitCommaText.Length == 2)
                {
                    string strGetIP = strGetSplitCommaText[0];
                    string strGetPort = strGetSplitCommaText[1];

                    int nGetPort = int.MaxValue;

                    if (IPAddress.TryParse(strGetIP, out getIPAddress) &&
                        int.TryParse(strGetPort, out nGetPort))
                    {
                        if (ChkTyp == eCehckType.TestListServer)
                        {
                            strGetIPAddressTL = strGetIP;

                            nGetConnPortTL = nGetPort;
                        }
                        else if (ChkTyp == eCehckType.DataLogServer1)
                        {
                            strGetIPAddressDT1 = strGetIP;

                            nGetConnPortDT1 = nGetPort;
                        }
                        else if (ChkTyp == eCehckType.DataLogServer2)
                        {
                            strGetIPAddressDT2 = strGetIP;

                            nGetConnPortDT2 = nGetPort;
                        }

                        CheckResult = DialogResult.OK;

                        return CheckResult;
                    }
                }

                string strGetUpperText = strText.ToUpper();

                if (strText.Length == 0)
                    CheckResult = DialogResult.Ignore;
                else if (strGetUpperText.CompareTo("LOCALHOST") == 0)
                    CheckResult = DialogResult.Ignore;
                else if (strText.CompareTo("If you do not enter the ip, will be connected to the local area.") == 0)
                    CheckResult = DialogResult.Ignore;
                else
                {
                    ((ComboBoxEdit)sender).Text = "";

                    InvaildIPAlarm();
                }
            }
            else
            {
                if (strGetSplitDotText.Length == 4)
                {
                    if (ChkTyp == eCehckType.TestListServer)
                        strGetIPAddressTL = strText;
                    else if (ChkTyp == eCehckType.DataLogServer1)
                        strGetIPAddressDT1 = strText;
                    else if (ChkTyp == eCehckType.DataLogServer2)
                        strGetIPAddressDT2 = strText;

                    CheckResult = DialogResult.OK;
                }
                else
                {
                    ((ComboBoxEdit)sender).Text = "";

                    InvaildIPAlarm();
                }
            }

            return CheckResult;
        }

        private void simpleButtonOK_Click(object sender, EventArgs e)
        {
            bSelectUseDataServer2 = checkBoxUseDT2.Checked;
            //
            nOverviewModelC1 = comboBoxOverviewModel1.SelectedIndex;
            nOverviewModelC2 = comboBoxOverviewModel2.SelectedIndex;

            TestListServerCheckResult = DialogResult.None;
            DataServerCheckResult1 = DialogResult.None;

            TestListServerCheckResult = CheckInformation(eCehckType.TestListServer, maskedTextBoxTLIP, maskedTextBoxTLIP.Text);
            DataServerCheckResult1 = CheckInformation(eCehckType.DataLogServer1, maskedTextBoxDT1IP, maskedTextBoxDT1IP.Text);

            if (bSelectUseDataServer2)
                DataServerCheckResult2 = CheckInformation(eCehckType.DataLogServer2, maskedTextBoxDT2IP, maskedTextBoxDT2IP.Text);
            else
                DataServerCheckResult2 = DialogResult.Ignore;

            if ((TestListServerCheckResult.HasFlag(DialogResult.OK) || TestListServerCheckResult.HasFlag(DialogResult.Ignore)) &&
                (DataServerCheckResult1.HasFlag(DialogResult.OK) || DataServerCheckResult1.HasFlag(DialogResult.Ignore)) &&
                (DataServerCheckResult2.HasFlag(DialogResult.OK) || DataServerCheckResult2.HasFlag(DialogResult.Ignore)))
                DialogResult = DialogResult.OK;
        }

        private void ConnectForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (DialogResult == DialogResult.None)
                DialogResult = DialogResult.Abort;

            /*if (maskedTextBoxIP.Text.Length == 0)
                DialogResult = DialogResult.Cancel;
            else if (maskedTextBoxIP.Text.CompareTo("localhost") == 0)
                DialogResult = DialogResult.Cancel;
            else if (maskedTextBoxIP.Text.CompareTo("If you do not enter the ip, will be connected to the local area.") == 0)
                DialogResult = DialogResult.Cancel;
            else
            {
                e.Cancel = true;

                if (IPAddress.TryParse(maskedTextBoxIP.Text, out IPAddress))
                    e.Cancel = false;
            }*/
        }

        private void maskedTextBoxIP_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            if (((ComboBoxEdit)sender).Text.CompareTo("If you do not enter the ip, will be connected to the local area.") == 0)
                ((ComboBoxEdit)sender).Text = string.Empty;
        }

        private void simpleButtonReset_Click(object sender, EventArgs e)
        {
            maskedTextBoxTLIP.Text = "If you do not enter the ip, will be connected to the local area.";
            maskedTextBoxDT1IP.Text = "If you do not enter the ip, will be connected to the local area.";
            maskedTextBoxDT2IP.Text = "If you do not enter the ip, will be connected to the local area.";
            checkBoxUseDT2.Checked = false;
        }

        private void label1_DoubleClick(object sender, EventArgs e)
        {
            comboBoxOverviewModel1.Visible = !comboBoxOverviewModel1.Visible;
        }

        private void label3_DoubleClick(object sender, EventArgs e)
        {
            comboBoxOverviewModel2.Visible = !comboBoxOverviewModel2.Visible;
        }
    }
}