import { Label, Theme } from "base.slint";
import { Separator, Link, DependencyLine } from "link.slint";
import { Pieces } from "pieces.slint";
export struct InfoRow { key: string, value: string, link: bool, dependency: bool, red: bool }
export struct DependencyItem { name: string, text: string, optional: bool, incompatible: bool, red: bool, tooltip: string, installed: bool }
export component InfoTable {
    in property <[InfoRow]> rows;
    in property <[DependencyItem]> dependencies;
    in property <length> key-width: 136px;
    in property <length> row-height: 32px;
    callback activated(int);
    callback dependency-activated(string);
    callback manage-focus(string);
    // Capture hook: show this dependency's tooltip without a pointer.
    in property <int> force-tip: -1;
    property <int> hover-dep: -1;
    property <int> tip-index: root.force-tip >= 0 ? root.force-tip : root.hover-dep;
    out property <string> tip-text: root.tip-index >= 0 ? root.dependencies[root.tip-index].tooltip : "";
    // The game anchors the tooltip at the cursor; this one sits just under the line's text start.
    out property <length> tip-x: root.key-width + 12px + 47px;
    out property <length> tip-y: (root.rows.length - 1) * root.row-height + 4px + root.tip-index * 28px + 32px;
    preferred-height: root.rows.length * root.row-height + 4px + (root.rows[root.rows.length - 1].dependency ? 8px : 0px);
    for row[i] in root.rows: Rectangle {
        y: i * root.row-height; height: root.row-height + 4px; width: root.width;
        Separator { x: 4px; y: 0px; width: root.key-width - 4px; height: 4px; }
        Separator { x: root.key-width + 4px; y: 0px; width: root.width - root.key-width - 8px; height: 4px; }
        Label { x: 12px; y: 4px; height: root.row-height - 4px; vertical-alignment: center; text: row.key; }
        if !row.link && !row.dependency: Label { x: root.key-width + 12px; y: 4px; height: root.row-height - 4px; vertical-alignment: center; text: row.value; color: row.red ? #ff8e8e : Theme.text; }
        if row.link: Link { x: root.key-width + 12px; y: 4px; width: root.width - self.x - 12px; height: root.row-height - 4px; text: row.value; clicked => { root.activated(i); } }
        if row.dependency && root.dependencies.length == 0: DependencyLine { x: root.key-width + 12px; y: 4px; height: root.row-height + 4px; text: row.value; clicked => { root.activated(i); } }
    }
    for dep[i] in root.dependencies: DependencyLine {
        x: root.key-width + 12px; y: (root.rows.length - 1) * root.row-height + 4px + i * 28px;
        width: root.width - self.x - 12px;
        height: root.row-height + 4px; text: dep.text; optional: dep.optional;
        installed: dep.installed;
        manage-focus => { root.manage-focus(dep.name); }
        required: !dep.optional; red: dep.red;
        clicked => { root.dependency-activated(dep.name); }
        hover-changed(h) => { root.hover-dep = h ? i : (root.hover-dep == i ? -1 : root.hover-dep); }
    }
    Separator { x: 4px; y: root.height - 4px; width: root.key-width - 4px; height: 4px; }
    Separator { x: root.key-width + 4px; y: root.height - 4px; width: root.width - root.key-width - 8px; height: 4px; }
    for i in root.rows.length: Rectangle {
        x: 0px; y: i * root.row-height + 4px; width: root.width;
        height: (i == root.rows.length - 1 ? root.height - 4px : (i + 1) * root.row-height) - self.y;
        for x in [0px, root.key-width, root.width - 4px]: Separator { x: x; y: 0px; width: 4px; height: parent.height; vertical: true; }
    }
    for i in root.rows.length + 1: Image {
        x: 0px; y: (i == root.rows.length ? root.height - 4px : i * root.row-height); width: 4px; height: 4px;
        source: i == 0 ? Pieces.bordered-table-top-left-coner-default-base : i == root.rows.length ? Pieces.bordered-table-bottom-left-corner-default-base : Pieces.bordered-table-left-t-default-base;
        image-rendering: pixelated;
    }
    for i in root.rows.length + 1: Image {
        x: root.width - 4px; y: (i == root.rows.length ? root.height - 4px : i * root.row-height); width: 4px; height: 4px;
        source: i == 0 ? Pieces.bordered-table-top-right-corner-default-base : i == root.rows.length ? Pieces.bordered-table-bottom-right-corner-default-base : Pieces.bordered-table-right-t-default-base;
        image-rendering: pixelated;
    }
    // Intersections use the atlas rather than overlapping two bevel ramps.
    for i in root.rows.length + 1: Image { x: root.key-width; y: (i == root.rows.length ? root.height - 4px : i * root.row-height); width: 4px; height: 4px; source: i == 0 ? Pieces.bordered-table-top-t-default-base : i == root.rows.length ? Pieces.bordered-table-bottom-t-default-base : Pieces.bordered-table-cross-default-base; image-rendering: pixelated; }
}
