using System;
using System.Collections.Generic;
using System.Linq;
using C4IT.FASD.Base;
using F4SD_AdaptableIcon;
using F4SD_AdaptableIcon.Enums;
using FasdDesktopUi.Basics.Converter;
using FasdDesktopUi.Basics.UiActions;
using static C4IT.Logging.cLogManager;

namespace FasdDesktopUi.Basics.Models
{
    //todo: check what properties can/should be set via constructor

    public class cMenuDataBase
    {
        public string MenuText { get; set; }

        public string SubMenuText { get; set; }

        public string TrailingText { get; set; }

        public IconData MenuIcon { get; set; }

        public double MenuIconSize { get; set; } = 1.0;

        public int IconPositionIndex { get; set; }

        public object Data { get; set; }

        public List<string> MenuSections { get; set; }

        public cUiActionBase UiAction { get; set; }

        public cMenuDataBase()
        {
        }
        public cMenuDataBase(cFasdBaseConfigMenuItem menuItem)
        {
            MenuText = menuItem.Names.GetValue(Default: null);
            MenuIcon = IconDataConverter.Convert(menuItem.Icon);
            MenuSections = menuItem.Sections;
        }

        public cMenuDataBase(cFasdQuickAction quickAction, Enums.enumActionDisplayType display) : this(quickAction)
        {
            SetUiActionDisplayType(display);
        }


        public cMenuDataBase(cFasdQuickAction quickAction) : this((cFasdBaseConfigMenuItem)quickAction)
        {
            var tempUiAction = cUiActionBase.GetUiAction(quickAction);
            tempUiAction.Name = quickAction.Name;
            tempUiAction.Description = quickAction.Descriptions?.GetValue(Default: null);
            tempUiAction.AlternativeDescription = quickAction.AlternativeDescriptions?.GetValue(Default: null);
            UiAction = tempUiAction;
        }

        public cMenuDataBase(cCopyTemplate copyTemplate) : this((cFasdBaseConfigMenuItem)copyTemplate)
        {
            UiAction = new cUiCopyAction(copyTemplate) { Name = copyTemplate.Name, Description = copyTemplate.Descriptions.GetValue(Default: null), DisplayType = Enums.enumActionDisplayType.enabled };
        }

        public cMenuDataBase(cFasdMenuSection menuSection) : this((cFasdBaseConfigMenuItem)menuSection)
        {
            IconPositionIndex = -1;
        }

        public cMenuDataBase(cFasdQuickTip quickTip) : this((cFasdBaseConfigMenuItem)quickTip)
        {
            UiAction = new cUiQuickTipAction(quickTip) { DisplayType = Enums.enumActionDisplayType.enabled };
        }

        public void SetUiActionDisplayType(Enums.enumActionDisplayType display)
        {
            if (this.UiAction != null)
                this.UiAction.DisplayType = display;
        }

    }

    public class cMenuDataContainer : cMenuDataBase
    {
        public string ContainerName { get; private set; }
        public List<cMenuDataBase> SubMenuData { get; set; }

        public cMenuDataContainer()
        {
            UiAction = new cSubMenuAction(true) { Name = MenuText, DisplayType = Enums.enumActionDisplayType.enabled };
        }

        public cMenuDataContainer(cFasdMenuSection menuSection) : base(menuSection)
        {
            ContainerName = menuSection.TechName;
            UiAction = new cSubMenuAction(true) { Name = MenuText, DisplayType = Enums.enumActionDisplayType.enabled };
        }
    }

    public class cMenuDataSearchResult : cMenuDataBase
    {
        public cMenuDataSearchResult(string menuText, ISearchUiProvider SearchUiProvider, List<cFasdApiSearchResultEntry> searchResults) : base()
        {
            if (searchResults?.Count <= 0)
                return;

            MenuText = menuText;
            UiAction = new cUiProcessSearchResultAction(menuText, SearchUiProvider, searchResults);
            var firstSearchResult = searchResults.First();
            MenuIcon = GetMenuIcon(firstSearchResult.Type, firstSearchResult.Infos);
        }

        static public IconData GetMenuIcon(enumF4sdSearchResultClass searchResultClass, Dictionary<string, string> infos)
        {
            switch (searchResultClass)
            {
                case enumF4sdSearchResultClass.Computer:
                    return new IconData(enumInternIcons.misc_computer);
                case enumF4sdSearchResultClass.User:
                    return new IconData(enumInternIcons.misc_user);
                case enumF4sdSearchResultClass.Phone:
                    return new IconData(MaterialIcons.MaterialIconType.ic_phone);
                case enumF4sdSearchResultClass.Ticket:
                    return new IconData(enumInternIcons.misc_ticket);
                case enumF4sdSearchResultClass.MobileDevice:
                    return new IconData(MaterialIcons.MaterialIconType.ic_smartphone);
                case enumF4sdSearchResultClass.VirtualSession:
                    if (!infos.TryGetValue("Status", out string status))
                        return new IconData(MaterialIcons.MaterialIconType.ic_cloud_off);
                    else if (status == nameof(enumCitrixSessionStatus.Active))
                        return new IconData(MaterialIcons.MaterialIconType.ic_cloud_queue);
                    else
                        return new IconData(MaterialIcons.MaterialIconType.ic_cloud_off);
                default:
                    return new IconData(MaterialIcons.MaterialIconType.ic_more_vert);
            }
        }
    }

    public sealed class cMenuDataSearchRelation : cMenuDataBase
    {
        public readonly DateTime LastUsed;
        public readonly double UsingLevel = 0;
        public readonly Dictionary<string, string> Infos = null;
        public bool IsMatchingRelation = false;
        public bool IsUsedForCaseEnrichment = false;

        public readonly cF4sdApiSearchResultRelation searchResultRelation = null;

        public cMenuDataSearchRelation(cF4sdApiSearchResultRelation searchResultRelation)
        {
            try
            {
                UiAction = null;

                if (searchResultRelation is null)
                    return;

                this.searchResultRelation = searchResultRelation;
                MenuText = searchResultRelation.DisplayName;
                Data = searchResultRelation;
                LastUsed = searchResultRelation.LastUsed;
                UsingLevel = searchResultRelation.UsingLevel;
                Infos = searchResultRelation.Infos;
                MenuIcon = cMenuDataSearchResult.GetMenuIcon(searchResultRelation.Type, searchResultRelation.Infos);
            }
            catch (Exception E)
            {
                LogException(E);
            }
        }
    }

    public sealed class cMenuDataLoading : cMenuDataBase
    {
        public cMenuDataLoading(string LoadingText)
        {
            MenuText = LoadingText;
        }
    }

    public class cFilteredResults
    {
        public bool AutoContinue { get; set; } = false;
        public cFasdApiSearchResultCollection Results { get; set; }

        public cF4sdApiSearchResultRelation PreSelectedRelation { get; set; }

        public cFilteredResults()
        {

        }

        public cFilteredResults(cFasdApiSearchResultCollection _results)
        {
            Results = _results ?? new cFasdApiSearchResultCollection();
        }
    }
}
