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

// `back_button` / `confirm_button` (l.1148, 1183; parent `dialog_button` l.1619): 32 px high, min 112 wide, one pointed
// end (24x64 arrow tile) and one straight end taken from the grey / green button. Bold 18 label, `#1c1c1c`.
// state: "auto" | "default" | "hovered" | "clicked" | "disabled".
component DialogButton {
    in property <string> text;
    in property <bool> back: true;
    // `red_back_button` (Exit): the back button's shape in the red set.
    in property <bool> red;
    in property <bool> enabled: true;
    in property <string> state: "auto";
    callback clicked();
    forward-focus: key;
    public function activate() {
        if (root.enabled) { UiSounds.begin(); root.clicked(); UiSounds.finish(root.back ? UiCue.click : UiCue.confirm); }
    }
    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;
        }
    }


    min-width: 112px;
    min-height: 32px;
    preferred-height: 32px;
    preferred-width: max(112px, label.preferred-width + 40px);

    property <string> visual: root.state != "auto" ? root.state
        : !root.enabled ? "disabled" : touch.pressed ? "clicked" : touch.has-hover ? "hovered" : "default";

    pure function shadow(b: bool, s: string) -> image {
        if (b && root.red) { return s == "disabled" ? Pieces.red-back-button-disabled-glow : Pieces.red-back-button-default-shadow; }
        if (b) { return s == "disabled" ? Pieces.back-button-disabled-glow : Pieces.back-button-default-shadow; }
        return s == "disabled" ? Pieces.confirm-button-disabled-glow : Pieces.confirm-button-default-shadow;
    }
    pure function face(b: bool, s: string) -> image {
        if (b && root.red) { return s == "hovered" ? Pieces.red-back-button-hovered-base : s == "clicked" ? Pieces.red-back-button-clicked-base : s == "disabled" ? Pieces.red-back-button-disabled-base : Pieces.red-back-button-default-base; }
        if (b) { return s == "hovered" ? Pieces.back-button-hovered-base : s == "clicked" ? Pieces.back-button-clicked-base : s == "disabled" ? Pieces.back-button-disabled-base : Pieces.back-button-default-base; }
        return s == "hovered" ? Pieces.confirm-button-hovered-base : s == "clicked" ? Pieces.confirm-button-clicked-base : s == "disabled" ? Pieces.confirm-button-disabled-base : Pieces.confirm-button-default-base;
    }

    // Dirt shadow (default, disabled); the clicked state has none, hovered swaps it for the orange / green glow.
    if root.visual == "default" || root.visual == "disabled": Nine { x: -4px; y: -4px; width: root.width + 8px; height: root.height + 8px; source: root.shadow(root.back, root.visual); }
    if (root.back && !root.red) || root.visual != "hovered": Nine { width: root.width; height: root.height; source: root.face(root.back, root.visual); }
    if root.visual == "hovered" && root.back && !root.red: Nine { x: -4px; y: -4px; width: root.width + 8px; height: root.height + 8px; source: root.back ? Pieces.back-button-hovered-glow : Pieces.confirm-button-hovered-glow; }
    if root.visual == "hovered" && (!root.back || root.red): Nine { x: -4px; y: -4px; width: root.width + 8px; height: root.height + 8px; source: root.red ? Pieces.red-back-button-hovered-precomposed : Pieces.confirm-button-hovered-precomposed; }
    label := Text {
        x: root.back ? 20px : 0px; y: root.visual == "clicked" ? 0px : -1px;
        width: root.width - 20px; height: root.height - 2px;
        horizontal-alignment: root.back ? left : right;
        vertical-alignment: center;
        text: root.text;
        color: root.visual == "disabled" ? #808080 : Theme.dialog-black;
        font-family: Theme.font; font-size: 18px; font-weight: 700; wrap: no-wrap;
    }
    touch := TouchArea { enabled: root.enabled; clicked => { key.focus(); root.activate(); } }
}

export component BackButton inherits DialogButton { back: true; }
export component ExitButton inherits DialogButton { back: true; red: true; }
export component ConfirmButton inherits DialogButton { back: false; }
