import { ExploreRow } from "widgets/rich-model.slint";
import { RichList } from "widgets/rich-list.slint";
import { ViewSwitch } from "widgets/view-switch.slint";
import { ListView } from "std-widgets.slint";
import { Nine, Label, Theme } from "widgets/base.slint";
import { Pieces } from "widgets/pieces.slint";
import { InsetPanel, ShallowPanel, ListFrame, SubheaderFrame, TabbedPaneFrame } from "widgets/frame.slint";
import { Tabs, TabItem } from "widgets/tabs.slint";
import { TableHeader, TableRow, EmptyList } from "widgets/table.slint";
import { InfoTable, InfoRow, DependencyItem } from "widgets/info-table.slint";
import { ThumbnailFrame } from "widgets/thumbnail.slint";
import { Button } from "widgets/button.slint";
import { ToolButton } from "widgets/tool-button.slint";
import { GameTooltip, SearchTooltip } from "widgets/tooltip.slint";
import { Scrollbar } from "widgets/scrollbar.slint";
import { ErrorBanner } from "widgets/banner.slint";
import { Dropdown } from "widgets/dropdown.slint";
import { TextField } from "widgets/text-field.slint";

export struct ManageRow { name: string, title: string, version: string, checked: bool, locked: bool, dimmed: bool, pending: bool, selected: bool, red: bool, tooltip: string, download: bool, failed: bool, placeholder: bool, fraction: float, progress-label: string }

export component Manage {
    in-out property <int> view-mode;
    in property <[ExploreRow]> rich-rows;
    in property <ExploreRow> rich-detail;
    changed view-mode => { root.list-y = 0px; }
    in property <string> issue;
    in property <bool> install-missing;
    in property <[string]> versions;
    in property <int> version-choice;
    in property <string> notice;
    in property <bool> force-pin-open;
    in property <bool> busy;
    callback pin(int);
    callback install-dependencies();
    property <bool> pin-open;
    changed title => { root.pin-open = false; }
    property <string> banner: root.error != "" ? root.error : root.notice;
    in property <bool> remove-enabled: true;
    callback remove();
    callback mod-action(string, bool); callback download-cancel(string);
    in property <[ManageRow]> rows;
    in property <string> title;
    in property <string> description;
    in property <image> thumbnail;
    in property <bool> missing: true;
    in property <[InfoRow]> info;
    in property <[DependencyItem]> dependencies;
    in property <string> error;
    in property <bool> mixed: true;
    in property <bool> all-checked;
    in-out property <int> current-tab;
    in property <int> force-tip: -1;
    in property <int> force-pressed-row: -1;
    in property <int> force-row-tip: -1;
    in property <int> force-hover-row: -1;
    in property <bool> has-changelog;
    in property <int> sort-column: -1;
    in property <bool> sort-ascending: true;
    in property <bool> search-open;
    in property <string> search-text;
    in property <int> search-tick;
    in property <bool> force-search-tip;
    callback sort(int);
    callback search(string);
    callback toggle-search();
    callback open-changelog();
    callback dependency-activated(string);
    callback manage-focus(string);
    in property <int> focus-row;
    in property <int> focus-tick;
    changed focus-tick => { root.list-y = -clamp(root.focus-row * 28px - max(0px, root.table-height - 28px) / 2, 0px, max(0px, root.rows.length * 28px - root.table-height)); }
    property <length> table-height: root.height - 72px - root.rows-top;
    callback open-url(string);
    callback selected(string);
    callback toggled(string);
    callback toggle-all();
    property <[TabItem]> tabs: [{text: "Manage", badge: false, enabled: true}, {text: "Explore", badge: false, enabled: true}, {text: "Updates", badge: true, enabled: true}];
    property <int> hover-row: -1;
    property <bool> search-hover;
    // Opening, closing or editing the search restarts the list at the top.
    changed search-open => { root.list-y = 0px; }
    in-out property <length> list-y;
    property <length> rows-top: 64px + (root.banner == "" ? 0px : root.error-height);
    property <int> tip-row: root.force-row-tip >= 0 ? root.force-row-tip : root.hover-row;
    property <length> left-width: root.width - 612px;
    property <length> error-height: root.left-width < 650px ? 100px : 60px;
    property <bool> rows-scroll: root.rows.length * 28px > root.height - 136px - (root.banner == "" ? 0px : root.error-height);

    InsetPanel { x: 0px; y: 0px; width: root.left-width; height: root.height; }
    Rectangle { x: 4px; y: 4px; width: root.left-width - 8px; height: 44px; background: Theme.list-bg; }
    Tabs { x: 12px; y: 12px; width: root.left-width < 384px ? 228px : 252px; compact: root.left-width < 384px; height: 40px; items: root.tabs; current: root.current-tab; part: "others"; selected(i) => { root.current-tab = i; } }
    TabbedPaneFrame { x: 0px; y: 48px; width: root.left-width; height: root.height - 48px; }
    Tabs { x: 12px; y: 12px; width: root.left-width < 384px ? 228px : 252px; compact: root.left-width < 384px; height: 40px; items: root.tabs; current: root.current-tab; part: "selected"; selected(i) => { root.current-tab = i; } }
    Rectangle { x: 0px; y: 48px; width: root.left-width; height: root.height - 48px; clip: true; Nine { x: 0px; y: -48px; width: parent.width; height: root.height; source: Pieces.inside-shallow-frame-default-shadow; } }

    ViewSwitch { x: root.left-width - 108px; y: 12px; mode <=> root.view-mode; }

    ListFrame {
        x: 12px; y: 60px; width: root.left-width - 24px; height: root.height - 72px; inner-shadow: false;
        SubheaderFrame { x: 0px; y: 0px; width: parent.width; height: 36px;
            if root.search-open: TextField { x: max(112px, parent.width - 264px); y: 3px; width: max(20px, min(232px, parent.width - self.x - 36px)); height: 28px; search: true; autofocus: true; focus-tick: root.search-tick; text: root.search-text; edited(text) => { root.list-y = 0px; root.search(text); } }
            ToolButton { x: parent.width - 32px; y: 3px; width: 28px; height: 28px; state: root.search-open ? "clicked" : "auto"; hover-changed(h) => { root.search-hover = h; } clicked => { root.list-y = 0px; root.toggle-search(); } }
        }
        if root.current-tab == 0 && root.view-mode == 0: TableHeader { x: 0px; y: 36px; width: parent.width - (root.rows-scroll ? 12px : 0px); height: 28px; widths: [self.width - 103px, 61px]; mixed: root.mixed; checked: root.all-checked; sort-column: root.sort-column; ascending: root.sort-ascending; sort(i) => { root.list-y = 0px; root.sort(i); } toggle-all => { root.toggle-all(); } }
        if root.current-tab == 0 && root.view-mode == 0: rows-area := Rectangle {
            x: 0px; y: root.rows-top; width: parent.width; height: parent.height - self.y; clip: true;
            property <bool> scrolls: root.rows-scroll;
            list := ListView {
                x: 0px; y: 0px; content-y <=> root.list-y;
                width: parent.width - (parent.scrolls ? 12px : 0px); height: parent.height;
                horizontal-scrollbar-policy: always-off; vertical-scrollbar-policy: always-off;
                for row[i] in root.rows: TableRow {
                    state: root.force-pressed-row == i ? "clicked" : root.force-hover-row == i ? "hovered" : "auto";
                    width: list.visible-width; height: 28px; text: row.title; version: row.version;
                    checked: row.checked; enabled: !row.locked && !root.busy; disabled-text: row.dimmed; pending: row.pending; selected: row.selected; elide-text: true; red: row.red;
                    placeholder: row.placeholder; downloading: row.download || row.failed; failed: row.failed;
                    fraction: row.fraction; progress-label: row.progress-label; cancel => { root.download-cancel(row.name); }
                    hover-changed(h) => { root.hover-row = h ? i : (root.hover-row == i ? -1 : root.hover-row); }
                    selected-row => { root.selected(row.name); }
                    toggled => { root.toggled(row.name); }
                }
            }
            if !rows-area.scrolls: EmptyList { x: 0px; y: root.rows.length * 28px + 2px; width: parent.width; height: max(0px, parent.height - self.y); }
            if rows-area.scrolls: Scrollbar {
                x: parent.width - 12px; y: 0px; width: 12px; height: parent.height;
                thumb-height: max(24px, self.height * list.visible-height / list.content-height);
                thumb-position: -list.content-y / max(1px, list.content-height - list.visible-height) * (self.height - self.thumb-height);
                scroll(delta) => { list.content-y = clamp(list.content-y + delta, -max(0px, list.content-height - list.visible-height), 0px); }
                moved(y) => { list.content-y = -clamp((y - self.thumb-height / 2) / (self.height - self.thumb-height), 0, 1) * (list.content-height - list.visible-height); }
            }
        }
        if root.current-tab == 0 && root.view-mode == 0 && root.tip-row >= 0 && root.tip-row < root.rows.length && root.rows[root.tip-row].tooltip != "": GameTooltip {
            // The game anchors the tooltip at the cursor; this one sits under the row's name text.
            x: 50px; y: root.rows-top + root.tip-row * 28px + root.list-y + 24px; text: root.rows[root.tip-row].tooltip;
        }
        if root.banner != "" && root.current-tab == 0: ErrorBanner { x: 0px; y: 64px; width: parent.width; height: root.error-height; text: root.banner; wrap-text: true; negative: root.error != ""; }
        if root.view-mode != 0: RichList {
            x: 0px; y: root.banner == "" ? 36px : 64px + root.error-height; width: parent.width; height: parent.height - self.y;
            list-y <=> root.list-y; rows: root.rich-rows; grid: root.view-mode == 2; checks: true; busy: root.busy;
            selected(name) => { root.selected(name); } toggled(name) => { root.toggled(name); }
            activate(name, remove) => { root.mod-action(name, remove); } cancel(name) => { root.download-cancel(name); }
        }
        if root.current-tab != 0: EmptyList { x: 0px; y: 36px; width: parent.width; height: parent.height - 36px; }
        Nine { x: 0px; y: 0px; width: parent.width; height: parent.height; source: Pieces.scroll-pane-default-shadow; }
    }

    ShallowPanel {
        x: root.left-width + 12px; y: 0px; width: 600px; height: root.height;
        SubheaderFrame { x: 0px; y: 0px; width: parent.width; height: 36px;
            Label { x: 12px; y: 5px; width: 432px; height: 24px; text: root.title; font-weight: 700; color: Theme.caption; overflow: elide; }
            Button { x: 456px; y: 3px; width: 108px; height: 28px; text: "Changelog"; enabled: root.title != ""; clicked => { root.open-changelog(); } }
            ToolButton { x: 568px; y: 3px; width: 28px; height: 28px; kind: "red"; icon: "trash"; enabled: root.remove-enabled; clicked => { root.remove(); } }
        }
        body := Flickable {
            // Leave 4px inside the scrolling clip for the thumbnail's outer ramp.
            x: 8px; y: 44px; width: 584px; height: parent.height - 56px;
            content-width: self.width;
            content-height: max(self.height, extras.y + extras.height + 4px);
            // Game paragraph pitch is 20px; this font's hhea metrics at 14px give 21.294px.
            description := Label { x: 4px; y: 4px; width: 408px; text: root.rich-detail.description != "" ? root.rich-detail.description : root.description; wrap: word-wrap; line-height-factor: 20 / 21.294; }
            ThumbnailFrame { x: 436px; y: 4px; width: 144px; height: 144px; source: !root.rich-detail.missing ? root.rich-detail.thumbnail : root.thumbnail; missing: !root.rich-detail.missing ? false : root.missing; }
            info := InfoTable {
                x: 4px; y: max(160px, description.preferred-height + 16px); width: 576px;
                height: root.info.length * 32px + 4px + (root.dependencies.length > 0 ? 8px + (root.dependencies.length - 1) * 28px : 0px);
                activated(i) => { if (root.info[i].link) { root.open-url(root.info[i].value); } }
                rows: root.info; dependencies: root.dependencies; force-tip: root.force-tip;
                dependency-activated(name) => { root.dependency-activated(name); }
                manage-focus(name) => { root.manage-focus(name); }
            }
            extras := VerticalLayout {
                x: 4px; y: info.y + info.height + 8px; width: 576px; height: self.preferred-height; spacing: 8px;
                if root.rich-detail.tags != "": Label { text: "Tags: " + root.rich-detail.tags; color: Theme.caption; wrap: word-wrap; }
                if root.issue != "": Label { text: root.issue; color: #ff8e8e; wrap: word-wrap; }
                if root.install-missing: Button { width: 148px; height: 28px; text: "Install missing"; kind: "green"; enabled: !root.busy; clicked => { root.install-dependencies(); } }
                if root.versions.length > 1: Rectangle {
                    height: 28px;
                    Label { x: 0px; width: 136px; height: 28px; vertical-alignment: center; text: "Load version"; }
                    Dropdown { x: 140px; width: 180px; items: root.versions; current: root.version-choice; open: root.pin-open; force-open: root.force-pin-open;
                        toggled => { if (!root.busy) { root.pin-open = !root.pin-open; } }
                        selected(i) => { if (!root.busy) { root.pin-open = false; root.pin(i); } }
                    }
                }
            }
        }
        if body.content-height > body.height: Scrollbar {
            x: 588px; y: 44px; height: body.height; width: 12px;
            thumb-height: max(24px, self.height * body.height / body.content-height);
            thumb-position: -body.content-y / max(1px, body.content-height - body.height) * (self.height - self.thumb-height);
            scroll(delta) => { body.content-y = clamp(body.content-y + delta, -max(0px, body.content-height - body.height), 0px); }
            moved(y) => { body.content-y = -clamp((y - self.thumb-height / 2) / (self.height - self.thumb-height), 0, 1) * (body.content-height - body.height); }
        }
        Rectangle { x: 0px; y: 0px; width: parent.width; height: 36px; clip: true; Nine { x: 0px; y: 0px; width: 600px; height: root.height; source: Pieces.inside-shallow-frame-default-shadow; } }
        // Last, so it paints over the table, the scrollbar and the sub-header.
        if info.tip-text != "": GameTooltip { x: body.x + info.x + info.tip-x; y: body.y + body.content-y + info.y + info.tip-y; text: info.tip-text; }
    }
    // The game anchors it at the cursor; this one hangs under the button, right-aligned to it.
    if root.search-hover || root.force-search-tip: SearchTooltip { x: root.left-width - 16px - self.width; y: 95px; }
}
