import { Label, Theme } from "base.slint";

export struct DescriptionImage {
    alt: string, url: string, link: string, state: int,
    frames: [image], delays: [int], playhead: [int],
    x: float, y: float, width: float, height: float,
}
export struct DescriptionBlock {
    kind: int, level: int, depth: int, text: styled-text, marker: string, images: [DescriptionImage], row-height: float,
}
export struct GalleryImage { thumbnail: image, source: image, missing: bool, url: string }
export struct DetailInfo { key: string, value: string, url: string }
export struct PortalDependency { name: string, text: string, optional: bool, incompatible: bool, hidden: bool, red: bool, installed: bool }
export struct PortalDetail {
    name: string, title: string, blocks: [DescriptionBlock], gallery: [GalleryImage],
    info: [DetailInfo], dependencies: [PortalDependency], thumbnail: image, missing: bool, tags: string, has-changelog: bool,
}

export global DescriptionImages {
    in-out property <int> capture-frame: -1;
    in-out property <bool> paused;
    in-out property <float> pane-width: 560;
    callback layout([DescriptionBlock], float);
    callback clear-cache();
    callback shown(string);
}

export component DescriptionView {
    in property <[DescriptionBlock]> blocks;
    in property <length> viewport-top;
    in property <length> viewport-height;
    init => { DescriptionImages.pane-width = root.width / 1px; DescriptionImages.layout(root.blocks, root.width / 1px); }
    changed width => { DescriptionImages.pane-width = root.width / 1px; DescriptionImages.layout(root.blocks, root.width / 1px); }
    changed blocks => { DescriptionImages.layout(root.blocks, root.width / 1px); }
    callback open-url(string);
    callback zoom-image(image);
    preferred-height: layout.preferred-height;
    layout := VerticalLayout {
        width: root.width; spacing: 0px; padding: 0px;
        for block in root.blocks: Rectangle {
            property <bool> heading: block.kind == 1;
            property <bool> code: block.kind == 3;
            property <length> inset: min(32, block.depth) * 32px + (block.kind == 2 ? 32px : 0px);
            property <length> margin: self.heading ? (block.level == 3 ? 16px : 12px) : block.kind == 2 ? 8px : 12px;
            height: block.kind == 4 ? 30px : block.kind == 5 ? block.row-height * 1px + 12px : text.preferred-height + self.margin;

            if block.kind == 2 && block.marker == "": Image {
                x: parent.inset - 18px; y: 5px; width: 10px; height: 10px;
                source: @image-url("../../../assets/portal/data-01.png");
            }
            if block.kind == 2 && block.marker != "": Label { x: parent.inset - 26px; y: 0px; text: block.marker; font-size: 16px; }
            text := StyledText {
                visible: block.kind != 4 && block.kind != 5;
                x: parent.inset + (parent.code ? 8px : 0px); y: 0px;
                width: parent.width - self.x - (parent.code ? 8px : 0px);
                text: block.text;
                default-font-family: parent.code ? "Consolas" : Theme.font;
                default-font-size: parent.heading ? (block.level == 1 ? 20.8px : block.level == 2 ? 19.2px : 18.56px) : parent.code ? 17.6px : 16px;
                default-color: parent.heading || parent.code ? #ffe6c0 : #ffffff;
                link-color: parent.heading ? #ffe6c0 : #7dcaed;
                link-clicked(url) => { root.open-url(url); }
            }
            if block.kind == 4: Rectangle { x: 0px; y: 12px; width: parent.width; height: 6px; background: #232223; }
            for item in block.images: Rectangle {
                x: item.x * 1px; y: item.y * 1px;
                width: item.width * 1px; height: item.height * 1px;
                property <int> frame: DescriptionImages.capture-frame >= 0 ? min(DescriptionImages.capture-frame, item.frames.length - 1) : min(item.playhead[0], item.frames.length - 1);
                property <bool> in-view: parent.y + self.y + self.height > root.viewport-top
                    && parent.y + self.y < root.viewport-top + root.viewport-height;
                property <bool> shown: self.in-view && item.state == 1;
                init => { if (self.shown) { DescriptionImages.shown(item.url); } }
                changed shown => { if (self.shown) { DescriptionImages.shown(item.url); } }
                property <bool> playing: self.in-view && !DescriptionImages.paused && DescriptionImages.capture-frame < 0 && item.frames.length > 1;
                Timer {
                    running: parent.playing;
                    interval: max(1, item.delays[parent.frame]) * 1ms;
                    triggered => { item.playhead[0] = mod(item.playhead[0] + 1, item.frames.length); }
                }
                picture := Image { visible: item.state == 1; width: parent.width; height: parent.height; source: item.frames[parent.frame]; image-fit: contain; }
                if item.state != 1: Label { width: parent.width; text: item.alt == "" ? "Image" : item.alt; color: item.state == 2 ? #7dcaed : Theme.caption; overflow: elide; }
                TouchArea {
                    enabled: item.state != 0; mouse-cursor: pointer;
                    clicked => {
                        if (item.link != "") { root.open-url(item.link); }
                        else if (item.state == 2) { root.open-url(item.url); }
                        else { root.zoom-image(picture.source); }
                    }
                }
            }
        }
    }
}
