using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Web.Caching;
using System.Windows;
using System.Windows.Controls;
using FasdDesktopUi.Basics.Helper;
using FasdDesktopUi.Basics.Models;
using FasdDesktopUi.Basics.UserControls;
using FasdDesktopUi.Config;
using FasdDesktopUi.Pages.DetailsPage;
using FasdDesktopUi.Pages.DetailsPage.ViewModels;
using static C4IT.Logging.cLogManager;

namespace FasdDesktopUi.Basics.UiActions
{
    public class cShowCustomDialog : cUiActionBase
    {
        private string dialogName;

        public cShowCustomDialog(string dialogName)
        {
            this.dialogName = dialogName;
        }

        private List<cContainerData> GetDialogDataByName(string name, cSupportCaseDataProvider dataProvider)
        {
            List<cContainerData> output = null;

            try
            {
                if (string.IsNullOrEmpty(name) || dataProvider is null)
                    return null;

                if (cF4SDCustomDialogConfig.Instance?.CustomDialogContainers is null)
                    return null;

                if (cF4SDCustomDialogConfig.Instance.CustomDialogContainers.TryGetValue(name, out var containerConfig))
                    output = dataProvider.HealthCardDataHelper.CustomizableSectionHelper.GetContainerDataFromConfig(containerConfig);
            }
            catch (Exception E)
            {
                LogException(E);
            }

            return output;
        }

        public override async Task<bool> RunUiActionAsync(object sender, UIElement UiLocation, bool isDetailedLayout, cSupportCaseDataProvider dataProvider)
        {
            try
            {
                var dialogData = GetDialogDataByName(dialogName, dataProvider);

                if (dialogData is null)
                    return false;

                var dialogElement = new CustomDialog() { ContainerData = dialogData.FirstOrDefault(), HasYesNoButtons = true, HasYesNoText = true };

                if (UiLocation is Decorator locationElement)
                {
                    dialogElement.ParentElement = locationElement;
                    locationElement.Child = dialogElement;
                }

                if (cSupportCaseDataProvider.detailsPage?.QuickActionDecorator?.Child is null)
                    return false;

                cSupportCaseDataProvider.detailsPage.QuickActionDecorator.Child = dialogElement;
                cSupportCaseDataProvider.detailsPage.QuickActionDecorator.Visibility = Visibility.Visible;

                await Task.CompletedTask;

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

            return false;
        }
    }
}
