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.ALIS.UI.Commons;

namespace SystemX.Product.ALIS.UI.Subs
{
    public partial class ConnectForm : DevExpress.XtraEditors.XtraForm
    {
        private IDataController ctrlDB;

        public string strInputText;

        private string strSetConnIPAddress;
        public string strGetConnIPAddress { get { return strSetConnIPAddress; } private set { strSetConnIPAddress = value; } }

        private int nSetConnPort;
        public int nGetConnPort { get { return nSetConnPort; } private set { nSetConnPort = value; } }
        //
        private string strSetConnCN;
        public string strGetConnCN { get { return strSetConnCN; } private set { strSetConnCN = value; } }

        private string strSetConnUID;
        public string strGetConnUID { get { return strSetConnUID; } private set { strSetConnUID = value; } }

        private string strSetConnPW;
        public string strGetConnPW { get { return strSetConnPW; } private set { strSetConnPW = value; } }

        public ConnectForm(IDataController ctrlDB)
        {
            InitializeComponent();

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

            this.ctrlDB = ctrlDB;

            maskedTextBoxCN.Text = DatabaseConnControl.CatalogName;
            maskedTextBoxUID.Text = DatabaseConnControl.CatalogConnUID;
            maskedTextBoxPW.Text = DatabaseConnControl.CatalogConnPW;

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

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

            DialogResult = DialogResult.None;

            ConnectInfoINICtrl CCtrl = new ConnectInfoINICtrl();
            string strGetConnectInfo = CCtrl.GetValue("LastestConnect", "Info", "");

            if (strGetConnectInfo.Length > 0)
                maskedComboIP.Text = strGetConnectInfo;

            for (int i = 0; i < INICtrl.MAX_INFORMATION; i++)
            {
                string strGetInfo = CCtrl.GetValue("HistoryConnect" + i.ToString(), "Info", "");

                if(strGetInfo.Length > 0)
                    maskedComboIP.Properties.Items.Add(strGetInfo);
            }
        }

        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 void simpleButtonOK_Click(object sender, EventArgs e)
        {
            IPAddress getIPAddress = null;

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

            strGetConnIPAddress = string.Empty;

            nGetConnPort = DatabaseConnControl.CatalogConnPort;

            strGetConnCN = maskedTextBoxCN.Text;
            strGetConnUID = maskedTextBoxUID.Text;
            strGetConnPW = maskedTextBoxPW.Text;

            strInputText = maskedComboIP.Text;

            if (IPAddress.TryParse(maskedComboIP.Text, 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))
                    {
                        strGetConnIPAddress = strGetIP;

                        nGetConnPort = nGetPort;

                        DialogResult = DialogResult.OK;

                        return;
                    }
                }

                string strGetUpperText = maskedComboIP.Text.ToUpper();

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

                    InvaildIPAlarm();
                }
            }
            else
            {
                if (strGetSplitDotText.Length == 4)
                {
                    strGetConnIPAddress = maskedComboIP.Text;

                    DialogResult = DialogResult.OK;
                }
                else
                {
                    maskedComboIP.Text = "";

                    InvaildIPAlarm();
                }
            }
        }

        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 simpleButtonAdvanced_Click(object sender, EventArgs e)
        {
            if (panelAdvanced.Visible == false)
            {
                this.MinimumSize = new Size(400, 200);
                this.MaximumSize = new Size(400, 200);

                panelAdvanced.Visible = true;
            }
            else
            {
                panelAdvanced.Visible = false;

                this.MaximumSize = new Size(400, 120);
                this.MinimumSize = new Size(400, 120);
            }
        }

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