using C4IT.FASD.Base;
using C4IT.MultiLanguage;
using FasdDesktopUi.Basics.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using static C4IT.Logging.cLogManager;

namespace FasdDesktopUi.Basics.Services.SupportCase.Controllers
{
    internal class SupportCaseHeadingController
    {
        internal static IEnumerable<cHeadingDataModel> GetHeadingData(Dictionary<enumFasdInformationClass, cF4sdApiSearchResultRelation> relations)
        {
            try
            {
                return Enum.GetValues(typeof(enumFasdInformationClass))
                    .Cast<enumFasdInformationClass>()
                    .Select(informationClass =>
                    {
                        if (relations.TryGetValue(informationClass, out var relation) && relation != null)
                            return GetHeadingDataFor(relation);
                        else
                            return GetEmptyHeadingDataFor(informationClass);
                    });
            }
            catch (Exception ex)
            {
                LogException(ex);
            }

            return Enumerable.Empty<cHeadingDataModel>();
        }

        private static cHeadingDataModel GetEmptyHeadingDataFor(enumFasdInformationClass informationClass)
        {
            string headingText = string.Empty;

            switch (informationClass)
            {
                case enumFasdInformationClass.Computer:
                    headingText = cMultiLanguageSupport.GetItem("Header.Select.Computer");
                    break;
                case enumFasdInformationClass.Ticket:
                    headingText = cMultiLanguageSupport.GetItem("Header.Select.Ticket");
                    break;
                case enumFasdInformationClass.VirtualSession:
                    headingText = cMultiLanguageSupport.GetItem("Header.Select.VirtualSession");
                    break;
                case enumFasdInformationClass.MobileDevice:
                    headingText = cMultiLanguageSupport.GetItem("Header.Select.MobileDevice");
                    break;
            }

            return new cHeadingDataModel() { InformationClass = informationClass, HeadingText = headingText };
        }

        private static cHeadingDataModel GetHeadingDataFor(cF4sdApiSearchResultRelation relation)
        {
            const string StatusString = "Status";
            bool isOnline = false;

            string statusValue = "Unknown";
            if (relation?.Infos?.TryGetValue(StatusString, out var statusValueFromDictionary) ?? false)
                statusValue = statusValueFromDictionary;

            switch (relation.Type)
            {
                case enumF4sdSearchResultClass.Computer:
                case enumF4sdSearchResultClass.User:
                    isOnline = string.Equals(statusValue, "Online", StringComparison.InvariantCultureIgnoreCase);
                    break;
                case enumF4sdSearchResultClass.Ticket:
                    isOnline = !string.Equals(statusValue, nameof(enumTicketStatus.Closed));
                    break;
                case enumF4sdSearchResultClass.VirtualSession:
                    isOnline = string.Equals(statusValue, nameof(enumCitrixSessionStatus.Active));
                    break;
                case enumF4sdSearchResultClass.MobileDevice:
                    break;
            }

            return new cHeadingDataModel()
            {
                HeadingText = string.IsNullOrEmpty(relation.Name) ? relation.DisplayName : relation.Name,
                InformationClass = cF4sdIdentityEntry.GetFromSearchResult(relation.Type),
                IsOnline = isOnline,
                Identities = relation.Identities,
                Realtion = relation
            };
        }
    }
}
