using System;
using System.Drawing;
using System.Windows.Forms;
using Elfisa.UI.Theming;

namespace Elfisa.UI.Controls
{
    /// <summary>
    /// Стили кнопок по макету Pharma Trust (variant-e-pharma-trust.html).
    /// </summary>
    public static class ModernButtonStyles
    {
        public const int DefaultHeight = 34;
        public const int ClearSize = 32;
        private static readonly Font ActionFont = new Font("Segoe UI Semibold", 10f, FontStyle.Bold);

        public static void ApplyAuto(ModernButton button)
        {
            if (button == null || button.HeaderNavMode || button.TopNavMode || button.SidebarMode)
            {
                return;
            }

            var text = (button.Text ?? string.Empty).Trim();
            if (IsClearButton(button, text))
            {
                ApplyClear(button);
                return;
            }

            if (IsDangerAction(text))
            {
                // Мягкий danger в тон Clinical Pro — без кислотно-красного «крика».
                ApplyAction(button, ModernButtonStyle.DangerSoft);
                return;
            }

            if (IsPrimaryAction(text))
            {
                ApplyAction(button, ModernButtonStyle.Primary);
                return;
            }

            ApplyAction(button, ModernButtonStyle.Secondary);
        }

        public static void ApplyAction(ModernButton button, ModernButtonStyle style)
        {
            if (button == null)
            {
                return;
            }

            button.ButtonStyle = style;
            button.CompactIconMode = false;
            button.Image = null;
            button.BackgroundImage = null;
            button.UseVisualStyleBackColor = false;
            button.AutoSizeMode = AutoSizeMode.GrowAndShrink;
            button.IconGlyph = null;
            button.AllowTextEllipsis = false;
            button.Font = ActionFont;
            button.Padding = new Padding(16, 6, 16, 6);
            var height = DefaultHeight;
            button.Height = Math.Max(button.Height, height);

            FitToText(button, height);
        }

        public static void ApplyClear(ModernButton button)
        {
            if (button == null)
            {
                return;
            }

            button.ButtonStyle = ModernButtonStyle.DangerSoft;
            button.CompactIconMode = true;
            button.Image = null;
            button.BackgroundImage = null;
            button.UseVisualStyleBackColor = false;
            button.AutoSizeMode = AutoSizeMode.GrowAndShrink;
            button.Text = "×";
            button.Font = new Font("Segoe UI Semibold", 13f, FontStyle.Bold);
            button.IconGlyph = null;
            button.AllowTextEllipsis = false;
            button.MinimumSize = new Size(ClearSize, ClearSize);
            button.Size = new Size(ClearSize, ClearSize);
        }

        public static void FitToText(ModernButton button, int height = DefaultHeight)
        {
            if (button == null || button.CompactIconMode)
            {
                return;
            }

            var text = button.Text ?? string.Empty;
            if (text.Length == 0)
            {
                return;
            }

            var textWidth = TextRenderer.MeasureText(
                text,
                button.Font ?? ActionFont,
                Size.Empty,
                TextFormatFlags.SingleLine).Width;

            var width = textWidth + button.Padding.Horizontal + 8;
            width = Math.Max(width, 72);
            button.MinimumSize = new Size(width, height);
            if (button.Width < width)
            {
                button.Width = width;
            }

            if (button.Height < height)
            {
                button.Height = height;
            }
        }

        private static bool IsClearButton(ModernButton button, string text)
        {
            if (text == "×" || text.Equals("X", StringComparison.OrdinalIgnoreCase))
            {
                return true;
            }

            var name = button.Name ?? string.Empty;
            if (name.IndexOf("Clear", StringComparison.OrdinalIgnoreCase) >= 0
                && button.Width <= 48
                && button.Height <= 44)
            {
                return true;
            }

            return button.Width <= 44 && button.Height <= 44 && text.Length <= 2;
        }

        private static bool IsDangerAction(string text)
        {
            var lower = text.ToLowerInvariant();
            return lower.Contains("удал")
                || lower.Contains("отмен")
                || lower.Contains("очист")
                || lower.Contains("delete")
                || lower.Contains("cancel")
                || lower.Contains("erase");
        }

        private static bool IsPrimaryAction(string text)
        {
            var lower = text.ToLowerInvariant();
            return lower.Contains("отправ")
                || lower.Contains("сохран")
                || lower.Contains("зарег")
                || lower.Contains("выполн")
                || lower.Contains("коммент")
                || lower.Contains("подтвер")
                || lower.Contains("обнов")
                || lower.Contains("send")
                || lower.Contains("save")
                || lower.Contains("refresh")
                || lower == "ок";
        }
    }
}
