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


using static C4IT.Logging.cLogManager;
using System.Threading;
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 tenants = Program.Collector.InfrastructureConfig.CitrixTenants?.Values;


                foreach (var Tenant in tenants)
                {
                    var result = await Program.Collector.CitrixCollector.GetCitrixCommunicationForTenantAsync(Tenant.TenantID, false);
                    if (result != null)
                    {
                        ctrlProtocolBox1.ProcessMessage($"Citrix connection for tenant {Tenant.TenantID} successful");
                    }
                    else
                    {
                        ctrlProtocolBox1.ProcessMessage($"Citrix connection for tenant {Tenant.TenantID} failed");

                    }
                }

            }
            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);
                var tenants = Program.Collector.InfrastructureConfig.CitrixTenants?.Values;


                foreach (var Tenant in tenants)
                {
                    var result = await Program.Collector.CitrixCollector.GetCitrixUsersAsync();
                    if (result != null)
                    {
                        ctrlProtocolBox1.ProcessMessage($"Citrix connection for tenant {Tenant.TenantID} successful");
                    }
                    else
                    {
                        ctrlProtocolBox1.ProcessMessage($"Citrix connection for tenant {Tenant.TenantID} failed");

                    }
                }

            }
            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);
                var tenants = Program.Collector.InfrastructureConfig.CitrixTenants?.Values;


                foreach (var Tenant in tenants)
                {
                    var result = await Program.Collector.CitrixCollector.GetCitrixSessionsAsync();
                    if (result != null)
                    {
                        ctrlProtocolBox1.ProcessMessage($"Citrix connection for tenant {Tenant.TenantID} successful");
                    }
                    else
                    {
                        ctrlProtocolBox1.ProcessMessage($"Citrix connection for tenant {Tenant.TenantID} failed");

                    }
                }

            }
            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);
                var tenants = Program.Collector.InfrastructureConfig.CitrixTenants?.Values;


                foreach (var Tenant in tenants)
                {
                    var result = await Program.Collector.CitrixCollector.DoScanAsync(true, true, _requestInfo, 1, CancellationToken.None);
                    if (result)
                    {
                        ctrlProtocolBox1.ProcessMessage($"Citrix connection for tenant {Tenant.TenantID} successful");
                    }
                    else
                    {
                        ctrlProtocolBox1.ProcessMessage($"Citrix connection for tenant {Tenant.TenantID} failed");

                    }
                }

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