using System.Diagnostics;
using AxCopilot.SDK;
using AxCopilot.Services;

namespace AxCopilot.Handlers;

/// <summary>
/// L27-5: 화면 밝기 제어 핸들러. "bright" 프리픽스로 사용합니다.
///
/// 예: bright             → 현재 밝기 표시
///     bright 70          → 밝기 70% 설정
///     bright up / down   → ±10% 조절
/// Enter → 해당 밝기로 설정.
/// WMI (WmiMonitorBrightness) 사용 — 노트북 내장 디스플레이 대상.
/// </summary>
public class BrightHandler : IActionHandler
{
    public string? Prefix => "bright";

    public PluginMetadata Metadata => new(
        "밝기 제어",
        "화면 밝기 조절 — 설정·증감 (노트북)",
        "1.0",
        "AX");

    public Task<IEnumerable<LauncherItem>> GetItemsAsync(string query, CancellationToken ct)
    {
        var q     = query.Trim().ToLowerInvariant();
        var items = new List<LauncherItem>();

        // 현재 밝기 읽기
        int current = GetCurrentBrightness();
        if (current < 0)
        {
            items.Add(new LauncherItem(
                "밝기 센서를 찾을 수 없습니다",
                "노트북 내장 디스플레이에서만 동작합니다 (외장 모니터 미지원)",
                null, null, Symbol: "\uE7BA"));
            return Task.FromResult<IEnumerable<LauncherItem>>(items);
        }

        var bar = BrightnessBar(current);

        if (string.IsNullOrWhiteSpace(q))
        {
            items.Add(new LauncherItem(
                $"현재 밝기: {current}%",
                $"{bar}  ·  bright 70 / bright up / bright down",
                null, null, Symbol: "\uE706"));

            items.Add(new LauncherItem("bright up",   "밝기 +10%", null, ("set", Math.Min(current + 10, 100)), Symbol: "\uE706"));
            items.Add(new LauncherItem("bright down", "밝기 −10%", null, ("set", Math.Max(current - 10, 0)),   Symbol: "\uE706"));

            foreach (var p in new[] { 10, 25, 50, 75, 100 })
                items.Add(new LauncherItem($"bright {p}", $"밝기 {p}%", null, ("set", p), Symbol: "\uE706"));

            return Task.FromResult<IEnumerable<LauncherItem>>(items);
        }

        if (q is "up" or "올려" or "+")
        {
            var target = Math.Min(current + 10, 100);
            items.Add(new LauncherItem($"밝기 +10% → {target}%", BrightnessBar(target), null, ("set", target), Symbol: "\uE706"));
        }
        else if (q is "down" or "내려" or "-")
        {
            var target = Math.Max(current - 10, 0);
            items.Add(new LauncherItem($"밝기 −10% → {target}%", BrightnessBar(target), null, ("set", target), Symbol: "\uE706"));
        }
        else if (int.TryParse(q, out int val) && val is >= 0 and <= 100)
        {
            items.Add(new LauncherItem(
                $"밝기 {val}% 설정",
                $"{BrightnessBar(val)}  (현재 {current}%)",
                null, ("set", val), Symbol: "\uE706"));
        }
        else
        {
            items.Add(new LauncherItem($"'{query}' — 알 수 없는 명령",
                "사용법: bright 70 / bright up / bright down",
                null, null, Symbol: "\uE7BA"));
        }

        return Task.FromResult<IEnumerable<LauncherItem>>(items);
    }

    public Task ExecuteAsync(LauncherItem item, CancellationToken ct)
    {
        if (item.Data is ("set", int level))
        {
            bool ok = SetBrightness(level);
            if (ok)
                NotificationService.Notify("bright", $"밝기 {level}%");
            else
                NotificationService.Notify("bright", "밝기 설정 실패 — 노트북 내장 디스플레이에서만 동작합니다.");
        }
        return Task.CompletedTask;
    }

    private static string BrightnessBar(int pct)
    {
        int filled = pct / 5;
        return "[" + new string('█', filled) + new string('░', 20 - filled) + "]";
    }

    // ─── WMI 밝기 읽기/쓰기 (PowerShell subprocess) ──────────────────────────

    private static int GetCurrentBrightness()
    {
        try
        {
            var output = RunPowerShell(
                "(Get-CimInstance -Namespace root/WMI -ClassName WmiMonitorBrightness -ErrorAction Stop).CurrentBrightness");
            if (int.TryParse(output.Trim(), out int val))
                return val;
        }
        catch { }
        return -1;
    }

    private static bool SetBrightness(int level)
    {
        try
        {
            level = Math.Clamp(level, 0, 100);
            var cmd = $"$m = Get-CimInstance -Namespace root/WMI -ClassName WmiMonitorBrightnessMethods -ErrorAction Stop; " +
                      $"Invoke-CimMethod -InputObject $m -MethodName WmiSetBrightness -Arguments @{{Timeout=1; Brightness={level}}}";
            RunPowerShell(cmd);
            return true;
        }
        catch { return false; }
    }

    private static string RunPowerShell(string command)
    {
        using var proc = new Process
        {
            StartInfo = new ProcessStartInfo
            {
                FileName = "powershell.exe",
                Arguments = $"-NoProfile -NonInteractive -Command \"{command}\"",
                RedirectStandardOutput = true,
                RedirectStandardError = true,
                UseShellExecute = false,
                CreateNoWindow = true
            }
        };
        proc.Start();
        var output = proc.StandardOutput.ReadToEnd();
        proc.WaitForExit(3000);
        return output;
    }
}
