using C4IT.FASD.Base;
using C4IT.FASD.Cockpit.Communication;
using C4IT.MultiLanguage;
using FasdDesktopUi.Basics.Models;
using FasdDesktopUi.Basics.UserControls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using static C4IT.Logging.cLogManager;

namespace FasdDesktopUi.Basics.Helper
{
    public class cQuickActionProtocollHelper
    {
        private readonly cSupportCaseDataProvider dataProvider;

        public cQuickActionProtocollHelper(cSupportCaseDataProvider dataProvider)
        {
            this.dataProvider = dataProvider;
        }

        public async Task<List<List<object>>> GetQuickActionHistoryAsync(string QuickActionName)
        {
            try
            {
                int agentDeviceId = int.MinValue;
                int? agentUserIdNullable = null;

                if (dataProvider.NamedParameterEntries.TryGetValue("AgentDeviceId", out var agentDeviceIdParameter))
                    _ = int.TryParse(agentDeviceIdParameter.GetValue(), out agentDeviceId);

                if (dataProvider.NamedParameterEntries.TryGetValue("AgentUserId", out var agentUserIdParameter))
                    if (int.TryParse(agentUserIdParameter.GetValue(), out int agentUserId))
                        agentUserIdNullable = agentUserId;

                var output = await cFasdCockpitCommunicationBase.Instance.GetQuickActionHistory(QuickActionName, cCockpitConfiguration.Instance.agentApiConfiguration.OrganizationCode.Value, agentDeviceId, agentUserIdNullable);

                foreach (var historyLine in output)
                {
                    var historyStatus = historyLine.FirstOrDefault();
                    if (historyStatus is null || !(historyStatus is enumQuickActionRevisionStatus revisionStatus))
                        continue;

                    switch (revisionStatus)
                    {
                        case enumQuickActionRevisionStatus.unknown:
                            historyLine[0] = cMultiLanguageSupport.GetItem("QuickAction.Revision.Status.Unknown");
                            break;
                        case enumQuickActionRevisionStatus.inProgress:
                            historyLine[0] = cMultiLanguageSupport.GetItem("QuickAction.Revision.Status.InProgress");
                            break;
                        case enumQuickActionRevisionStatus.finishedSuccessfull:
                            historyLine[0] = cMultiLanguageSupport.GetItem("QuickAction.Revision.Status.FinishedSuccessfull");
                            break;
                        case enumQuickActionRevisionStatus.finishedWithError:
                            historyLine[0] = cMultiLanguageSupport.GetItem("QuickAction.Revision.Status.FinishedWithError");
                            break;
                    }
                }

                output.Reverse();
                return output;
            }
            catch (Exception E)
            {
                LogException(E);
            }

            return null;
        }
    }
}
