import { Nine, Label, Theme } from "base.slint";
import { Pieces } from "pieces.slint";
import { ShallowPanel, SubheaderFrame } from "frame.slint";
import { InfoTable, InfoRow } from "info-table.slint";
import { ThumbnailFrame } from "thumbnail.slint";
import { Button } from "button.slint";
import { HorizontalScrollbar } from "hscrollbar.slint";
import { Scrollbar } from "scrollbar.slint";
import { PortalDetail, DescriptionView } from "description.slint";
import { LoadingOverlay } from "loading.slint";
import { ModAction } from "mod-action.slint";
import { DependencyLine } from "link.slint";
import { UiCue } from "audio.slint";

export component DetailsPane {
    in property <int> action;
    in property <bool> busy;
    in property <bool> downloading;
    in property <float> fraction;
    in property <string> progress-label;
    callback activate(bool);
    callback cancel();
    in property <bool> installed;
    callback manage-focus(string);
    property <length> action-height: 44px;
    in property <string> title;
    in property <string> description;
    in property <string> tags;
    in property <[InfoRow]> info;
    in property <image> thumbnail;
    in property <bool> missing: true;
    in property <bool> has-changelog;
    in property <bool> loading;
    in property <PortalDetail> portal;
    in-out property <int> gallery-open: -1;
    in-out property <length> scroll-y;
    callback open-changelog();
    callback dependency-activated(string);
    callback open-url(string);
    callback zoom-image(image);
    property <bool> gallery-drag;
    property <string> identity: root.portal.name;
    changed identity => { root.gallery-open = -1; root.scroll-y = 0px; }
    ShallowPanel {
        x: 0px; y: 0px; width: root.width; height: root.height;
        SubheaderFrame { x: 0px; y: 0px; width: parent.width; height: 36px;
            Label { x: 12px; y: 5px; width: parent.width - 164px; height: 24px; text: root.portal.name != "" ? root.portal.title : root.title; font-weight: 700; color: Theme.caption; overflow: elide; }
            Rectangle { x: parent.width - 146px; y: 3px; width: 28px; height: 28px;
                Button { width: parent.width; height: parent.height; enabled: root.portal.name != ""; clicked => { root.open-url("https://mods.factorio.com/mod/" + root.portal.name); } }
                Image { x: 6px; y: 6px; width: 16px; height: 16px; source: @image-url("../../../assets/game/icons1x/open-browser.png"); image-rendering: pixelated; }
            }
            Button { x: parent.width - 112px; y: 3px; width: 108px; height: 28px; text: "Changelog"; enabled: root.portal.name != "" ? root.portal.has-changelog : root.has-changelog; clicked => { root.open-changelog(); } }
        }
        body := Flickable {
            interactive: !root.gallery-drag;
            x: 8px; y: 44px + root.action-height; width: parent.width - 24px; height: parent.height - 56px - root.action-height;
            content-y <=> root.scroll-y;
            content-width: self.width;
            content-height: max(self.height, article.y + article.height + 4px);
            metadata := VerticalLayout {
                x: 4px; y: 0px; width: parent.width - 164px; spacing: 0px;
                for row in root.portal.info: Rectangle {
                    height: 32px;
                    background: #414040;
                    Rectangle { x: 0px; y: 0px; width: parent.width; height: 1px; background: #888888; }
                    Rectangle { x: 0px; y: 31px; width: parent.width; height: 1px; background: #232223; }
                    Label { x: 8px; y: 5px; width: 112px; height: 24px; text: row.key; }
                    Label { x: 128px; y: 5px; width: parent.width - 136px; height: 24px; text: row.value.replace-all("//", "/\u{200a}/"); color: row.url == "" ? Theme.text : #7dcaed; overflow: elide; }
                    if row.url != "": TouchArea { mouse-cursor: pointer; clicked => { root.open-url(row.url); } }
                }
            }
            fallback := InfoTable { x: 4px; y: 0px; width: parent.width - 164px; height: root.portal.name == "" ? root.info.length * 32px + 4px : 0px; visible: root.portal.name == ""; rows: root.info; dependencies: []; activated(i) => { if (root.info[i].link) { root.open-url(root.info[i].value); } } }
            ThumbnailFrame { x: parent.width - 148px; y: 0px; width: 144px; height: 144px; source: root.portal.name != "" ? root.portal.thumbnail : root.thumbnail; missing: root.portal.name != "" ? root.portal.missing : root.missing; }
            dependencies := VerticalLayout {
                x: 4px; y: max(152px, root.portal.name != "" ? metadata.y + metadata.preferred-height + 8px : fallback.y + fallback.height + 8px);
                width: parent.width - 8px; height: self.preferred-height; spacing: 4px;
                if root.portal.dependencies.length > 0: Label { text: "Dependencies"; color: Theme.caption; font-weight: 700; }
                for dep in root.portal.dependencies: DependencyLine {
                    text: dep.text; wrap-text: true;
                    optional: dep.optional; required: !dep.optional; red: dep.red; incompatible: dep.incompatible;
                    installed: dep.installed; opacity: dep.hidden ? 0.5 : 1;
                    clicked => { root.dependency-activated(dep.name); }
                    manage-focus => { root.manage-focus(dep.name); }
                }
            }
            gallery := Flickable {
                interactive: false;
                x: 0px; y: dependencies.y + dependencies.preferred-height + (root.portal.dependencies.length > 0 ? 8px : 0px);
                width: parent.width; height: root.portal.gallery.length > 0 ? 168px : 0px;
                content-height: self.height; content-width: strip.preferred-width;
                TouchArea {
                    property <length> anchor;
                    changed pressed => { root.gallery-drag = self.pressed; self.anchor = self.mouse-x; }
                    moved => { if (self.pressed) { gallery.content-x = clamp(gallery.content-x + self.mouse-x - self.anchor, min(0px, gallery.width - gallery.content-width), 0px); } }
                    x: 0px; y: 0px; width: max(gallery.width, strip.preferred-width); height: gallery.height;
                    scroll-event(event) => { if (gallery.content-width <= gallery.width) { return reject; } gallery.content-x = clamp(gallery.content-x + event.delta-x + event.delta-y, min(0px, gallery.width - gallery.content-width), 0px); return accept; }
                }
                strip := HorizontalLayout {
                    spacing: 12px; padding-left: 6px; padding-right: 6px;
                    for item[i] in root.portal.gallery: Rectangle {
                        width: 148px; height: 148px; y: 8px;
                        background: #414040;
                        border-radius: 2px;
                        Image { x: 2px; y: 2px; width: 144px; height: 144px; source: item.thumbnail; image-fit: contain; }
                        Nine { x: 2px; y: 2px; width: 144px; height: 144px; source: Pieces.mod-thumbnail-image-default-shadow; }
                        Rectangle { width: parent.width; height: parent.height; border-width: 2px; border-color: root.gallery-open == i || touch.has-hover ? #ffa900 : transparent; }
                        if item.missing: Label { x: 8px; y: 60px; width: 132px; text: "Image"; horizontal-alignment: center; }
                        touch := TouchArea { mouse-cursor: pointer;
                            property <length> anchor;
                            changed pressed => { root.gallery-drag = self.pressed; self.anchor = self.mouse-x; }
                            moved => { if (self.pressed) { gallery.content-x = clamp(gallery.content-x + self.mouse-x - self.anchor, min(0px, gallery.width - gallery.content-width), 0px); } }
                            scroll-event(event) => { if (gallery.content-width <= gallery.width) { return reject; } gallery.content-x = clamp(gallery.content-x + event.delta-x + event.delta-y, min(0px, gallery.width - gallery.content-width), 0px); return accept; }
                            clicked => { root.gallery-open = root.gallery-open == i ? -1 : i; } }
                    }
                }
            }
            if gallery.content-width > gallery.width: HorizontalScrollbar {
                x: gallery.x; y: gallery.y + gallery.height; width: gallery.width; height: 12px;
                changed dragging => { root.gallery-drag = self.dragging; }
                thumb-width: max(24px, self.width * gallery.width / gallery.content-width);
                thumb-position: -gallery.content-x / max(1px, gallery.content-width - gallery.width) * (self.width - self.thumb-width);
                moved(x) => { gallery.content-x = -clamp((x - self.thumb-width / 2) / max(1px, self.width - self.thumb-width), 0, 1) * (gallery.content-width - gallery.width); }
                scroll(dx, dy) => { gallery.content-x = clamp(gallery.content-x + dx + dy, min(0px, gallery.width - gallery.content-width), 0px); }
            }
            // Natural size, shrunk to fit the pane but never enlarged (the website does the same).
            open-picture := Image {
                property <image> picture: root.portal.gallery[max(0, root.gallery-open)].source;
                property <length> room: parent.width - 8px;
                width: self.picture.width > 0 ? min(self.room, self.picture.width * 1px) : self.room;
                x: 4px + (self.room - self.width) / 2; y: gallery.y + gallery.height + (gallery.content-width > gallery.width ? 16px : 0px);
                height: root.gallery-open < 0 ? 0px : self.picture.width > 0 ? self.width * self.picture.height / self.picture.width : 80px;
                visible: root.gallery-open >= 0; source: self.picture; image-fit: contain;
            }
            if root.gallery-open >= 0: Nine { x: open-picture.x; y: open-picture.y; width: open-picture.width; height: open-picture.height; source: Pieces.mod-thumbnail-image-default-shadow; }
            if root.gallery-open >= 0 && open-picture.picture.width == 0: Label { x: 12px; y: open-picture.y + 12px; text: "Image"; }
            if root.gallery-open >= 0 && open-picture.picture.width > 0: TouchArea {
                x: open-picture.x; y: open-picture.y; width: open-picture.width; height: open-picture.height;
                mouse-cursor: pointer; clicked => { root.zoom-image(open-picture.picture); }
            }
            article := Rectangle {
                x: 4px; y: open-picture.y + open-picture.height + 12px; width: parent.width - 8px;
                height: root.portal.name != "" ? formatted.preferred-height + (root.portal.tags == "" ? 0px : 30px) : plain.preferred-height;
                formatted := DescriptionView { viewport-top: -body.content-y - article.y; viewport-height: body.height; width: parent.width; blocks: root.portal.blocks; visible: root.portal.name != ""; open-url(url) => { root.open-url(url); } zoom-image(image) => { root.zoom-image(image); } }
                plain := Label { width: parent.width; text: root.description; wrap: word-wrap; visible: root.portal.name == ""; }
                if root.portal.tags != "": Label { y: formatted.preferred-height + 8px; text: "Tags: " + root.portal.tags; color: Theme.caption; }
            }
        }
        if body.content-height > body.height: Scrollbar {
            x: parent.width - 12px; y: 44px + root.action-height; 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); }
        }
        LoadingOverlay { x: 0px; y: 36px; width: parent.width; height: parent.height - 36px; active: root.loading; }
        if root.action-height > 0px: ModAction { x: 16px; y: 44px; width: parent.width - 32px; height: 28px;
            action: root.action; busy: root.busy; downloading: root.downloading; fraction: root.fraction; progress-label: root.progress-label;
            activate(remove) => { root.activate(remove); } cancel => { root.cancel(); }
        }
        if root.installed && !root.downloading: Button { sound: UiCue.tool; x: parent.width - 264px; y: 44px; width: 132px; height: 28px; text: "Show in Manage"; clicked => { root.manage-focus(root.portal.name); } }
        Rectangle { x: 0px; y: 0px; width: parent.width; height: 36px; clip: true; Nine { x: 0px; y: 0px; width: root.width; height: root.height; source: Pieces.inside-shallow-frame-default-shadow; } }
    }
}
