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.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using SystemX.Common;
using SystemX.Product.ALIS.Interface;

namespace SystemX.Product.ALIS.UI.View.InfoList
{
    public partial class HostInfoEdit : Form
    {
        public eSelectMode CurrentWizardMode { internal set; get; }

        private UcInfoView.selectHostInfo SelHostInfo;

        private IDataController ctrlDB;

        private static object objInfoControlNextLock = new object();

        public enum eSelectMode
        {
            Insert = 0,
            Modify
        }

        private string strTitle = "STAT Host";
        private string strWizardControl = "STAT Host";
        private string strWelcomeWizard = "Welcome to the STAT Host wizard!";
        private string strWizardPage = "STAT Host Information";

        private string strModifyHostID;
        private string strModifySection;

        public void WizardCancelEvent(object sender, EventArgs e)
        {
            if (((bool)sender) == false)
                this.DialogResult = DialogResult.Cancel;
        }

        public HostInfoEdit(IDataController ctrlDB, eSelectMode setMode, UcInfoView.selectHostInfo getSelHostInfo)
        {
            InitializeComponent();

            this.Text = strTitle;
            wizardControl.Text = strWizardControl;
            welcomeWizardPage.Text = strWelcomeWizard;
            wizardPage.Text = strWizardPage;
            //
            CurrentWizardMode = setMode;

            SelHostInfo = getSelHostInfo;

            this.ctrlDB = ctrlDB;

            strModifyHostID = string.Empty;
            strModifySection = string.Empty;

            switch (CurrentWizardMode)
            {
                case eSelectMode.Insert:
                    welcomeWizardPage.IntroductionText = "This is the procedure to insert new information into the " + strTitle + " table.";
                    wizardControl.Text = strTitle + " - Insert";
                    this.Text = strTitle + " - Insert";
                    break;
                case eSelectMode.Modify:
                    welcomeWizardPage.IntroductionText = "This is the procedure to modify new information in the " + strTitle + " table.";
                    wizardControl.Text = strTitle + " - Modify";
                    this.Text = strTitle + " - Modify";

                    textBoxHostID.Text = SelHostInfo.HostID;
                    textBoxSection.Text = SelHostInfo.Section;
                    textBoxIP.Text = SelHostInfo.IP;
                    textBoxTestCode.Text = SelHostInfo.TestCode;
                    textBoxComment.Text = SelHostInfo.Comment;

                    strModifyHostID = textBoxHostID.Text;
                    strModifySection = textBoxSection.Text;
                    break;
            }
        }
        private DataTable getQueryDatabase(string strQuery)
        {
            ctrlDB.GetConnSqlCmd().CommandText = strQuery;
            SqlDataReader reader;
            reader = ctrlDB.GetConnSqlCmd().ExecuteReader();
            DataTable dt = new DataTable();
            dt.Load(reader);
            reader.Close();

            return dt;
        }

        private void wizardControl_NextClick(object sender, DevExpress.XtraWizard.WizardCommandButtonClickEventArgs e)
        {
            if (wizardControl.SelectedPageIndex == 1)
            {
                lock (objInfoControlNextLock)
                {
                    try
                    {
                        string strItemName = "";
                        string strGetQueryText = string.Empty;

                        if (textBoxHostID.Text.Length <= 0) { strItemName = "HostID"; throw new Exception($"The value cannot be null. " + strItemName); }
                        if (textBoxSection.Text.Length <= 0) { strItemName = "Section"; throw new Exception($"The value cannot be null. " + strItemName); }
                        if (textBoxIP.Text.Length <= 0) { strItemName = "IP"; throw new Exception($"The value cannot be null. " + strItemName); }
                        //
                        IPAddress getIPAddres;
                        if (IPAddress.TryParse(textBoxIP.Text, out getIPAddres) == false)
                            throw new Exception($"IP format is not correct. : {textBoxIP.Text}");
                        else
                            textBoxIP.Text = getIPAddres.ToString();
                        //
                        if (textBoxTestCode.Text.Length > 0) 
                        {
                            strGetQueryText = "SELECT * FROM [STAT_TestCode] WHERE TestCode = '" + textBoxTestCode.Text + "';";

                            if(XCommons.isHasRow(getQueryDatabase(strGetQueryText)) == false)
                            {
                                strItemName = "TestCode"; throw new Exception($"Only existing test codes can be specified. " + strItemName);
                            }
                        }

                        string strGetHostID = textBoxHostID.Text;
                        string strGetSection = textBoxSection.Text;

                        strGetQueryText = "SELECT* FROM [STAT_Host] WHERE " +
                            "HostID = '" + strGetHostID + "' AND " +
                            "Section = '" + strGetSection + "' ORDER BY No ASC";

                        DataTable dtCheck = getQueryDatabase(strGetQueryText);
              
                        /*
                        DataRow[] drFindInfo = dtCheck.Select($"HostID='{strGetHostID}' AND Section='{strGetSection}'");

                        if (drFindInfo.Count() > 0)
                        {
                            MessageBox.Show("dddd");
                        }
                        */

                        if (XCommons.isHasRow(dtCheck))
                        {
                            bool bExistException = true;

                            if (CurrentWizardMode == eSelectMode.Modify)
                            {
                                if (strGetHostID.CompareTo(strModifyHostID) == 0 &&
                                   strGetSection.CompareTo(strModifySection) == 0)
                                    bExistException = false;
                            }

                            if (bExistException)
                                throw new Exception($"HostID - Section Combine Key is information that already exists. : " +
                                    $"{strGetHostID}" + " " + $"{strGetSection}");
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, strTitle, MessageBoxButtons.OK, MessageBoxIcon.Warning);

                        e.Handled = true;

                        return;
                    }

                    labelControlHostID.Text = textBoxHostID.Text;
                    labelControlSection.Text = textBoxSection.Text;
                    labelControlIP.Text = textBoxIP.Text;
                    labelControlTestCode.Text = textBoxTestCode.Text;
                    labelControlComment.Text = textBoxComment.Text;

                    switch (CurrentWizardMode)
                    {
                        case eSelectMode.Insert:
                            break;
                        case eSelectMode.Modify:
                            break;
                    }

                    SelHostInfo.HostID = textBoxHostID.Text;
                    SelHostInfo.Section = textBoxSection.Text;
                    SelHostInfo.IP = textBoxIP.Text;
                    SelHostInfo.TestCode = textBoxTestCode.Text;
                    SelHostInfo.Comment = textBoxComment.Text;
                }
            }
        }

        private void wizardControl_CustomizeCommandButtons(object sender, DevExpress.XtraWizard.CustomizeCommandButtonsEventArgs e)
        {
            if (e.Page == wizardPage)
            {
                this.ActiveControl = textBoxHostID;

                textBoxHostID.Focus();
            }
            else if (e.Page == welcomeWizardPage)
            {
                this.ActiveControl = e.NextButton.Button;

                e.NextButton.Button.Focus();
            }
        }
    }
}
