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 ctrlCleanup : UserControl
    {
        public ctrlCleanup()
        {
            InitializeComponent();
        }

        private void CheckInTime()
        {
            if (Program.Collector != null)
            {
                var InTime = Program.Collector.CheckCleanupTimeframes();
                if (InTime)
                    panelValidTime.BackColor = Color.Green;
                else
                    panelValidTime.BackColor = Color.Red;
            }
        }

        private void ctrlCleanup_Load(object sender, EventArgs e)
        {
            CheckInTime();
        }

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

                Cursor = Cursors.WaitCursor;
                this.Enabled = false;
                textBoxCleanupsCount.Text = "-";

                var _requestInfo = new cF4sdWebRequestInfo("buttonScanCleanups_Click", null);
                var res = await Program.Collector.CheckCleanup(_requestInfo, 1, CancellationToken.None);
                textBoxCleanupsCount.Text = res.ToString();
                buttonDoCleanups.Enabled = res > 0;
            }
            catch (Exception E)
            {
                LogException(E);
            }
            finally
            {
                this.Enabled = true;
                Cursor = Cursors.Default;
                if (CM != null) LogMethodEnd(CM);
            }
        }

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

                Cursor = Cursors.WaitCursor;
                this.Enabled = false;

                var _requestInfo = new cF4sdWebRequestInfo("buttonDoCleanups_Click", null);
                var res = await Program.Collector.DoCleanup(true, _requestInfo, 1, CancellationToken.None);
                if (res < 0)
                    ctrlProtocolBox1.ProcessMessage($"error while removing history scan data.");
                else
                    ctrlProtocolBox1.ProcessMessage($"{res} history scans removed successful");

                var res2 = await Program.Collector.CheckCleanup(_requestInfo, 1, CancellationToken.None);

                textBoxCleanupsCount.Text = res2.ToString();
                buttonDoCleanups.Enabled = res2 > 0;
            }
            catch (Exception E)
            {
                LogException(E);
            }
            finally
            {
                this.Enabled = true;
                Cursor = Cursors.Default;
                if (CM != null) LogMethodEnd(CM);
            }
        }
    }
}
