using ZelWiki.Engine.Library;
using ZelWiki.Engine.Library.Interfaces;
using ZelWiki.Models;

namespace ZelWiki.Engine.Implementation
{
    /// <summary>
    /// 
    /// </summary>
    public class EmojiHandler : IEmojiHandler
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="state"></param>
        /// <param name="key"></param>
        /// <param name="scale"></param>
        /// <returns></returns>
        public HandlerResult Handle(IZelEngineState state, string key, int scale)
        {
            var emoji = GlobalConfiguration.Emojis.FirstOrDefault(o => o.Shortcut == key);

            if (GlobalConfiguration.Emojis.Exists(o => o.Shortcut == key))
            {
                if (scale != 100 && scale > 0 && scale <= 500)
                {
                    var emojiImage =
                        $"<img src=\"{GlobalConfiguration.BasePath}/file/Emoji/{key.Trim('%')}?Scale={scale}\" alt=\"{emoji?.Name}\" />";

                    return new HandlerResult(emojiImage);
                }
                else
                {
                    var emojiImage =
                        $"<img src=\"{GlobalConfiguration.BasePath}/file/Emoji/{key.Trim('%')}\" alt=\"{emoji?.Name}\" />";

                    return new HandlerResult(emojiImage);
                }
            }
            else
            {
                return new HandlerResult(key)
                    { Instructions = [Constants.HandlerResultInstruction.DisallowNestedProcessing] };
            }
        }
    }
}