using C4IT.FASD.Base;
using C4IT.MultiLanguage;
using F4SD_AdaptableIcon;
using F4SD_AdaptableIcon.Enums;
using FasdDesktopUi.Basics.CustomEvents;
using FasdDesktopUi.Basics.Helper;
using FasdDesktopUi.Basics.Models;
using FasdDesktopUi.Basics.UiActions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;

namespace FasdDesktopUi.Basics.UserControls.SearchResult
{
    public partial class SearchResultCategory : UserControl
    {
        public string Title
        {
            get { return (string)GetValue(TitleProperty); }
            set { SetValue(TitleProperty, value); }
        }

        public static readonly DependencyProperty TitleProperty =
            DependencyProperty.Register("Title", typeof(string), typeof(SearchResultCategory), new PropertyMetadata(string.Empty));

        public bool IsPending
        {
            get { return (bool)GetValue(IsPendingProperty); }
            set { SetValue(IsPendingProperty, value); }
        }

        public static readonly DependencyProperty IsPendingProperty =
            DependencyProperty.Register("IsPending", typeof(bool), typeof(SearchResultCategory), new PropertyMetadata(false));

        public IconData TitleIcon
        {
            get { return (IconData)GetValue(TitleIconProperty); }
            set { SetValue(TitleIconProperty, value); }
        }

        public static readonly DependencyProperty TitleIconProperty =
            DependencyProperty.Register("TitleIcon", typeof(IconData), typeof(SearchResultCategory), new PropertyMetadata(new IconData(enumInternIcons.f4sd)));

        public SearchResultCategory()
        {
            InitializeComponent();
        }

        protected override void OnInitialized(EventArgs e)
        {
            base.OnInitialized(e);
            AddHandler(CustomEventManager.IndexChangedEvent, new CustomEventManager.IndexChangedHandlerDelegate(HandleSelectedIndexChanged));
        }

        private void HandleSelectedIndexChanged(object sender, IndexEventArgs args)
        {
            if (args.OriginalSource == this)
                return;

            CustomEventManager.RaiseIndexChangedEvent(this, args.OldValue, args.NewValue);
        }

        public void UpdateSearchResults(cFasdApiSearchResultCollection results, ISearchUiProvider uiSearchProvider)
        {
            ResultMenu.MenuDataList = results.Select(searchResult =>
            {
                var requiredInformationClasses = new List<enumFasdInformationClass>() { cF4sdIdentityEntry.GetFromSearchResult(searchResult.Value.First().Type) };
                bool isEnabled = cHealthCardDataHelper.HasAvailableHealthCard(requiredInformationClasses);

                var menuData = new cMenuDataSearchResult(searchResult.Key, uiSearchProvider, searchResult.Value) as cMenuDataBase;
                menuData.UiAction.DisplayType = isEnabled ? Enums.enumActionDisplayType.enabled : Enums.enumActionDisplayType.disabled;
                menuData.UiAction.Description = isEnabled ? string.Empty : cMultiLanguageSupport.GetItem("Searchbar.NoValidHealthcard");
                menuData.UiAction.AlternativeDescription = isEnabled ? string.Empty : cMultiLanguageSupport.GetItem("Searchbar.NoValidHealthcard");

                return menuData;
            }).ToList();
        }

        public void UpdateSearchRelations(IEnumerable<cMenuDataBase> relations) => ResultMenu.MenuDataList = relations.ToList();

        internal void ShowDetailHeadings(bool showDetailHeadings) => ResultMenu.ShowDetailHeading(showDetailHeadings);

        internal int GetItemCount() => ResultMenu.MenuDataList.Count;

        internal void HiglightItemAt(int index) => ResultMenu.HightlightItemAt(index);

        internal void SelectItemAt(int index) => ResultMenu.SelectItemAt(index);
    }
}
