using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Threading;

using FasdCockpitBase.Models;
using FasdDesktopUi.Basics.Models;
using FasdDesktopUi.Basics.UserControls;
using FasdDesktopUi.Basics.UserControls.AdaptableIcon;

using C4IT.FASD.Base;
using static C4IT.Logging.cLogManager;
using C4IT.Logging;
using C4IT.F4SD.DisplayFormatting;

namespace FasdDesktopUi.Basics.UiActions
{
    public abstract class cUiQuickAction : cUiActionBase
    {
        private readonly bool shouldRunImmidiate = false;

        public cFasdQuickAction QuickActionConfig { get; private set; }

        public cSupportCaseDataProvider DataProvider { get; set; }

        public QuickActionStatusMonitor StatusMonitor { get; set; }

        public cRecommendationDataModel QuickActionRecommendation { get; set; }


        public cUiQuickAction(cFasdQuickAction QuickActionConfig)
        {
            try
            {
                if (QuickActionConfig is null)
                    return;

                cUtility.RawValueFormatter.SetDefaultCulture(new System.Globalization.CultureInfo(cFasdCockpitConfig.Instance.SelectedLanguage));
                cUtility.RawValueFormatter.SetDefaultTimeZone(TimeZoneInfo.Local);

                shouldRunImmidiate = QuickActionConfig.RunImmediate;
                Description = QuickActionConfig.Descriptions?.GetValue(Default: null);
                AlternativeDescription = QuickActionConfig.AlternativeDescriptions?.GetValue(Default: null);
                this.QuickActionConfig = QuickActionConfig;
            }
            catch (Exception E)
            {
                LogException(E);
            }
        }

        public virtual async Task<cQuickActionStatusMonitorModel> GetQuickActionStatusMonitorDataAsync()
        {
            await Task.CompletedTask;
            return new cQuickActionStatusMonitorModel();
        }

        public abstract Task<cDataCanvasDataModel> GetQuickActionDataAsync(cSupportCaseDataProvider dataProvider, bool isDetailedLayout);

        public abstract Task<List<object>> ProcessActionAsync(CancellationToken token, Dictionary<cAdjustableParameter, object> ParameterDictionary = null);

        private void DrawUiActionControls(cDataCanvasDataModel quickActionData, UIElement UiLocation, bool isDetailedLayout)
        {
            if (!(UiLocation is Decorator decorator))
                return;

            if (decorator.Child is DataCanvas existinDataCanvas)
            {
                existinDataCanvas.DataCanvasData = quickActionData;
                existinDataCanvas.DataProvider = DataProvider;
                existinDataCanvas.IsDetailedLayout = isDetailedLayout;
                StatusMonitor = existinDataCanvas.QuickActionStatusUc;
                existinDataCanvas.Visibility = Visibility.Visible;
            }
            else
            {
                DataCanvas dataCanvas = new DataCanvas(isDetailedLayout) { DataCanvasData = quickActionData, IsDetailedLayout = isDetailedLayout, DataProvider = DataProvider };
                decorator.Child = dataCanvas;
                StatusMonitor = dataCanvas.QuickActionStatusUc;
            }

            StatusMonitor.ExecuteButton.IsHitTestVisible = true;
            decorator.Visibility = Visibility.Visible;
        }

        public override async Task<bool> RunUiActionAsync(object sender, UIElement UiLocation, bool isDetailedLayout, cSupportCaseDataProvider dataProvider)
        {
            try
            {
                var quickActionData = await GetQuickActionDataAsync(dataProvider, isDetailedLayout);
                DrawUiActionControls(quickActionData, UiLocation, isDetailedLayout);

                if (shouldRunImmidiate)
                    if (UiLocation is Decorator uiDecorator)
                        if (uiDecorator.Child is DataCanvas dataCanvas)
                            await dataCanvas.QuickActionStatusUc.RunQuickActionAsync();
                return true;
            }
            catch (Exception E)
            {
                LogException(E);
            }

            return false;
        }
    }
}
