using NTDLS.Helpers;
using System.Text;

namespace ZelWiki.Library
{
    public static class ConfirmActionHelper
    {
        /// <summary>
        /// 生成一个链接，通过GET导航到“确认操作”页面，其中“是”链接为红色，但“否”按钮仍为绿色。
        /// </summary>
        /// <param name="basePath"></param>
        /// <param name="message"></param>
        /// <param name="linkLabel"></param>
        /// <param name="controllerURL"></param>
        /// <param name="yesOrDefaultRedirectURL"></param>
        /// <param name="noRedirectURL"></param>
        /// <returns></returns>
        public static string GenerateDangerLink(string basePath, string message, string linkLabel, string controllerURL,
            string? yesOrDefaultRedirectURL, string? noRedirectURL = null)
        {
            noRedirectURL ??= yesOrDefaultRedirectURL;

            yesOrDefaultRedirectURL.EnsureNotNull();
            noRedirectURL.EnsureNotNull();

            var param = new StringBuilder();
            param.Append($"ControllerURL={Uri.EscapeDataString($"{basePath}{controllerURL}")}");
            param.Append($"&YesRedirectURL={Uri.EscapeDataString(yesOrDefaultRedirectURL)}");
            param.Append($"&NoRedirectURL={Uri.EscapeDataString(noRedirectURL)}");
            param.Append($"&Message={Uri.EscapeDataString(message)}");
            param.Append($"&Style=Danger");

            return
                $"<a class=\"btn btn-danger btn-thin\" href=\"{basePath}/Utility/ConfirmAction?{param}\">{linkLabel}</a>";
        }

        /// <summary>
        /// 生成一个链接，通过GET导航到“确认操作”页面，其中“是”链接为绿色。
        /// </summary>
        /// <param name="basePath"></param>
        /// <param name="message"></param>
        /// <param name="linkLabel"></param>
        /// <param name="controllerURL"></param>
        /// <param name="yesOrDefaultRedirectURL"></param>
        /// <param name="noRedirectURL"></param>
        /// <returns></returns>
        public static string GenerateSafeLink(string basePath, string message, string linkLabel, string controllerURL,
            string? yesOrDefaultRedirectURL, string? noRedirectURL = null)
        {
            noRedirectURL ??= yesOrDefaultRedirectURL;

            yesOrDefaultRedirectURL.EnsureNotNull();
            noRedirectURL.EnsureNotNull();

            var param = new StringBuilder();
            param.Append($"ControllerURL={Uri.EscapeDataString($"{basePath}{controllerURL}")}");
            param.Append($"&YesRedirectURL={Uri.EscapeDataString(yesOrDefaultRedirectURL)}");
            param.Append($"&NoRedirectURL={Uri.EscapeDataString(noRedirectURL)}");
            param.Append($"&Message={Uri.EscapeDataString(message)}");
            param.Append($"&Style=Safe");

            return
                $"<a class=\"btn btn-success btn-thin\" href=\"{basePath}/Utility/ConfirmAction?{param}\">{linkLabel}</a>";
        }

        /// <summary>
        /// 生成一个链接，通过GET导航到“确认操作”页面，其中“是”链接为黄色，但“否”按钮仍为绿色。
        /// </summary>
        /// <param name="basePath"></param>
        /// <param name="message"></param>
        /// <param name="linkLabel"></param>
        /// <param name="controllerURL"></param>
        /// <param name="yesOrDefaultRedirectURL"></param>
        /// <param name="noRedirectURL"></param>
        /// <returns></returns>
        public static string GenerateWarnLink(string basePath, string message, string linkLabel, string controllerURL,
            string? yesOrDefaultRedirectURL, string? noRedirectURL = null)
        {
            noRedirectURL ??= yesOrDefaultRedirectURL;

            yesOrDefaultRedirectURL.EnsureNotNull();
            noRedirectURL.EnsureNotNull();

            var param = new StringBuilder();
            param.Append($"ControllerURL={Uri.EscapeDataString($"{basePath}{controllerURL}")}");
            param.Append($"&YesRedirectURL={Uri.EscapeDataString(yesOrDefaultRedirectURL)}");
            param.Append($"&NoRedirectURL={Uri.EscapeDataString(noRedirectURL)}");
            param.Append($"&Message={Uri.EscapeDataString(message)}");
            param.Append($"&Style=Warn");

            return
                $"<a class=\"btn btn-warning btn-thin\" href=\"{basePath}/Utility/ConfirmAction?{param}\">{linkLabel}</a>";
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="message"></param>
        /// <param name="buttonLabel"></param>
        /// <param name="controllerURL"></param>
        /// <param name="yesOrDefaultRedirectURL"></param>
        /// <param name="noRedirectURL"></param>
        /// <returns></returns>
        public static string GenerateDangerButton(string message, string buttonLabel, string controllerURL,
            string? yesOrDefaultRedirectURL, string? noRedirectURL = null)
        {
            noRedirectURL ??= yesOrDefaultRedirectURL;

            yesOrDefaultRedirectURL.EnsureNotNull();
            noRedirectURL.EnsureNotNull();

            var html = new StringBuilder();
            html.Append("<form action='/Utility/ConfirmAction' method='post'>");
            html.Append($"<input type='hidden' name='ControllerURL' value='{controllerURL}' />");
            html.Append($"<input type='hidden' name='YesRedirectURL' value='{yesOrDefaultRedirectURL}' />");
            html.Append($"<input type='hidden' name='NoRedirectURL' value='{noRedirectURL}' />");
            html.Append($"<input type='hidden' name='Message' value='{message}' />");
            html.Append($"<input type='hidden' name='Style' value='Danger' />");
            html.Append(
                $"<button type='submit' class='btn btn-danger rounded-0' name='ActionToConfirm' value='PurgeDeletedPages'>{buttonLabel}</button>");
            html.Append("</form>");

            return html.ToString();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="message"></param>
        /// <param name="buttonLabel"></param>
        /// <param name="controllerURL"></param>
        /// <param name="yesOrDefaultRedirectURL"></param>
        /// <param name="noRedirectURL"></param>
        /// <returns></returns>
        public static string GenerateSafeButton(string message, string buttonLabel, string controllerURL,
            string? yesOrDefaultRedirectURL, string? noRedirectURL = null)
        {
            noRedirectURL ??= yesOrDefaultRedirectURL;

            yesOrDefaultRedirectURL.EnsureNotNull();
            noRedirectURL.EnsureNotNull();

            var html = new StringBuilder();

            html.Append("<form action='/Utility/ConfirmAction' method='post'>");
            html.Append($"<input type='hidden' name='ControllerURL' value='{controllerURL}' />");
            html.Append($"<input type='hidden' name='YesRedirectURL' value='{yesOrDefaultRedirectURL}' />");
            html.Append($"<input type='hidden' name='NoRedirectURL' value='{noRedirectURL}' />");
            html.Append($"<input type='hidden' name='Message' value='{message}' />");
            html.Append($"<input type='hidden' name='Style' value='Safe' />");
            html.Append(
                $"<button type='submit' class='btn btn-success rounded-0' name='ActionToConfirm' value='PurgeDeletedPages'>{buttonLabel}</button>");
            html.Append("</form>");

            return html.ToString();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="message"></param>
        /// <param name="buttonLabel"></param>
        /// <param name="controllerURL"></param>
        /// <param name="yesOrDefaultRedirectURL"></param>
        /// <param name="noRedirectURL"></param>
        /// <returns></returns>
        public static string GenerateWarnButton(string message, string buttonLabel, string controllerURL,
            string? yesOrDefaultRedirectURL, string? noRedirectURL = null)
        {
            noRedirectURL ??= yesOrDefaultRedirectURL;

            yesOrDefaultRedirectURL.EnsureNotNull();
            noRedirectURL.EnsureNotNull();

            var html = new StringBuilder();

            html.Append("<form action='/Utility/ConfirmAction' method='post'>");
            html.Append($"<input type='hidden' name='ControllerURL' value='{controllerURL}' />");
            html.Append($"<input type='hidden' name='YesRedirectURL' value='{yesOrDefaultRedirectURL}' />");
            html.Append($"<input type='hidden' name='NoRedirectURL' value='{noRedirectURL}' />");
            html.Append($"<input type='hidden' name='Message' value='{message}' />");
            html.Append($"<input type='hidden' name='Style' value='Warn' />");
            html.Append(
                $"<button type='submit' class='btn btn-warning rounded-0' name='ActionToConfirm' value='PurgeDeletedPages'>{buttonLabel}</button>");
            html.Append("</form>");

            return html.ToString();
        }
    }
}