using C4IT.FASD.Base;
using FasdDesktopUi.Basics.Enums;
using System;
using System.Threading.Tasks;
using System.Windows;
using static C4IT.Logging.cLogManager;

namespace FasdDesktopUi.Basics.UiActions
{
    public abstract class cUiActionBase
    {
        public string Name { get; set; }
        public string Description { get; set; }
        public string AlternativeDescription { get; set; }
        public enumActionDisplayType DisplayType { get; set; }

        public static cUiActionBase GetUiAction(cFasdQuickAction quickAction)
        {
            try
            {
                if (quickAction is cF4sdQuickActionLocalWebRequest webRequestQuickAction)
                    return new cUiLocalWebRequestQuickAction(webRequestQuickAction);
                else if (quickAction is cFasdQuickActionLocal localQuickAction)
                    return new cUiLocalQuickAction(localQuickAction);
                else if (quickAction is cF4sdQuickActionRemote remoteQuickAction)
                    return new cUiRemoteQuickAction(remoteQuickAction);
                else if (quickAction is cF4sdQuickActionChained chainQuickAction)
                    return new cUiChainQuickAction(chainQuickAction);
                else if (quickAction is cF4sdQuickActionDemo QuickActionDemo)
                    return new cUiDemoQuickAction(QuickActionDemo);
                else if (quickAction is cF4sdQuickActionServer serverQuickAction)
                    return new cUiServerQuickAction(serverQuickAction);
                else
                    return new cUiDummyQuickAction(quickAction);
            }
            catch (Exception E)
            {
                LogException(E);
            }

            return null;
        }

        public abstract Task<bool> RunUiActionAsync(object sender, UIElement UiLocation, bool isDetailedLayout, cSupportCaseDataProvider dataProvider);

        public delegate void UiActionEventHandlerDelegate(object sender, UiActionEventArgs args);

        public static readonly RoutedEvent UiActionClickedEvent = EventManager.RegisterRoutedEvent("UiActionClicked", RoutingStrategy.Bubble, typeof(UiActionEventHandlerDelegate), typeof(UIElement));

        public static void RaiseEvent(cUiActionBase uiAction, UIElement uiElement, object sender)
        {
            uiElement.RaiseEvent(new UiActionEventArgs(UiActionClickedEvent, sender) { UiAction = uiAction });
        }
    }

    public class UiActionEventArgs : RoutedEventArgs
    {
        public cUiActionBase UiAction { get; set; }

        public UiActionEventArgs(RoutedEvent routedEvent, object source) : base(routedEvent, source)
        {
        }
    }
}
