﻿/*
MIT License

Copyright(c) 2019 Mitchel Thompson
www.angryarugula.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Arugula
{
    public static class RichTextExtensions
    {
        /*
        void Usage()
        {
            string str = "The Apples are turning Blue!";

            string stylizedStr = str.Colorize("Apples", Color.red).Colorize("Blue", Color.blue).QuickTag("Blue", "i").Tag("!", "<size=32>", "</size>");

            Debug.Log(stylizedStr);
        }
        */

        /// <summary>
        /// Wraps an entire string in a color tag
        /// </summary>
        /// <param name="str"></param>
        /// <param name="color"></param>
        /// <returns></returns>
        public static string Colorize(this string str, Color color)
        {
            return str.Tag($"<color=#{ColorUtility.ToHtmlStringRGBA(color)}>", "</color>");
        }

        /// <summary>
        /// Wraps all instances of a word in a string with a color tag.
        /// </summary>
        /// <param name="str"></param>
        /// <param name="color"></param>
        /// <param name="word"></param>
        /// <returns></returns>
        public static string Colorize(this string str, string word, Color color)
        {
            return str.Tag(word, $"<color=#{ColorUtility.ToHtmlStringRGBA(color)}>", "</color>");
        }

        /// <summary>
        /// Wraps a range of characters beginning at index with a color tag.
        /// </summary>
        /// <param name="str"></param>
        /// <param name="color"></param>
        /// <param name="index"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public static string Colorize(this string str, int index, int length, Color color)
        {
            return str.Tag(index, length, $"<color=#{ColorUtility.ToHtmlStringRGBA(color)}>", "</color>");
        }


        /// <summary>
        /// Wraps an entire string in a tag
        /// </summary>
        /// <param name="str"></param>
        /// <param name="style"></param>
        /// <returns></returns>
        public static string Stylize(this string str, string style)
        {
            return str.Tag($"<style=\"{style}\">", "</style>");
        }

        /// <summary>
        /// Wraps all instances of a word in a string with a simple tag. Usage myStr.QuickTag("Bold!", "b")
        /// </summary>
        /// <param name="str"></param>
        /// <param name="word"></param>
        /// <param name="tag"></param>
        /// <returns></returns>
        public static string Stylize(this string str, string word, string style)
        {
            return str.Tag(word, $"<style=\"{style}\">", "</style>");
        }

        /// <summary>
        /// Wraps a range of characters in a string with a simple tag. Usage myStr.QuickTag(0, 10, "b")
        /// </summary>
        /// <param name="str"></param>
        /// <param name="index"></param>
        /// <param name="length"></param>
        /// <param name="tag"></param>
        /// <returns></returns>
        public static string Stylize(this string str, int index, int length, string style)
        {
            return str.Tag(index, length, $"<style=\"{style}\">", "</style>");
        }


        /// <summary>
        /// Wraps an entire string in a tag
        /// </summary>
        /// <param name="str"></param>
        /// <param name="tag"></param>
        /// <returns></returns>
        public static string QuickTag(this string str, string tag)
        {
            return str.Tag($"<{tag}>", $"</{tag}>");
        }

        /// <summary>
        /// Wraps all instances of a word in a string with a simple tag. Usage myStr.QuickTag("Bold!", "b")
        /// </summary>
        /// <param name="str"></param>
        /// <param name="word"></param>
        /// <param name="tag"></param>
        /// <returns></returns>
        public static string QuickTag(this string str, string word, string tag)
        {
            return str.Tag(word, $"<{tag}>", $"</{tag}>");
        }

        /// <summary>
        /// Wraps a range of characters in a string with a simple tag. Usage myStr.QuickTag(0, 10, "b")
        /// </summary>
        /// <param name="str"></param>
        /// <param name="index"></param>
        /// <param name="length"></param>
        /// <param name="tag"></param>
        /// <returns></returns>
        public static string QuickTag(this string str, int index, int length, string tag)
        {
            return str.Tag(index, length, $"<{tag}>", $"</{tag}>");
        }

        /// <summary>
        /// Wraps an entire string in a tag
        /// </summary>
        /// <param name="str"></param>
        /// <param name="open"></param>
        /// <param name="close"></param>
        /// <returns></returns>
        public static string Tag(this string str, string open, string close)
        {
            return $"{open}{str}{close}";
        }

        /// <summary>
        /// Wraps all instances of a word in a string with a tag. Usage myStr.Tag("Bold!", "<b>", "</b>")
        /// </summary>
        /// <param name="str"></param>
        /// <param name="word"></param>
        /// <param name="open"></param>
        /// <param name="close"></param>
        /// <returns></returns>
        public static string Tag(this string str, string word, string open, string close)
        {
            return str.Replace(word, $"{open}{word}{close}");
        }

        /// <summary>
        /// Wraps a range of characters in a string with a tag. Usage myStr.Tag(0, 10, "<b>", "</b>")
        /// </summary>
        /// <param name="str"></param>
        /// <param name="index"></param>
        /// <param name="length"></param>
        /// <param name="open"></param>
        /// <param name="close"></param>
        /// <returns></returns>
        public static string Tag(this string str, int index, int length, string open, string close)
        {
            str = str.Insert(index, open);
            return str.Insert(index + length + open.Length, close);
        }

        /// <summary>
        /// Wraps an entire string in a size tag
        /// </summary>
        /// <param name="str"></param>
        /// <param name="size"></param>
        /// <returns></returns>
        public static string Size(this string str, float size)
        {
            return str.Tag($"<size={size}>", "</size>");
        }

        /// <summary>
        /// Wraps all instances of a word in a string with a size tag.
        /// </summary>
        /// <param name="str"></param>
        /// <param name="size"></param>
        /// <param name="word"></param>
        /// <returns></returns>
        public static string Size(this string str, string word, float size)
        {
            return str.Tag(word, $"<size={size}>", "</size>");
        }

        /// <summary>
        /// Wraps a range of characters beginning at index with a size tag.
        /// </summary>
        /// <param name="str"></param>
        /// <param name="size"></param>
        /// <param name="index"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public static string Size(this string str, int index, int length, float size)
        {
            return str.Tag(index, length, $"<size={size}>", "</size>");
        }

        /// <summary>
        /// Wraps an entire string in a size tag
        /// </summary>
        /// <param name="str"></param>
        /// <param name="space"></param>
        /// <returns></returns>
        public static string Monospace(this string str, float space)
        {
            return str.Tag($"<mspace={space}>", "</mspace>");
        }

        /// <summary>
        /// Wraps all instances of a word in a string with a size tag.
        /// </summary>
        /// <param name="str"></param>
        /// <param name="space"></param>
        /// <param name="word"></param>
        /// <returns></returns>
        public static string Monospace(this string str, string word, float space)
        {
            return str.Tag(word, $"<mspace={space}>", "</mspace>");
        }

        /// <summary>
        /// Wraps a range of characters beginning at index with a size tag.
        /// </summary>
        /// <param name="str"></param>
        /// <param name="space"></param>
        /// <param name="index"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public static string Monospace(this string str, int index, int length, float space)
        {
            return str.Tag(index, length, $"<mspace={space}>", "</mspace>");
        }
    }
}