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

using C4IT.Logging;
using C4IT.XML;
using C4IT.DataHistoryProvider;

using static C4IT.Logging.cLogManager;

namespace C4IT_DataHistoryProvider_Test
{
    public partial class ctrlClientAgent : UserControl
    {
        public ctrlClientAgent()
        {
            InitializeComponent();
        }

        private async void ctrlClientAgent_Load(object sender, EventArgs e)
        {
            panelScanNext.Height = textBoxScanNext.Height;
            panelScanNext.Top = textBoxScanNext.Top;

            var _requestInfo = new cF4sdWebRequestInfo("ctrlClientAgent_Load", null);
            await checkScanTimesAsync(_requestInfo, 1);
        }

        private async Task checkScanTimesAsync(cF4sdWebRequestInfo requestInfo, int LogDeep)
        {
            MethodBase CM = null; if (cLogManager.DefaultLogger.IsDebug) { CM = MethodBase.GetCurrentMethod(); LogMethodBegin(CM); }
            if (cPerformanceLogger.IsActive && requestInfo != null) { if (CM == null) CM = MethodBase.GetCurrentMethod(); cPerformanceLogger.LogPerformanceStart(LogDeep, CM, requestInfo.id, requestInfo.created); }
            var _startTime = DateTime.UtcNow;

            try
            {
                var Col = Color.Transparent;
                textBoxScanLast.Text = "";
                textBoxScanNext.Text = "";

                if (Program.Collector?.F4sdAgent != null)
                {
                    var scanInfo = await Program.Collector.F4sdAgent.GetScanTimeInfoAsync(requestInfo, LogDeep+1, CancellationToken.None);


                    if (scanInfo?.NextScan is DateTime nextScan)
                    {
                        textBoxScanNext.Text = nextScan.ToLocalTime().ToString();
                        if (scanInfo?.LastScan is DateTime lastScan)
                        {
                            textBoxScanLast.Text = lastScan.ToLocalTime().ToString();
                            if (nextScan >= lastScan)
                                Col = Color.Red;
                            else
                                Col = Color.Green;
                        }
                        else
                            Col = Color.Red;
                    }
                    else
                    {
                        Col = Color.Red;
                        if (scanInfo?.LastScan is DateTime lastScan)
                            textBoxScanLast.Text = lastScan.ToLocalTime().ToString();
                    }
                }

                panelScanNext.BackColor = Col;

            }
            catch (Exception E)
            {
                LogException(E);
            }
            finally
            {
                if (cPerformanceLogger.IsActive && requestInfo != null) { cPerformanceLogger.LogPerformanceEnd(LogDeep, CM, requestInfo.id, requestInfo.created, _startTime); }
                if (CM != null) LogMethodEnd(CM);
            }
        }

        private async void buttonScanClientAgent_Click(object sender, EventArgs e)
        {
            MethodBase CM = null; if (cLogManager.DefaultLogger.IsDebug) { CM = MethodBase.GetCurrentMethod(); LogMethodBegin(CM); }

            if (Program.Collector == null)
                return;

            try
            {
                Cursor = Cursors.WaitCursor;

                ctrlProtocolBox1.ClearMessages();
                buttonScanClientAgent.Enabled = false;

                Program.mainForm.DisableAllTabPages(this);

                Program.Collector.CleanupUiMessageHandler();
                Program.Collector.ProcessUiMessage += ctrlProtocolBox1.ProcessMessage;
                var _requestInfo = new cF4sdWebRequestInfo("buttonScanClientAgent_Click", null);
                await Program.Collector.F4sdAgent.DoScanAsync(true, true, _requestInfo, 1, CancellationToken.None);

                await checkScanTimesAsync(_requestInfo, 1);
            }
            catch (Exception E)
            {
                LogException(E);
            }
            finally
            {
                ctrlProtocolBox1.ProcessMessage("");
                Program.mainForm.EnableAllTabPages();
                buttonScanClientAgent.Enabled = true;
                Cursor = Cursors.Default;
                if (CM != null) LogMethodEnd(CM);
            }
        }

    }

}
