using DevExpress.Data.Helpers;
using DevExpress.XtraBars;
using DevExpress.XtraBars.Navigation;

using System;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;
using System.IO;
using System.Diagnostics;

using SystemX.Net;
using SystemX.Common;
using SystemX.Net.BaseProtocol;
using SystemX.Net.Comm;
using SystemX.Net.Schedule;
using SystemX.Net.DB.LogProcess;
using SystemX.Common.Serialization;
using SystemX.Common.Archive;
using System.Threading.Tasks;

//using SystemX.Common.Protocol.SIA;

using static SystemX.Net.Platform.Common.Util.LogMessage;
using System.Threading;
using SystemX.Net.Platform.Common.Util;
using SystemX.Net.Platform.Common.ExtensionMethods;
using SystemX.Net.Middleware.Log.Commons;
using static SystemX.Net.DB.LogProcess.XLogDBConnManager;

namespace SystemX.Net.MiddlewareUI
{
    public partial class MainForm : DevExpress.XtraBars.FluentDesignSystem.FluentDesignForm
    {
        private void Chktimer_Tick(object sender, EventArgs e)
        {
            Chktimer.Enabled = false;

            if (MiddlewareLogStartState == false)
            {
                ConsoleUtil.ConsoleVisibleControl();

                //The program is automatically terminated.
                MessageBox.Show("Server information could not be retrieved. Check the console error text.", "SystemX.Net.Middleware.UI.Log", MessageBoxButtons.OK, MessageBoxIcon.Error);

                // TODO : FTP ALIS
                this.Close();
            }
        }

        private void UItimer_Tick(object sender, EventArgs e)
        {
            if (m_bTimerLock[(int)TIMER_LOCK.UI_TIMER] == false)
            {
                m_bTimerLock[(int)TIMER_LOCK.UI_TIMER] = true;

                if (bTryBulkInsertBuffer)
                {
                    if (buttonBulkInsertStart.BackColor != Color.Yellow)
                        buttonBulkInsertStart.BackColor = Color.Yellow;
                }
                else
                {
                    if (buttonBulkInsertStart.BackColor != Color.Silver)
                        buttonBulkInsertStart.BackColor = Color.Silver;
                }

                labelBulkCntInfo1.Text = nBulkLogCount.ToString();
                labelBulkCntInfo2.Text = nBulkShortTermCount.ToString();
                labelBulkCntInfo3.Text = nBulkLongTermCount.ToString();

                labelBulkTime.Text = nBulkTimeValue.ToString();
                labelFTPTime.Text = nFTPTimeValue.ToString();

                labelDBMain.Text = "Information [" + MngDBLogConn.GetDBConnectInfo().ConnMain.SCHEMA + "]";
                labelDBShortTerm.Text = "ShortTerm [" + MngDBLogConn.GetDBConnectInfo().ConnShortTerm.SCHEMA + "]";
                labelShortTermSummaryTableName.Text = "└ [" + MngDBLogConn.GetDBConnectInfo().ConnShortTerm.SUMMARY_TABLE + "]";
                labelShortTermLogTableName.Text = "└ [" + MngDBLogConn.GetDBConnectInfo().ConnShortTerm.LOG_TABLE + "]";
                labelDBLongTerm.Text = "LongTerm [" + MngDBLogConn.GetDBConnectInfo().ConnLongTerm.SCHEMA + "]";

                if (bDatabaseConnResult)
                {
                    if (panelDBMain2.BackColor != Color.LimeGreen) panelDBMain2.BackColor = Color.LimeGreen;
                    if (panelDBShortTerm2.BackColor != Color.LimeGreen) panelDBShortTerm2.BackColor = Color.LimeGreen;
                    if (panelDBLongTerm2.BackColor != Color.LimeGreen) panelDBLongTerm2.BackColor = Color.LimeGreen;

                    if (buttonDbConnect.Visible) buttonDbConnect.Visible = false;
                }
                else
                {
                    if (panelDBMain2.BackColor != Color.Red) panelDBMain2.BackColor = Color.Red;
                    if (panelDBShortTerm2.BackColor != Color.Red) panelDBShortTerm2.BackColor = Color.Red;
                    if (panelDBLongTerm2.BackColor != Color.Red) panelDBLongTerm2.BackColor = Color.Red;

                    if (buttonDbConnect.Visible == false) buttonDbConnect.Visible = true;
                }

                if (progressBarProgram.Value + 20 <= 100)
                    progressBarProgram.Value += 20;
                else
                    progressBarProgram.Value = 0;

                int nPanelHeight = panelMain.Height / 2;

                panelRecv.Height = nPanelHeight - 20;
                panelProcess.Height = nPanelHeight - 20;

                int n1 = richTextBoxRecv.Lines.Count();
                int n2 = richTextBoxProcess.Lines.Count();

                if (richTextBoxRecv.Lines.Count() > 8192)
                    richTextBoxRecv.Clear();
                if (richTextBoxProcess.Lines.Count() > 8192)
                    richTextBoxProcess.Clear();

                if (LogDataRecvText.Count > 0)
                {
                    StringBuilder sb = null;

                    if (LogDataRecvText.TryDequeue(out sb))
                    {
                        //richTextBoxRecv.SuspendLayout();
                        richTextBoxRecv.AppendText(sb.ToString());

                        richTextBoxRecv.ScrollToCaret();
                        //richTextBoxRecv.ResumeLayout();
                    }
                }

                if (LogDataProcessText.Count > 0)
                {
                    StringBuilder sb = null;

                    if (LogDataProcessText.TryDequeue(out sb))
                    {
                        //richTextBoxProcess.SuspendLayout();
                        richTextBoxProcess.AppendText(sb.ToString());

                        richTextBoxProcess.ScrollToCaret();
                        //richTextBoxProcess.ResumeLayout();
                    }
                }

                m_bTimerLock[(int)TIMER_LOCK.UI_TIMER] = false;
            }
        }

        private string MakeInfoConnectProcessText(int nPos, int nCommandPort, int nStreamPort)
        {
            string strSetProcessInfo = nPos.ToString("D2") + ":" +
                            nCommandPort.ToString("D4") + ":" +
                            nStreamPort.ToString("D4");

            return strSetProcessInfo;
        }
    }
}