import { UiCue } from "audio.slint";
import { Button } from "button.slint";
import { ToolButton } from "tool-button.slint";
import { Nine, Label, Theme } from "base.slint";
import { Pieces } from "pieces.slint";

// none / download / update / remove; folders use the same actions as archives.
export component ModAction {
    in property <int> action;
    in property <bool> busy;
    in property <bool> downloading;
    in property <bool> failed;
    in property <float> fraction;
    in property <string> progress-label;
    in property <bool> compact;
    in property <string> version;
    callback activate(bool);
    callback cancel();
    callback tooltip(string);
    if root.downloading: Rectangle {
        x: 0px; y: 0px; width: max(0px, parent.width - 72px); height: 28px;
        if !root.failed: Nine { y: 10px; width: parent.width; height: 8px; source: Pieces.progressbar-background-default-base; }
        if !root.failed && root.fraction > 0: Nine { x: 0px; y: 10px; width: max(8px, parent.width * clamp(root.fraction, 0, 1)); height: 8px; source: Pieces.progressbar-fill-default-base; }
        if !root.failed: Nine { x: -4px; y: 6px; width: parent.width + 8px; height: 16px; source: Pieces.progressbar-background-default-shadow; }
        Label { width: parent.width; height: 28px; vertical-alignment: center; overflow: elide; text: root.progress-label; color: root.failed ? #ff8e8e : Theme.text; font-size: 12px; }
    }
    if root.downloading: Button { sound: UiCue.tool; x: parent.width - 56px; width: 56px; min-width: 0px; height: 28px; text: "X"; clicked => { root.cancel(); } }
    if !root.downloading && root.action > 0 && !root.compact: Button {
        sound: root.action == 3 ? UiCue.tool : UiCue.confirm;
        x: parent.width - 108px; width: 108px; height: 28px;
        kind: root.action == 3 ? "red" : "green";
        text: root.action == 1 ? "Download" : root.action == 2 ? "Update" : "Remove";
        enabled: !root.busy; clicked => { root.activate(root.action == 3); }
    }
    if !root.downloading && root.action > 0 && root.compact: ToolButton {
        x: parent.width - 28px; width: 28px; height: 28px;
        transparent-background: true;
        kind: root.action == 3 ? "red" : "green";
        icon: root.action == 1 ? "download" : root.action == 2 ? "refresh" : "trash";
        enabled: !root.busy; clicked => { root.activate(root.action == 3); }
        hover-changed(h) => { root.tooltip(h ? (root.action == 1 ? "Download" : root.action == 2 ? "Update to " + root.version : "Remove") : ""); }
    }
}
