using C4IT.FASD.Base;
using F4SD_AdaptableIcon;
using FasdDesktopUi.Basics.Converter;
using FasdDesktopUi.Basics.Enums;
using FasdDesktopUi.Basics.Helper;
using FasdDesktopUi.Basics.Models;
using FasdDesktopUi.Basics.UserControls.QuickTip;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using static C4IT.Logging.cLogManager;

namespace FasdDesktopUi.Basics.UiActions
{
    public class cUiQuickTipAction : cUiActionBase
    {
        public cSupportCaseDataProvider DataProvider { get; set; }

        private readonly cFasdQuickTip _quickTip;

        private readonly List<cUiQuickTipElement> _quickTipElements = new List<cUiQuickTipElement>();

        public cUiQuickTipAction(cFasdQuickTip quickTip)
        {
            _quickTip = quickTip;
        }

        public override async Task<bool> RunUiActionAsync(object sender, UIElement UiLocation, bool isDetailedLayout, cSupportCaseDataProvider dataProvider)
        {
            try
            {
                if (!(UiLocation is QuickTipStatusMonitor quickTipStatusMonitor))
                    return false;

                DataProvider = dataProvider;

                CreateElementData();

                quickTipStatusMonitor.QuickTipElementData = _quickTipElements;

                quickTipStatusMonitor.QuickTipName = _quickTip.Names.GetValue();

                if (_quickTip.Icon is null)
                    quickTipStatusMonitor.QuickTipIcon = new IconData(MaterialIcons.MaterialIconType.ic_lightbulb_outline);
                else
                    quickTipStatusMonitor.QuickTipIcon = IconDataConverter.Convert(_quickTip.Icon);

                quickTipStatusMonitor.Visibility = Visibility.Visible;

                return true;
            }
            catch (Exception E)
            {
                LogException(E);
            }
            return false;
        }

        private void CreateElementData()
        {
            _quickTipElements.Clear();

            foreach (cQuickTipElement element in _quickTip.QuickTipElements)
            {
                switch (element)
                {
                    case cManualStep manualStep:
                        _quickTipElements.Add(new cUiQuickTipElement(manualStep, enumQuickTipElementType.Manual, DataProvider));
                        break;
                    case cAutomatedStep automatedStep:
                        _quickTipElements.Add(new cUiQuickTipElement(automatedStep, enumQuickTipElementType.Automated, DataProvider));
                        break;
                    case cTextElement textElement:
                        _quickTipElements.Add(new cUiQuickTipElement(textElement, enumQuickTipElementType.Text, DataProvider));
                        break;
                    default:
                        break;
                }
            }
        }

        public class cUiQuickTipElement
        {
            public enumQuickTipElementType ElementType { get; set; }

            public string ElementName { get; set; }

            public string TextBlock { get; set; }

            public bool IsRequired { get; set; }

            public IconData Icon { get; set; }

            public cQuickTipElement QuickTipElementDefinition { get; set; }

            public cUiActionBase StepUiAction { get; set; }

            public cSupportCaseDataProvider DataProvider { get; set; }

            public cUiQuickTipElement(cSupportCaseDataProvider dataProvider)
            {
                DataProvider = dataProvider;
            }

            public cUiQuickTipElement(cQuickTipElement element, enumQuickTipElementType type, cSupportCaseDataProvider dataProvider)
            {
                DataProvider = dataProvider;
                ElementType = type;
                QuickTipElementDefinition = element;
                ElementName = element.Names.GetValue();
                TextBlock = element.TextBlocks.GetValue();
                IsRequired = element.IsRequired;

                if (element is cTextElement)
                    return;

                if (element is cAutomatedStep automatedStep)
                    SetValues(automatedStep);

                if (element is cManualStep manualStep)
                    SetValues(manualStep);
            }

            private void SetValues(cAutomatedStep automatedStep)
            {
                cF4SDCockpitXmlConfig.Instance.MenuItems.TryGetValue(automatedStep.QuickAction, out cFasdBaseConfigMenuItem test);

                MenuItemDataProvider _menuDataProvider = new MenuItemDataProvider(DataProvider);

                cMenuDataBase uiAction = _menuDataProvider.GetMenuItemData()
                    .Where(data => automatedStep.QuickAction == data.UiAction.Name && data.UiAction.DisplayType != enumActionDisplayType.hidden)
                    .FirstOrDefault();

                if (uiAction != null && uiAction.UiAction is cUiQuickAction elementQuickAction)
                    StepUiAction = elementQuickAction;
            }

            private void SetValues(cManualStep manualStep)
            {
                StepUiAction = new cShowRecommendationAction(ElementName, manualStep.Summaries.GetValue());
                Icon = IconDataConverter.Convert(manualStep.Icon);
            }
        }
    }
}
