using System;
using System.Windows.Forms;
using System.Threading;

using System.Reflection;

using C4IT.Logging;
using C4IT.DataHistoryProvider;
using C4IT.DataHistoryProvider.Base.Modules.Citrix;

using static C4IT.Logging.cLogManager;

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

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



                if (Program.Collector == null)
                    return;


                Program.Collector.CleanupUiMessageHandler();
                Program.Collector.ProcessUiMessage += ctrlProtocolBox1.ProcessMessage;
                var _requestInfo = new cF4sdWebRequestInfo("btnConnectCitrix_Click", null);
                var systems = Program.Collector.InfrastructureConfig.Citrix.Systems;

                ctrlProtocolBox1.ProcessMessage($"Validating Citrix connections...");

                await Program.Collector.CitrixCollector.ValidateConnectionsAsync(CancellationToken.None);

                ctrlProtocolBox1.ProcessMessage($"Validating Citrix connections finished.");

                return;
            }
            catch (Exception E)
            {
                LogException(E);
            }
            finally
            {
                if (CM != null) LogMethodEnd(CM);
                Cursor = Cursors.Default;
            }
        }

        public async void btnGetUsers_Click(object sender, EventArgs e)
        {
            MethodBase CM = null; if (cLogManager.DefaultLogger.IsDebug) { CM = MethodBase.GetCurrentMethod(); LogMethodBegin(CM); }
            try
            {
                Cursor = Cursors.WaitCursor;



                if (Program.Collector == null)
                    return;


                Program.Collector.CleanupUiMessageHandler();
                Program.Collector.ProcessUiMessage += ctrlProtocolBox1.ProcessMessage;
                var _requestInfo = new cF4sdWebRequestInfo("btnConnectCitrix_Click", null);

            }
            catch (Exception E)
            {
                LogException(E);
            }
            finally
            {
                if (CM != null) LogMethodEnd(CM);
                Cursor = Cursors.Default;
            }
        }

        public async void btnGetSessions_Click(object sender, EventArgs e)
        {
            MethodBase CM = null; if (cLogManager.DefaultLogger.IsDebug) { CM = MethodBase.GetCurrentMethod(); LogMethodBegin(CM); }
            try
            {
                Cursor = Cursors.WaitCursor;



                if (Program.Collector == null)
                    return;


                Program.Collector.CleanupUiMessageHandler();
                Program.Collector.ProcessUiMessage += ctrlProtocolBox1.ProcessMessage;
                var _requestInfo = new cF4sdWebRequestInfo("btnGetSessions_Click", null);
            }
            catch (Exception E)
            {
                LogException(E);
            }
            finally
            {
                if (CM != null) LogMethodEnd(CM);
                Cursor = Cursors.Default;
            }
        }

        private async void btnScanUsers_Click(object sender, EventArgs e)
        {
            if (Program.Collector == null || Program.Collector.ActiveDirectory == null)
                return;
            var requestInfo = new cF4sdWebRequestInfo("buttonUserCitrix_Click", null);
            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(1, CM, requestInfo.id, requestInfo.created); }
            var _startTime = DateTime.UtcNow;
            try
            {
                Cursor = Cursors.WaitCursor;

                if (Program.Collector == null)
                    return;


                Program.Collector.CleanupUiMessageHandler();
                Program.Collector.ProcessUiMessage += ctrlProtocolBox1.ProcessMessage;
                var _requestInfo = new cF4sdWebRequestInfo("btnScanUsers_Click", null);

                ctrlProtocolBox1.ProcessMessage($"Starting Citrix user scans...");

                Program.Collector.CitrixCollector.DoScanSequential = true;
                var result = await Program.Collector.CitrixCollector.DoScanAsync(true, true, _requestInfo, 0, CancellationToken.None);

                if (result)
                    ctrlProtocolBox1.ProcessMessage($"All Citrix user scans finished successfully.");
                else
                    ctrlProtocolBox1.ProcessMessage($"At least one Citrix user scan finished with errors.");
            }
            catch (Exception E)
            {
                LogException(E);
            }
            finally
            {
                if (CM != null) LogMethodEnd(CM);
                Cursor = Cursors.Default;
            }
        }
    }
    
}
