using C4IT.FASD.Base;
using F4SD_AdaptableIcon;
using F4SD_AdaptableIcon.Enums;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;

namespace FasdDesktopUi.Basics.Converter
{
    /// <summary>
    /// Manages conversion between <see cref="cFasdIcon"/> and <see cref="IconData"/>
    /// </summary>
    internal class IconDataConverter : IValueConverter
    {
        /// <summary>
        /// Converts <see cref="cFasdIcon"/> to <see cref="IconData"/>
        /// </summary>
        /// <returns>The icon data of type <see cref="IconData"/></returns>
        internal static IconData Convert(cFasdIcon iconConfig)
        {
            switch (iconConfig.IconType)
            {
                case enumIconType.intern:
                    if (Enum.TryParse(iconConfig.Name, out enumInternIcons intern))
                        return new IconData(intern);
                    break;
                case enumIconType.material:
                    if (Enum.TryParse(iconConfig.Name, out MaterialIcons.MaterialIconType material))
                        return new IconData(material);
                    break;
                case enumIconType.gif:
                    if (Enum.TryParse(iconConfig.Name, out enumInternGif gif))
                        return new IconData(gif);
                    break;
            }

            return new IconData(enumInternIcons.none);
        }

        /// <summary>
        /// Converts <see cref="cFasdIcon"/> to <see cref="IconData"/>
        /// </summary>
        /// <returns>The icon data of type <see cref="IconData"/></returns>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (targetType != typeof(IconData))
                return null;

            if (!(value is cFasdIcon iconConfig))
                return new IconData(enumInternIcons.none);

            return Convert(iconConfig);
        }

        /// <summary>
        /// Converts <see cref="IconData"/> to <see cref="cFasdIcon"/> 
        /// </summary>
        /// <returns>The configuration of type <see cref="cFasdIcon"/></returns>
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (!(value is IconData iconData))
                return null;

            if (iconData.Intern != null)
                return new cFasdIcon() { IconType = enumIconType.intern, Name = iconData.Intern.ToString(), IsValid = true };
            else if (iconData.Material != null)
                return new cFasdIcon(iconData.Material.Value);
            else if (iconData.Gif != null)
                return new cFasdIcon() { IconType = enumIconType.gif, Name = iconData.Gif.ToString(), IsValid = true };

            return null;
        }
    }
}
