using System.IO;
using Streamingle.Editor.Utilities;
using UnityEditor;
using UnityEditor.Overlays;
using UnityEngine;
using UnityEngine.UIElements;

/// <summary>
/// Native Scene View overlay for the screenshot workflow. Unity owns the title
/// bar, drag behavior, collapse state, and overlay menu; this class only
/// supplies the capture actions inside the panel.
/// </summary>
[Overlay(
    typeof(SceneView),
    "Screenshot",
    defaultDisplay = true,
    defaultDockZone = DockZone.Floating)]
public sealed class SceneViewCaptureOverlay : Overlay
{
    public override VisualElement CreatePanelContent()
    {
        var root = new VisualElement();
        root.style.minWidth = 150f;
        root.style.paddingLeft = 2f;
        root.style.paddingRight = 2f;
        root.style.paddingTop = 2f;
        root.style.paddingBottom = 2f;

        var shotButton = new Button(() => CaptureCamera(1920, 1080))
        {
            text = "Shot"
        };
        shotButton.style.height = 24f;
        shotButton.style.marginBottom = 4f;
        root.Add(shotButton);

        root.Add(CreateLabel("Camera"));
        root.Add(CreateButtonRow(
            ("FHD", () => CaptureCamera(1920, 1080)),
            ("QHD", () => CaptureCamera(2560, 1440)),
            ("4K", () => CaptureCamera(3840, 2160))));

        root.Add(CreateLabel("Utilities"));
        root.Add(CreateButtonRow(
            ("Scene", CaptureSceneView),
            ("Frame", FrameSelection),
            ("Folder", OpenScreenshotFolder)));

        return root;
    }

    private static Label CreateLabel(string text)
    {
        var label = new Label(text);
        label.style.fontSize = 10;
        label.style.marginTop = 2f;
        label.style.marginBottom = 1f;
        return label;
    }

    private static VisualElement CreateButtonRow(params (string text, System.Action action)[] buttons)
    {
        var row = new VisualElement();
        row.style.flexDirection = FlexDirection.Row;
        row.style.marginBottom = 3f;

        foreach ((string text, System.Action action) in buttons)
        {
            var button = new Button(action) { text = text };
            button.style.flexGrow = 1f;
            button.style.marginRight = 2f;
            row.Add(button);
        }

        return row;
    }

    private static void CaptureCamera(int width, int height)
    {
        // Keep the established workflow: selected Camera, then Main Camera,
        // then the best available scene camera.
        ResolutionScreenshotMenu.Capture(
            width,
            height,
            camera: null,
            transparentBackground: false,
            revealAfterCapture: false);
    }

    private void CaptureSceneView()
    {
        SceneView sceneView = containerWindow as SceneView ?? SceneView.lastActiveSceneView;
        if (sceneView == null || sceneView.camera == null)
        {
            Debug.LogWarning("[Screenshot] Active Scene View camera was not available.");
            return;
        }

        ResolutionScreenshotMenu.Capture(
            1920,
            1080,
            sceneView.camera,
            transparentBackground: false,
            revealAfterCapture: false);
    }

    private void FrameSelection()
    {
        SceneView sceneView = containerWindow as SceneView ?? SceneView.lastActiveSceneView;
        if (sceneView == null)
        {
            return;
        }

        sceneView.FrameSelected();
        sceneView.Repaint();
    }

    private static void OpenScreenshotFolder()
    {
        string folder = ResolutionScreenshotMenu.GetScreenshotFolder();
        Directory.CreateDirectory(folder);
        EditorUtility.RevealInFinder(folder);
    }
}
