export { UiSounds, UiCue } from "widgets/audio.slint";
import { UiSounds, UiCue } from "widgets/audio.slint";
import { ExploreRow } from "widgets/rich-model.slint";
import { Manage, ManageRow } from "manage.slint";
import { InfoRow, DependencyItem } from "widgets/info-table.slint";
import { Frame, FrameTitle, RibStrip } from "widgets/frame.slint";
import { BackButton, ExitButton, ConfirmButton } from "widgets/dialog-button.slint";
import { Label, Theme } from "widgets/base.slint";
import { ChangelogView } from "changelog.slint";

import { Explore, FilterItem, CatalogueTabStrip, PageButton } from "explore.slint";
import { Updates, UpdateRow } from "updates.slint";

import { Lightbox } from "widgets/lightbox.slint";
export { DescriptionImages } from "widgets/description.slint";
import { PortalDetail } from "widgets/description.slint";
import { OperationDialog } from "widgets/operation-dialog.slint";
import { ShallowPanel } from "widgets/frame.slint";
import { Button } from "widgets/button.slint";
import { SettingsDialog, OnboardingDialog } from "settings.slint";
import { FrameActionButton } from "widgets/frame-action-button.slint";
import { ToolButton } from "widgets/tool-button.slint";
import { GameTooltip } from "widgets/tooltip.slint";

export component ModsWindow inherits Window {
    no-frame: true;
    resize-border-width: 6px;
    in property <bool> native-move-on-press;
    callback window-drag(float, float); callback window-minimize(); callback window-maximize(); callback window-close();
    out property <bool> title-hovered: title-touch.has-hover;
    in-out property <bool> onboarding-open;
    in property <string> data-folder;
    in property <string> install-folder;
    callback onboarding-continue();
    in property <bool> verify-downloads: true;
    in-out property <bool> settings-verify: true;
    in-out property <bool> settings-sounds: true;
    in-out property <int> settings-volume: 50;
    callback settings-cancel();
    in property <[string]> detected-data-folders;
    callback data-folder-picked(int);
    in-out property <bool> settings-open;
    in-out property <string> settings-install;
    in-out property <string> settings-data;
    in-out property <string> settings-error;
    in property <string> settings-environment-note;
    in property <bool> install-ready: true;
    callback settings-requested();
    callback settings-save();
    icon: @image-url("../../assets/game/factorio-icon.png");
    in-out property <int> manage-view;
    in-out property <int> updates-view;
    in-out property <int> explore-view: 1;
    in property <[ExploreRow]> manage-rich-rows;
    in property <ExploreRow> manage-rich-detail;
    in property <[ExploreRow]> updates-rich-rows;
    callback rich-refresh();
    in property <bool> batch-busy;
    in property <string> batch-status;
    in property <string> download-name;
    in property <float> download-fraction;
    in property <string> download-label;
    in-out property <string> results-banner;
    in property <string> manage-issue;
    in property <bool> manage-install-missing;
    in property <[string]> manage-versions;
    in property <int> manage-version-choice;
    in property <string> manage-notice;
    in property <bool> force-pin-open;
    callback manage-pin(int);
    callback install-missing();
    in property <bool> manage-remove-enabled: true;
    in property <bool> operation-dialog;
    in property <string> operation-title;
    in property <[string]> operation-lines;
    in property <bool> operation-confirmable;
    in property <int> updates-detail-action;
    callback mod-action(string, bool);
    callback update-selected();
    callback download-cancel(string);
    callback operation-accept();
    callback operation-back();
    callback operation-event();
    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 dependency-activated(string);
    callback manage-focus(string);
    in property <int> manage-focus-row;
    in property <int> manage-focus-tick;
    callback detail-selected(string);
    in-out property <bool> lightbox-open;
    in-out property <image> lightbox-source;
    in-out property <float> lightbox-zoom: 0;
    in property <bool> lightbox-software;
    pure callback lightbox-raster(image, float, length, length, length, length) -> image;
    changed current-tab => { root.lightbox-open = false; }
    title: "Mods";
    preferred-width: 1444px; preferred-height: 949px;
    min-width: 1000px; min-height: 600px;
    background: Theme.frame-bg;
    in property <[ManageRow]> rows;
    in property <string> detail-title;
    in property <string> description;
    in property <image> thumbnail;
    in property <bool> missing: true;
    in property <[InfoRow]> info;
    in property <[DependencyItem]> dependencies;
    in property <int> pending-count;
    in property <bool> mixed: true;
    in property <bool> all-checked;
    in property <string> error;
    in property <string> selected-name;
    in-out property <int> current-tab;
    in property <int> force-dependency-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> search-open;
    in property <string> search-text;
    in property <int> sort-column: 0;
    in property <bool> sort-ascending: true;
    in property <bool> has-changelog;
    in property <bool> show-changelog;
    in property <string> changelog-text;
    in property <string> changelog-date;
    in property <[string]> changelog-versions;
    in property <[string]> changelog-subversions;
    in property <int> changelog-version;
    in property <int> changelog-subversion;
    in property <int> changelog-category: 3;
    in-out property <string> runtime-status;
    in property <[ExploreRow]> explore-rows;
    in property <[FilterItem]> explore-filters;
    in property <ExploreRow> explore-detail;
    in property <int> explore-count;
    in property <string> explore-search-text;
    in property <int> explore-sort;
    in property <[string]> explore-sort-names;
    in property <[string]> explore-group-names;
    in property <string> explore-sort-label;
    in property <string> explore-exclude-tip;
    in property <string> explore-error;
    in property <string> explore-found;
    in property <string> explore-notice;
    in property <[PageButton]> explore-pages;
    in property <int> explore-page-number: 1;
    in property <bool> explore-loading;
    in property <int> explore-scroll-tick;
    in-out property <int> explore-first;
    in-out property <int> explore-visible: 12;
    in-out property <bool> show-portal-changelog;
    in property <string> portal-changelog-text;
    in property <string> portal-changelog-date;
    in property <[string]> portal-changelog-versions;
    in property <[string]> portal-changelog-subversions;
    in property <int> portal-changelog-version;
    in property <int> portal-changelog-subversion;
    in property <int> portal-changelog-category: 3;
    property <bool> log-open: root.show-changelog || root.show-portal-changelog;
    in property <int> force-filter-tip: -1;
    in property <int> force-exclude-tip: -1;
    in property <[UpdateRow]> updates-rows;
    in property <[string]> updates-headers;
    in property <string> updates-action-label;
    in property <string> updates-message;
    in property <bool> updates-failed: true;
    in property <int> updates-count;
    in property <int> updates-selected-count;
    in property <bool> updates-all-checked;
    in property <bool> updates-mixed;
    in property <int> updates-sort-column: -1;
    in property <bool> updates-sort-ascending: true;
    in-out property <string> updates-picked;
    in property <string> updates-search-text;
    in property <bool> updates-search-open;
    in property <bool> force-search-tip;
    in property <bool> force-filter-end;
    in property <bool> force-dropdown-open;
    in property <int> force-dropdown-hover: -1;
    in property <int> force-dropdown-pressed: -1;
    // Ctrl+F bumps search-tick, Escape bumps search-close-tick; each view reacts for the active tab.
    in-out property <int> search-tick;
    in-out property <int> search-close-tick;
    callback explore-search(string);
    callback explore-sort-changed(int);
    callback explore-filter(string, bool);
    callback explore-selected(string);
    callback explore-page(int);
    callback explore-refresh();
    callback updates-refresh();
    callback updates-sort(int);
    callback portal-open-changelog();
    callback portal-changelog-filter(int, int);
    callback updates-search(string);
    callback updates-toggle-search();
    callback updates-toggled(string);
    callback updates-toggle-all();
    callback catalogue-loaded();
    callback request-close();
    callback undo();
    callback confirm();
    callback save();
    callback selected(string);
    callback toggled(string);
    callback toggle-all();
    callback sort(int);
    callback search(string);
    callback toggle-search();
    callback open-changelog();
    callback changelog-filter(int, int);

    scope := FocusScope {
    width: root.width; height: root.height;
    init => { self.focus(); }
    key-pressed(event) => {
        if (root.onboarding-open && !root.settings-open) { return accept; }
        if (root.settings-open) {
            if (event.text == Key.Escape) { UiSounds.begin(); root.settings-cancel(); UiSounds.finish(UiCue.click); self.focus(); }
            return accept;
        }
        if (root.operation-dialog) { if (event.text == Key.Escape) { UiSounds.begin(); root.operation-back(); UiSounds.finish(UiCue.click); } return accept; }
        if (root.lightbox-open) {
            if (event.text == Key.Escape) { UiSounds.begin(); root.lightbox-open = false; UiSounds.finish(UiCue.click); self.focus(); }
            return accept;
        }
        if (root.log-open) { return reject; }
        if (event.modifiers.control && (event.text == "f" || event.text == "F")) {
            if (root.current-tab == 0 && !root.search-open) { UiSounds.begin(); root.toggle-search(); UiSounds.finish(UiCue.tool); }
            else if (root.current-tab == 2 && !root.updates-search-open) { UiSounds.begin(); root.updates-toggle-search(); UiSounds.finish(UiCue.tool); }
            else { root.search-tick += 1; }
            return accept;
        }
        if (event.text == Key.Escape) {
            if (root.current-tab == 0 && root.search-open) { UiSounds.begin(); root.toggle-search(); UiSounds.finish(UiCue.tool); }
            else if (root.current-tab == 2 && root.updates-search-open) { UiSounds.begin(); root.updates-toggle-search(); UiSounds.finish(UiCue.tool); }
            else if (root.current-tab == 1) { root.search-close-tick += 1; }
            else { return reject; }
            self.focus();
            return accept;
        }
        return reject;
    }
    // The atlas ends in a black outer row; keep it outside the frameless viewport.
    Frame { x: 0px; y: 0px; width: root.width; height: root.height + 1px; glow: false; }
    heading := FrameTitle { x: 16px; y: 13px; text: root.log-open ? (root.show-portal-changelog ? root.portal-detail.title : root.detail-title) + " - Changelog" : "Mods"; }
    RibStrip { x: heading.x + heading.width + 12px; y: 16px; width: max(0px, root.width - self.x - 220px); height: 24px; header: true; }
    title-touch := TouchArea { x: 12px; y: 12px; width: root.width - 232px; height: 28px;
        pointer-event(e) => {
            if (root.native-move-on-press && e.kind == PointerEventKind.down && e.button == PointerEventButton.left) {
                root.window-drag(self.mouse-x / 1px, self.mouse-y / 1px);
            }
        }
        moved => { if (!root.native-move-on-press && self.pressed && abs(self.mouse-x - self.pressed-x) + abs(self.mouse-y - self.pressed-y) > 4px) { root.window-drag(self.mouse-x / 1px, self.mouse-y / 1px); } }
        double-clicked => { if (!root.native-move-on-press) { root.window-maximize(); } }
    }
    Button { x: root.width - 216px; y: 12px; width: 84px; min-width: 0px; height: 28px; text: "Settings"; enabled: !root.batch-busy; clicked => { root.settings-requested(); } }
    FrameActionButton { x: root.width - 120px; y: 14px; icon: "minimize"; clicked => { root.window-minimize(); } }
    FrameActionButton { x: root.width - 84px; y: 14px; icon: root.maximized ? "restore" : "maximize"; clicked => { root.window-maximize(); } }
    ToolButton { x: root.width - 48px; y: 14px; width: 24px; height: 24px; icon: "close"; kind: "red"; enabled: !root.batch-busy; clicked => { root.window-close(); } }
    if !root.log-open && root.current-tab == 0: Manage {
        focus-row: root.manage-focus-row; focus-tick: root.manage-focus-tick;
        manage-focus(name) => { root.manage-focus(name); }
        open-url(url) => { root.detail-open-url(url); }
        view-mode <=> root.manage-view; rich-rows: root.manage-rich-rows; rich-detail: root.manage-rich-detail;
        dependency-activated(name) => { root.dependency-activated(name); }
        mod-action(name, remove) => { root.mod-action(name, remove); } download-cancel(name) => { root.download-cancel(name); }
        issue: root.manage-issue; install-missing: root.manage-install-missing; versions: root.manage-versions; version-choice: root.manage-version-choice;
        notice: root.manage-notice; force-pin-open: root.force-pin-open; busy: root.batch-busy;
        pin(i) => { root.manage-pin(i); } install-dependencies => { root.install-missing(); }
        remove-enabled: root.manage-remove-enabled && !root.batch-busy;
        remove => { root.mod-action(root.selected-name, true); }
        x: 16px; y: 48px; width: root.width - 32px; height: root.height - 115px;
        rows: root.rows; title: root.detail-title; description: root.description; thumbnail: root.thumbnail;
        missing: root.missing; info: root.info; dependencies: root.dependencies; error: root.error; mixed: root.mixed; all-checked: root.all-checked;
        current-tab <=> root.current-tab; force-tip: root.force-dependency-tip; force-pressed-row: root.force-pressed-row; force-row-tip: root.force-row-tip;
        force-hover-row: root.force-hover-row; search-tick: root.search-tick; force-search-tip: root.force-search-tip; search-open: root.search-open; search-text: root.search-text; sort-column: root.sort-column; sort-ascending: root.sort-ascending; has-changelog: root.has-changelog;
        selected(name) => { root.selected(name); }
        toggled(name) => { if (!root.batch-busy) { root.toggled(name); } }
        toggle-all => { if (!root.batch-busy) { root.toggle-all(); } }
        sort(i) => { root.sort(i); }
        search(text) => { root.search(text); }
        toggle-search => { root.toggle-search(); }
        open-changelog => { root.open-changelog(); }
    }
    // Keep Manage's no-catalogue render; replace its static badge once a catalogue is loaded.
    if !root.log-open && root.current-tab == 0 && !root.updates-failed: Rectangle {
        x: 28px; y: 52px; width: root.width < 1028px ? 228px : 252px; height: 44px; background: Theme.list-bg;
        CatalogueTabStrip { y: 8px; width: parent.width; compact: root.width < 1028px; height: 40px; current: 0; update-count: root.updates-count; selected(i) => { root.current-tab = i; } }
    }
    if !root.log-open && root.current-tab == 1: Explore {
        manage-focus(name) => { root.manage-focus(name); }
        view-mode <=> root.explore-view;
        batch-busy: root.batch-busy; download-name: root.download-name; download-fraction: root.download-fraction; download-label: root.download-label;
        dependency-activated(name) => { root.dependency-activated(name); }
        mod-action(name, remove) => { root.mod-action(name, remove); } download-cancel(name) => { root.download-cancel(name); }
        portal-detail: root.portal-detail; detail-loading: root.detail-loading;
        detail-gallery <=> root.detail-gallery; detail-scroll <=> root.detail-scroll;
        detail-open-url(url) => { root.detail-open-url(url); }
        detail-zoom-image(image) => { root.lightbox-source = image; root.lightbox-zoom = 0; root.lightbox-open = true; }
        detail-open-changelog => { root.portal-open-changelog(); }
        x: 16px; y: 48px; width: root.width - 32px; height: root.height - 115px;
        rows: root.explore-rows; filters: root.explore-filters; detail: root.explore-detail;
        search-text: root.explore-search-text; sort: root.explore-sort; sort-names: root.explore-sort-names;
        group-names: root.explore-group-names; sort-label: root.explore-sort-label; exclude-tip: root.explore-exclude-tip;
        error: root.explore-error; found: root.explore-found; update-count: root.updates-count;
        notice: root.explore-notice; pages: root.explore-pages;
        page-number: root.explore-page-number; loading: root.explore-loading; scroll-tick: root.explore-scroll-tick;
        page-changed(p) => { root.explore-page(p); }
        refresh => { root.explore-refresh(); }
        viewport(first, count) => { root.explore-first = first; root.explore-visible = count; }
        open-changelog => { root.portal-open-changelog(); }
        force-filter-tip: root.force-filter-tip; force-exclude-tip: root.force-exclude-tip;
        search-tick: root.search-tick; search-close-tick: root.search-close-tick; force-search-tip: root.force-search-tip; force-filter-end: root.force-filter-end;
        force-dropdown-open: root.force-dropdown-open; force-dropdown-hover: root.force-dropdown-hover; force-dropdown-pressed: root.force-dropdown-pressed;
        tab(i) => { root.current-tab = i; }
        search(text) => { root.explore-search(text); }
        sort-changed(i) => { root.explore-sort-changed(i); }
        filter(id, exclude) => { root.explore-filter(id, exclude); }
        selected(name) => { root.explore-selected(name); }
    }
    if !root.log-open && root.current-tab == 2: Updates {
        manage-focus(name) => { root.manage-focus(name); }
        view-mode <=> root.updates-view; rich-rows: root.updates-rich-rows;
        batch-busy: root.batch-busy; selected-count: root.updates-selected-count; detail-action: root.updates-detail-action;
        download-name: root.download-name; download-fraction: root.download-fraction; download-label: root.download-label;
        dependency-activated(name) => { root.dependency-activated(name); }
        mod-action(name, remove) => { root.mod-action(name, remove); } download-cancel(name) => { root.download-cancel(name); }
        update-selected => { root.update-selected(); }
        portal-detail: root.portal-detail; detail-loading: root.detail-loading;
        detail-gallery <=> root.detail-gallery; detail-scroll <=> root.detail-scroll;
        detail-open-url(url) => { root.detail-open-url(url); }
        detail-zoom-image(image) => { root.lightbox-source = image; root.lightbox-zoom = 0; root.lightbox-open = true; }
        detail-open-changelog => { root.portal-open-changelog(); }
        x: 16px; y: 48px; width: root.width - 32px; height: root.height - 115px;
        rows: root.updates-rows; headers: root.updates-headers; action-label: root.updates-action-label;
        message: root.updates-message; failed: root.updates-failed; count: root.updates-count;
        all-checked: root.updates-all-checked; mixed: root.updates-mixed; sort-column: root.updates-sort-column; sort-ascending: root.updates-sort-ascending;
        search-text: root.updates-search-text; search-open: root.updates-search-open; search-tick: root.search-tick; force-search-tip: root.force-search-tip;
        selected-name: root.selected-name; picked <=> root.updates-picked;
        title: root.detail-title; description: root.description; thumbnail: root.thumbnail; missing: root.missing; info: root.info;
        selected(name) => { root.selected(name); root.detail-selected(name); }
        tab(i) => { root.current-tab = i; }
        search(text) => { root.updates-search(text); }
        toggle-search => { root.updates-toggle-search(); }
        toggled(name) => { root.updates-toggled(name); }
        toggle-all => { root.updates-toggle-all(); }
        sort(i) => { root.updates-sort(i); }
        refresh => { root.updates-refresh(); }
    }
    if root.log-open: ChangelogView {
        x: 16px; y: 48px; width: root.width - 32px; height: root.height - 115px;
        text: root.show-portal-changelog ? root.portal-changelog-text : root.changelog-text;
        date: root.show-portal-changelog ? root.portal-changelog-date : root.changelog-date;
        versions: root.show-portal-changelog ? root.portal-changelog-versions : root.changelog-versions;
        subversions: root.show-portal-changelog ? root.portal-changelog-subversions : root.changelog-subversions;
        version: root.show-portal-changelog ? root.portal-changelog-version : root.changelog-version;
        subversion: root.show-portal-changelog ? root.portal-changelog-subversion : root.changelog-subversion;
        category: root.show-portal-changelog ? root.portal-changelog-category : root.changelog-category;
        filter(kind, index) => { if (root.show-portal-changelog) { root.portal-changelog-filter(kind, index); } else { root.changelog-filter(kind, index); } }
    }
    // Changelog: Back to Manage. Pending changes: Undo clears them. Otherwise the red Exit closes the app. None of them writes.
    if root.log-open: BackButton { x: 16px; y: root.height - 55px; width: 112px; height: 32px; text: "Back"; clicked => { if (root.show-portal-changelog) { root.show-portal-changelog = false; } else { root.request-close(); } } }
    if !root.log-open && root.pending-count > 0: BackButton { x: 16px; y: root.height - 55px; width: 112px; height: 32px; text: "Undo"; clicked => { root.undo(); } }
    if !root.log-open && root.pending-count == 0: ExitButton { x: 16px; y: root.height - 55px; width: 112px; height: 32px; text: "Exit"; enabled: !root.batch-busy; clicked => { root.request-close(); } }
    RibStrip { x: 136px; y: root.height - 55px; width: root.width - (root.log-open ? 152px : root.pending-count > 0 ? 360px : 320px); height: 32px; }
    // The footer ribs are draggable space too, like the title bar.
    footer-touch := TouchArea { x: 136px; y: root.height - 55px; width: root.width - (root.log-open ? 152px : root.pending-count > 0 ? 360px : 320px); height: 32px;
        pointer-event(e) => {
            if (root.native-move-on-press && e.kind == PointerEventKind.down && e.button == PointerEventButton.left) {
                root.window-drag(self.mouse-x / 1px, self.mouse-y / 1px);
            }
        }
        moved => { if (!root.native-move-on-press && self.pressed && abs(self.mouse-x - self.pressed-x) + abs(self.mouse-y - self.pressed-y) > 4px) { root.window-drag(self.mouse-x / 1px, self.mouse-y / 1px); } }
        double-clicked => { if (!root.native-move-on-press) { root.window-maximize(); } }
    }
    if root.pending-count > 0 && !root.log-open: Label { x: 148px; y: root.height - 51px; height: 24px; text: "Mod list changed"; color: Theme.caption; }
    if !root.log-open: ConfirmButton { x: root.width - 176px; y: root.height - 55px; width: 160px; height: 32px; text: "Save and quit"; enabled: !root.batch-busy && root.install-ready; clicked => { root.confirm(); } }
    if !root.log-open && root.pending-count > 0: ToolButton { x: root.width - 216px; y: root.height - 55px; width: 32px; height: 32px; icon: "save"; kind: "green"; enabled: !root.batch-busy && root.install-ready; clicked => { root.save(); } }
    if root.batch-status != "": Rectangle { x: 144px; y: root.height - 55px; width: root.width - 328px; height: 32px; background: Theme.frame-bg;
        Label { x: 8px; width: parent.width - 16px; height: 32px; vertical-alignment: center; overflow: elide; text: root.batch-status; color: Theme.caption; }
    }
    if root.results-banner != "": ShallowPanel { x: 28px; y: root.height - 133px; width: root.width - 56px; height: 56px;
        Label { x: 12px; y: 8px; width: parent.width - 92px; height: 40px; text: root.results-banner; wrap: word-wrap; color: Theme.caption; }
        Button { x: parent.width - 68px; y: 14px; width: 56px; min-width: 0px; height: 28px; text: "Close"; clicked => { root.results-banner = ""; } }
    }
    if title-touch.has-hover && !title-touch.pressed: GameTooltip { x: 20px; y: 48px; wrap-width: root.width - 256px; text: root.data-folder; }
    }
    if root.lightbox-open: Lightbox {
        width: root.width; height: root.height; source: root.lightbox-source;
        zoom <=> root.lightbox-zoom;
        software: root.lightbox-software;
        rasterize(image, zoom, pan-x, pan-y, width, height) => { return root.lightbox-raster(image, zoom, pan-x, pan-y, width, height); }
        closed => { root.lightbox-open = false; scope.focus(); }
    }
    if root.operation-dialog: OperationDialog { width: root.width; height: root.height;
        title: root.operation-title; lines: root.operation-lines; confirmable: root.operation-confirmable;
        accepted => { root.operation-accept(); } back => { root.operation-back(); }
    }
    if root.onboarding-open && !root.settings-open: OnboardingDialog {
        width: root.width; height: root.height; folders: root.detected-data-folders; picked(i) => { root.data-folder-picked(i); }
        data-dir: root.data-folder; install-dir: root.install-folder; error: root.settings-error;
        change => { root.settings-requested(); } continued => { root.onboarding-continue(); }
    }
    if root.settings-open: SettingsDialog {
        width: root.width; height: root.height;
        verify-downloads <=> root.settings-verify;
        sounds <=> root.settings-sounds; volume <=> root.settings-volume; install-dir <=> root.settings-install; data-dir <=> root.settings-data;
        error: root.settings-error; environment-note: root.settings-environment-note;
        saved => { root.settings-save(); if (!root.settings-open) { scope.focus(); } }
        cancelled => { root.settings-cancel(); scope.focus(); }
    }
}
