import { DescriptionImages } from "widgets/description.slint";
import { Scrollbar } from "widgets/scrollbar.slint";
import { UiSounds, UiCue } from "widgets/audio.slint";
import { VolumeSlider } from "widgets/volume-slider.slint";
import { Frame, FrameTitle, RibStrip, ShallowPanel } from "widgets/frame.slint";
import { Label, Theme } from "widgets/base.slint";
import { Checkbox } from "widgets/checkbox.slint";
import { Button } from "widgets/button.slint";
import { TextField } from "widgets/text-field.slint";
import { BackButton, ConfirmButton } from "widgets/dialog-button.slint";

export component SettingsDialog {
    in-out property <bool> verify-downloads: true;
    in-out property <bool> sounds: true;
    in-out property <int> volume: 50;
    in-out property <string> data-dir;
    in-out property <string> install-dir;
    in property <string> error;
    in property <string> environment-note;
    callback saved(); callback cancelled();
    Rectangle { width: root.width; height: root.height; background: #00000080; }
    TouchArea { width: root.width; height: root.height; }
    FocusScope {
        width: root.width; height: root.height;
        key-pressed(event) => { if (event.text == Key.Escape) { UiSounds.begin(); root.cancelled(); UiSounds.finish(UiCue.click); return accept; } return reject; }
        Frame {
            x: (root.width - self.width) / 2; y: (root.height - self.height) / 2;
            width: min(740px, root.width - 48px); height: min(620px, root.height - 48px);
            heading := FrameTitle { x: 12px; y: 5px; text: "Settings"; }
            RibStrip { x: heading.x + heading.width + 12px; y: 8px; width: parent.width - self.x - 12px; height: 24px; header: true; }
            ShallowPanel {
                x: 12px; y: 40px; width: parent.width - 24px; height: parent.height - 100px;
                body := Flickable {
                    width: parent.width - 12px; height: parent.height;
                    content-width: self.width; content-height: root.error == "" ? 486px : 536px;
                Label { x: 16px; y: 12px; text: "Factorio data folder"; color: Theme.caption; font-weight: 600; }
                TextField { x: 16px; y: 38px; width: parent.width - 32px; height: 28px; text <=> root.data-dir; autofocus: true; }
                Label { x: 16px; y: 72px; width: parent.width - 32px; height: 40px; wrap: word-wrap; text: "The parent directory of your mods and saves folders, don't edit unless you know what you're doing"; color: Theme.caption; }
                Label { x: 16px; y: 128px; text: "Factorio install folder (optional)"; color: Theme.caption; font-weight: 600; }
                TextField { x: 16px; y: 154px; width: parent.width - 32px; height: 28px; text <=> root.install-dir; }
                Label { x: 16px; y: 188px; width: parent.width - 32px; height: 40px; wrap: word-wrap; text: "If missing, you'll need to enable base content (Space age, etc.) from within the game itself"; color: Theme.caption; }
                Checkbox { x: 16px; y: 252px; checked: root.verify-downloads; toggled => { root.verify-downloads = !root.verify-downloads; } }
                Label { x: 44px; y: 248px; text: "Verify all downloads"; }
                TouchArea { x: 40px; y: 248px; width: 220px; height: 24px; mouse-cursor: pointer; clicked => { UiSounds.begin(); root.verify-downloads = !root.verify-downloads; UiSounds.finish(UiCue.checkbox); } }
                Label { x: 16px; y: 280px; width: parent.width - 32px; height: 40px; wrap: word-wrap; text: "Turn this off if having issues when downloading mods. Check your internet connection first though."; color: Theme.caption; }
                Checkbox { x: 16px; y: 330px; checked: root.sounds; toggled => { root.sounds = !root.sounds; UiSounds.update(root.sounds, root.volume); } }
                Label { x: 44px; y: 326px; text: "UI sound effects"; }
                TouchArea { x: 40px; y: 326px; width: 220px; height: 24px; mouse-cursor: pointer; clicked => { UiSounds.begin(); root.sounds = !root.sounds; UiSounds.update(root.sounds, root.volume); UiSounds.finish(UiCue.checkbox); } }
                Label { x: 16px; y: 360px; text: "UI sound volume"; color: root.sounds ? Theme.text : Theme.disabled-text; }
                VolumeSlider { x: 164px; y: 358px; width: parent.width - 244px; height: 24px; enabled: root.sounds; value <=> root.volume; adjusted => { UiSounds.update(root.sounds, root.volume); } }
                Label { x: parent.width - 64px; y: 360px; width: 48px; text: root.volume + "%"; color: root.sounds ? Theme.text : Theme.disabled-text; horizontal-alignment: right; }
                Button { x: 16px; y: 400px; width: 144px; height: 28px; text: "Clear cache"; clicked => { DescriptionImages.clear-cache(); } }
                Label { x: 16px; y: 446px; width: parent.width - 32px; height: 40px; wrap: word-wrap; text: "Folder changes apply after restart. Sound changes apply immediately."; color: Theme.orange; }
                if root.error != "": Label { x: 16px; y: 490px; width: parent.width - 32px; height: 38px; wrap: word-wrap; overflow: elide; text: root.error; color: #ff8e8e; }
                }
                if body.content-height > body.height: Scrollbar {
                    x: parent.width - 12px; y: 4px; width: 12px; height: parent.height - 8px;
                    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, min(0px, body.height - body.content-height), 0px); }
                    moved(y) => { body.content-y = -clamp((y - self.thumb-height / 2) / max(1px, self.height - self.thumb-height), 0, 1) * max(0px, body.content-height - body.height); }
                }
            }

        BackButton { x: 12px; y: parent.height - 44px; width: 112px; height: 32px; text: "Cancel"; clicked => { root.cancelled(); } }
            RibStrip { x: 132px; y: parent.height - 44px; width: parent.width - 316px; height: 32px; }
            ConfirmButton { x: parent.width - 172px; y: parent.height - 44px; width: 160px; height: 32px; text: "Save settings"; clicked => { root.saved(); } }
        }
    }
}

export component OnboardingDialog {
    in property <[string]> folders;
    callback picked(int);
    in property <string> data-dir;
    in property <string> install-dir;
    in property <string> error;
    callback change(); callback continued();
    Rectangle { width: root.width; height: root.height; background: #00000080; }
    TouchArea { width: root.width; height: root.height; }
    Frame {
        x: (root.width - self.width) / 2; y: (root.height - self.height) / 2;
        width: min(740px, root.width - 48px); height: root.folders.length > 1 ? min(root.height - 48px, 230px + root.folders.length * 48px) : 370px;
        heading := FrameTitle { x: 12px; y: 5px; text: "Factorio folders"; }
        RibStrip { x: heading.x + heading.width + 12px; y: 8px; width: parent.width - self.x - 12px; height: 24px; header: true; }
        ShallowPanel {
            x: 12px; y: 40px; width: parent.width - 24px; height: parent.height - 100px;
            if root.folders.length <= 1: VerticalLayout { x: 16px; y: 12px; width: parent.width - 32px; spacing: 8px;
                Label { text: "The mod manager will use these folders:"; color: Theme.caption; }
                Label { text: "Factorio data folder"; color: Theme.caption; font-weight: 700; }
                Label { text: root.data-dir; wrap: word-wrap; }
                Label { text: "Factorio install folder (game version and built-in mods)"; color: Theme.caption; font-weight: 700; }
                Label { text: root.install-dir == "" ? "Not found — set it in Settings to enable compatible downloads." : root.install-dir; wrap: word-wrap; }
                Label { text: root.error; wrap: word-wrap; color: #ff8e8e; }
            }
        }
        if root.folders.length > 1: VerticalLayout { x: 28px; y: 52px; width: parent.width - 56px; height: self.preferred-height; spacing: 12px;
            Label { text: "Multiple Factorio installs detected. Choose one to continue."; color: Theme.caption; }
            for folder[i] in root.folders: Rectangle { height: 32px;
                Button { x: 0px; y: 0px; width: parent.width; height: 32px; clicked => { root.picked(i); } }
                Label { x: 12px; width: parent.width - 24px; height: parent.height; vertical-alignment: center; text: folder; overflow: elide; color: Theme.black; font-weight: 600; }
            }
        }
        BackButton { x: 12px; y: parent.height - 44px; width: 156px; height: 32px; text: root.folders.length > 1 ? "Custom folder…" : "Change…"; clicked => { root.change(); } }
        RibStrip { x: 176px; y: parent.height - 44px; width: parent.width - 360px; height: 32px; }
        if root.folders.length <= 1: ConfirmButton { x: parent.width - 172px; y: parent.height - 44px; width: 160px; height: 32px; text: "Continue"; clicked => { root.continued(); } }
    }
}
