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 { ListFrame, SubheaderFrame } from "widgets/frame.slint";
import { LeftPane } from "widgets/left-pane.slint";
import { DetailsPane } from "widgets/details-pane.slint";
import { PortalDetail } from "widgets/description.slint";
import { InfoRow } from "widgets/info-table.slint";
import { TableHeader, TableCell, UpdateTableRow, EmptyList } from "widgets/table.slint";
import { Button } from "widgets/button.slint";
import { TextField } from "widgets/text-field.slint";
import { ToolButton } from "widgets/tool-button.slint";
import { Scrollbar } from "widgets/scrollbar.slint";
import { ErrorBanner } from "widgets/banner.slint";
import { SearchTooltip } from "widgets/tooltip.slint";

export struct UpdateRow { name: string, title: string, local-version: string, new-version: string, updated: string, checked: bool, downloading: bool, fraction: float }
export component Updates {
    in-out property <int> view-mode;
    in property <[ExploreRow]> rich-rows;
    changed view-mode => { root.list-y = 0px; }
    in property <bool> batch-busy;
    in property <int> selected-count;
    in property <int> detail-action;
    in property <string> download-name;
    in property <float> download-fraction;
    in property <string> download-label;
    callback update-selected();
    callback mod-action(string, bool);
    callback download-cancel(string);
    callback refresh();
    callback sort(int);
    in property <int> sort-column: -1;
    in property <bool> sort-ascending: true;
    in property <[UpdateRow]> rows;
    in property <[string]> headers;
    in property <string> action-label;
    in property <string> message;
    in property <bool> failed;
    in property <int> count;
    in property <bool> all-checked;
    in property <bool> mixed;
    in property <string> search-text;
    in property <bool> search-open;
    in property <int> search-tick;
    in property <bool> force-search-tip;
    // The shared selection (Manage's): the installed mod whose details the pane shows.
    in property <string> selected-name;
    in property <string> title;
    in property <string> description;
    in property <PortalDetail> portal-detail;
    in property <bool> detail-loading;
    in-out property <int> detail-gallery: -1;
    in-out property <length> detail-scroll;
    callback detail-open-url(string);
    callback detail-zoom-image(image);
    callback detail-open-changelog();
    in property <image> thumbnail;
    in property <bool> missing: true;
    in property <[InfoRow]> info;
    in-out property <string> picked;
    callback tab(int);
    callback dependency-activated(string);
    callback manage-focus(string);
    callback selected(string);
    callback search(string);
    callback toggle-search();
    callback toggled(string);
    callback toggle-all();
    in-out property <length> list-y;
    property <bool> search-hover;
    property <length> update-button-width: root.left-width < 524px ? 80px : 148px;
    property <length> left-width: root.width - 612px;
    property <length> pane-height: root.height - 72px;
    property <bool> has-rows: root.rows.length > 0 && root.message == "";
    // Header rows (36 sub-header + 28 table header) sit above the list.
    property <bool> rows-scroll: root.has-rows && root.rows.length * 28px > root.pane-height - 64px;
    property <length> version-width: 94px;
    property <length> new-width: root.left-width < 600px ? 80px : 90px;
    property <length> updated-width: root.left-width < 600px ? 104px : 118px;
    // The local version column gives way when the name would drop under 200px (the 1000px minimum window).
    property <length> local-width: root.left-width - 24px - 42px - root.new-width - root.updated-width - root.version-width < 200px ? 0px : root.version-width;
    // A row is selected once it was clicked here and Manage's selection still is that mod.
    property <bool> has-selection: root.picked != "" && root.picked == root.selected-name;

    LeftPane {
        x: 0px; y: 0px; width: root.left-width; height: root.height; current: 2; badge: root.count > 0 || root.failed;
        tab(i) => { root.tab(i); }

        ListFrame {
            x: 12px; y: 60px; width: root.left-width - 24px; height: root.pane-height; inner-shadow: false;
            if root.view-mode == 0: rows-area := Rectangle {
                x: 0px; y: 64px; width: parent.width; height: parent.height - 64px; clip: true;
                list := ListView {
                    x: 0px; y: 0px; width: parent.width - (root.rows-scroll ? 12px : 0px); height: parent.height;
                    content-y <=> root.list-y;
                    horizontal-scrollbar-policy: always-off; vertical-scrollbar-policy: always-off;
                    for row in root.rows: UpdateTableRow {
                        downloading: row.downloading; fraction: row.fraction;
                        check-enabled: !root.batch-busy;
                        width: list.visible-width; height: 28px;
                        check-width: 42px; check-arrow: true;
                        text: row.title; local-version: row.local-version; new-version: row.new-version; updated: row.updated; checked: row.checked; selected: root.has-selection && row.name == root.picked;
                        widths: [root.local-width, root.new-width, root.updated-width];
                        toggled => { root.toggled(row.name); }
                        selected-row => { root.picked = row.name; root.selected(row.name); }
                    }
                }
                if root.rows.length > 0 && !root.rows-scroll: EmptyList { x: 0px; y: root.rows.length * 28px + 2px; width: parent.width; height: max(0px, parent.height - self.y); }
                if root.rows-scroll: 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); }
                }
                // Up to date: one plain table row with the centred message (no banner).
                if !root.failed && root.message != "": TableCell { x: 0px; y: 0px; width: parent.width; height: 28px; }
                if !root.failed && root.message != "": Label { x: 0px; y: 0px; width: parent.width; height: 28px; horizontal-alignment: center; vertical-alignment: center; text: root.message; }
                if !root.failed && root.message != "": EmptyList { x: 0px; y: 30px; width: parent.width; height: max(0px, parent.height - self.y); }
                if root.failed: EmptyList { x: 0px; y: 58px; width: parent.width; height: max(0px, parent.height - self.y); }
            }
            if root.view-mode == 0: TableHeader {
                x: 0px; y: 36px; width: parent.width - (root.rows-scroll ? 12px : 0px); height: 28px;
                labels: root.headers; check-width: 42px; check-sort-column: 4; widths: [self.width - 42px - root.local-width - root.new-width - root.updated-width, root.local-width, root.new-width, root.updated-width];
                sort-column: root.sort-column; ascending: root.sort-ascending; sort(i) => { root.list-y = 0px; root.sort(i); }
                enabled: root.has-rows; mixed: root.mixed; checked: root.all-checked;
                toggle-all => { root.toggle-all(); }
            }
            if root.view-mode != 0: RichList {
                x: 0px; y: root.failed ? 124px : 36px; 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.batch-busy;
                selected(name) => { root.picked = 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.view-mode != 0 && root.message != "" && !root.failed: Label { x: 8px; y: 44px; width: parent.width - 16px; height: 28px; text: root.message; }
            if root.failed: ErrorBanner { x: 0px; y: 64px; width: parent.width; height: 60px; text: root.message; }
            Nine { x: 0px; y: 0px; width: parent.width; height: parent.height; source: Pieces.scroll-pane-default-shadow; }
            SubheaderFrame { x: 0px; y: 0px; width: parent.width; height: 36px;
                if root.search-open: TextField { x: max(112px, parent.width - root.update-button-width - 72px - 232px); y: 3px; width: max(20px, min(232px, parent.width - root.update-button-width - 72px - self.x)); 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 - root.update-button-width - 68px; 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(); } }
                Button { x: parent.width - 36px - root.update-button-width; y: 3px; width: root.update-button-width; min-width: 0px; height: 28px; text: root.update-button-width < 148px ? "Update" : root.action-label; kind: "green"; enabled: root.selected-count > 0 && !root.batch-busy; clicked => { root.update-selected(); } }
                ToolButton { x: parent.width - 32px; y: 3px; width: 28px; height: 28px; icon: "refresh"; clicked => { root.refresh(); } }
            }
        }
    }

    ViewSwitch { x: root.left-width - 108px; y: 12px; mode <=> root.view-mode; }
    DetailsPane {
        manage-focus(name) => { root.manage-focus(name); }
        action: root.has-selection ? root.detail-action : 0; busy: root.batch-busy;
        downloading: root.has-selection && root.picked == root.download-name; fraction: root.download-fraction; progress-label: root.download-label;
        activate(remove) => { root.mod-action(root.picked, remove); } cancel => { root.download-cancel(root.picked); }
        portal: root.has-selection ? root.portal-detail : { name: "" }; loading: root.detail-loading;
        gallery-open <=> root.detail-gallery; scroll-y <=> root.detail-scroll;
        dependency-activated(name) => { root.dependency-activated(name); }
        open-url(url) => { root.detail-open-url(url); }
        zoom-image(image) => { root.detail-zoom-image(image); }
        open-changelog => { root.detail-open-changelog(); }
        x: root.left-width + 12px; y: 0px; width: 600px; height: root.height;
        title: root.has-selection ? root.title : ""; description: root.has-selection ? root.description : "";
        thumbnail: root.thumbnail; missing: !root.has-selection || root.missing; info: root.has-selection ? root.info : [];
    }
    if root.search-hover || root.force-search-tip: SearchTooltip { x: root.left-width - 200px; y: 95px; }
}
