using C4IT.Logging;
using C4IT.MultiLanguage;
using FasdDesktopUi.Basics.Services.RelationService;
using System;
using System.Diagnostics;
using System.Reflection;
using System.Threading.Tasks;
using System.Windows;
using static C4IT.Logging.cLogManager;

namespace FasdDesktopUi.Basics.UiActions
{
    internal class cUiProcessSearchHistoryEntry : cUiActionBase
    {
        public cSearchHistoryEntryBase HistoryEntry { get; private set; }

        public cUiProcessSearchHistoryEntry(cSearchHistoryEntryBase historyEntry)
        {
            HistoryEntry = historyEntry;
        }

        public override async Task<bool> RunUiActionAsync(object sender, UIElement UiLocation, bool isDetailedLayout, cSupportCaseDataProvider dataProvider)
        {
            var CM = MethodBase.GetCurrentMethod();
            LogMethodBegin(CM);
            try
            {
                cSearchManager.Instance.ReplaceEntry(HistoryEntry, HistoryEntry);
                HistoryEntry.SearchUiProvider.SetSearchHistoryVisibility(false);
                if (HistoryEntry.ShouldRefresh())
                {
                    HistoryEntry.SearchUiProvider.ShowLoadingTextItem(cMultiLanguageSupport.GetItem("Searchbar.Refresh.Relations"));
                    await HistoryEntry.RefreshAsync();
                }

                // todo set relationService
                IRelationService relationService = new RelationService();

                switch (HistoryEntry)
                {
                    case cSearchHistorySearchResultEntry SearchEntry:
                        HistoryEntry.SearchUiProvider.SetSearchHistoryVisibility(true);
                        HistoryEntry.SearchUiProvider.ShowSearchRelations(SearchEntry, relationService, HistoryEntry.SearchUiProvider);
                        return true;
                    case cSearchHistoryRelationEntry RelationEntry:
                        string caseObjectName = RelationEntry.SelectedRelation.DisplayName;
                        string loadingStatusText = string.Format(cMultiLanguageSupport.GetItem("Searchbar.Loading.CaseData"), caseObjectName);
                        RelationEntry.SearchUiProvider.ShowLoadingTextItem(loadingStatusText);

                        // check, if  have an active support case
                        var supportCaseActive = dataProvider?.IsActive ?? false;
                        if (supportCaseActive)
                        {
                            if (!await cSupportCaseDataProvider.SupportTicketActiveNoticeAsync())
                            {
                                HistoryEntry.isSeen = false;
                                return false;
                            }
                        }
                        HistoryEntry.isSeen = true;
                        dataProvider = await cSupportCaseDataProvider.GetDataProviderForAsync(RelationEntry.SelectedRelation, relationService);
                        if (dataProvider is null)
                        {
                            Debug.Assert(true, "Could not start a data provider for the selected criterias.");
                            LogEntry("Could not start a data provider for the selected criterias.", LogLevels.Error);
                        }
                        return false;
                }
            }
            catch (Exception E)
            {
                LogException(E);
            }
            finally
            {
                LogMethodEnd(CM);
            }

            return false;
        }
    }
}
