import { UiSounds, UiCue } from "audio.slint";
import { Nine } from "base.slint";
import { Pieces } from "pieces.slint";
import { Icons } from "icons.slint";

// `frame_action_button` (style.lua l.2783): the 24 px square on the window title bar (close, search).
export component FrameActionButton {
    in property <string> state: "auto";
    in property <bool> enabled: true;
    in property <string> icon: "search";
    in property <bool> red;
    callback clicked();
    forward-focus: key;
    public function activate() {
        if (root.enabled) { UiSounds.begin(); root.clicked(); UiSounds.finish(UiCue.tool); }
    }
    key := FocusScope {
        enabled: root.enabled; focus-on-click: false;
        width: root.width; height: root.height;
        key-pressed(event) => {
            if ((event.text == " " || event.text == Key.Return) && root.enabled) {
                root.activate(); return accept;
            }
            return reject;
        }
    }

    preferred-width: 24px; preferred-height: 24px;
    property <string> visual: root.state != "auto" ? root.state : !root.enabled ? "disabled" : touch.pressed ? "clicked" : touch.has-hover ? "hovered" : "default";
    pure function shadow(s: string) -> image {
        return s == "hovered" ? Pieces.frame-action-button-hovered-shadow : s == "clicked" ? Pieces.frame-action-button-clicked-shadow : s == "disabled" ? Pieces.frame-action-button-disabled-shadow : Pieces.frame-action-button-default-shadow;
    }
    pure function face(s: string) -> image {
        return s == "hovered" ? Pieces.frame-action-button-hovered-base : s == "clicked" ? Pieces.frame-action-button-clicked-base : s == "disabled" ? Pieces.frame-action-button-disabled-base : Pieces.frame-action-button-default-base;
    }
    Nine { x: -4px; y: -4px; width: root.width + 8px; height: root.height + 8px; source: root.red ? (root.visual == "clicked" ? Pieces.red-button-clicked-shadow : root.visual == "hovered" ? Pieces.red-button-hovered-shadow : root.visual == "disabled" ? Pieces.red-button-disabled-shadow : Pieces.red-button-default-shadow) : root.shadow(root.visual); }
    Nine { width: root.width; height: root.height; source: root.red ? (root.visual == "clicked" ? Pieces.red-button-clicked-base : root.visual == "hovered" ? Pieces.red-button-hovered-base : root.visual == "disabled" ? Pieces.red-button-disabled-base : Pieces.red-button-default-base) : root.face(root.visual); }
    if root.visual == "hovered": Nine { x: -4px; y: -4px; width: root.width + 8px; height: root.height + 8px; source: root.red ? Pieces.red-button-hovered-glow : Pieces.frame-action-button-hovered-glow; }
    if root.icon == "search" || root.icon == "close": Image {
        width: 16px; height: 16px;
        x: (root.width - self.width) / 2; y: (root.height - self.height) / 2 + (root.visual == "clicked" ? 1px : 0px);
        source: root.icon == "close" ? @image-url("../../../assets/game/icons1x/close.png") : Icons.search; opacity: root.enabled ? 1 : 0.5; image-rendering: pixelated;
    }
    if root.icon == "minimize": Rectangle { x: (root.width - 12px) / 2; y: (root.height - 2px) / 2 + 4px + (root.visual == "clicked" ? 1px : 0px); width: 12px; height: 2px; background: white; }
    if root.icon == "maximize" || root.icon == "restore": Rectangle {
        x: (root.width - 12px) / 2; y: (root.height - 12px) / 2 + (root.visual == "clicked" ? 1px : 0px);
        width: 12px; height: 12px;
        if root.icon == "restore": Rectangle { x: 3px; y: 0px; width: 9px; height: 9px; border-width: 2px; border-color: white; }
        Rectangle { x: 0px; y: root.icon == "restore" ? 3px : 0px; width: root.icon == "restore" ? 9px : 12px; height: root.icon == "restore" ? 9px : 12px; border-width: 2px; border-color: white; background: root.icon == "restore" ? #313031 : transparent; }
    }
    touch := TouchArea { enabled: root.enabled; mouse-cursor: pointer; clicked => { key.focus(); root.activate(); } }
}
