// Copyright © SixtyFPS GmbH <info@slint.dev>
// SPDX-License-Identifier: MIT

import { Switch, ComboBox, Button, HorizontalBox, VerticalBox, GroupBox } from "std-widgets.slint";
import { Page } from "page.slint";
import { State } from "localgame_page.slint";

export global ControllerData {
    in-out property <[bool]> controller_enabled;
    in-out property <[bool]> transferpak;
    in-out property <[string]> gb_rom_paths;
    in-out property <[string]> gb_ram_paths;
    in-out property <[int]> selected_profile_binding: [-1, -1, -1, -1];
    in-out property <[string]> input_profiles;
    in-out property <[string]> controller_names;
    in-out property <[string]> controller_paths;
    in-out property <[int]> selected_controller;
    in-out property <[bool]> controller_changed;
}

component ControllerPortLabel inherits Text {
    height: 32px;
    width: 36px;
    vertical-alignment: center;
    horizontal-alignment: center;
    font-weight: 600;
}

component ControllerFieldHeader inherits Text {
    height: 28px;
    horizontal-alignment: left;
    vertical-alignment: center;
    font-size: 13px;
}

component ControllerSettingsColumn inherits VerticalLayout {
    in property <string> title;
    in property <length> content-min-width: 0px;
    spacing: 6px;
    alignment: start;
    horizontal-stretch: 0;
    min-width: content-min-width > header.preferred-width ? content-min-width : header.preferred-width;

    header := ControllerFieldHeader {
        text: root.title;
    }

    @children
}

component ControllerSettingsCell inherits HorizontalLayout {
    height: 32px;
    alignment: start;

    @children
}

component ControllerSettingsGrid inherits HorizontalLayout {
    callback transferpak_toggled(int, bool);

    spacing: 12px;
    alignment: start;

    VerticalLayout {
        spacing: 6px;
        alignment: start;
        horizontal-stretch: 0;

        Rectangle {
            width: 36px;
            height: 28px;
        }

        for player[index] in [0, 1, 2, 3]: ControllerPortLabel {
            text: index + 1;
        }
    }

    ControllerSettingsColumn {
        title: @tr("ControllerSettingsGrid" => "Enabled");

        for player[index] in [0, 1, 2, 3]: ControllerSettingsCell {
            Switch {
                checked: ControllerData.controller_enabled[index];
                toggled => {
                    ControllerData.controller_enabled[index] = self.checked;
                }
            }
        }
    }

    ControllerSettingsColumn {
        title: @tr("ControllerSettingsGrid" => "Transfer Pak");

        for player[index] in [0, 1, 2, 3]: ControllerSettingsCell {
            Switch {
                checked: ControllerData.transferpak[index];
                toggled => {
                    ControllerData.transferpak[index] = self.checked;
                    root.transferpak_toggled(index, self.checked);
                }
            }
        }
    }

    ControllerSettingsColumn {
        title: @tr("ControllerSettingsGrid" => "Profile");
        content-min-width: 150px;

        for player[index] in [0, 1, 2, 3]: ControllerSettingsCell {
            ComboBox {
                horizontal-stretch: 0;
                min-width: self.preferred-width > 150px ? self.preferred-width : 150px;
                model: ControllerData.input_profiles;
                current-index: ControllerData.selected_profile_binding[index];
                selected => {
                    ControllerData.selected_profile_binding[index] = self.current-index;
                }
            }
        }
    }
}

component ControllerAssignmentGrid inherits VerticalLayout {
    spacing: 6px;
    alignment: start;

    for player[index] in [0, 1, 2, 3]: HorizontalLayout {
        spacing: 8px;
        alignment: start;

        ControllerPortLabel {
            text: index + 1;
        }

        HorizontalLayout {
            alignment: start;

            ComboBox {
                horizontal-stretch: 0;
                min-width: self.preferred-width > 300px ? self.preferred-width : 300px;
                model: ControllerData.controller_names;
                current-index: ControllerData.selected_controller[index];
                selected => {
                    ControllerData.selected_controller[index] = self.current-index;
                    ControllerData.controller_changed[index] = true;
                }
            }
        }
    }
}

export component ControllerConfig inherits Page {
    callback input_profile_button_clicked;
    callback transferpak_toggled(int, bool);
    callback controller_window_created;
    title: @tr("Controller Configuration");

    init => {
        controller_window_created();
    }

    VerticalBox {
        spacing: 8px;
        alignment: start;

        HorizontalBox {
            alignment: start;

            Button {
                text: @tr("Configure Input Profile");
                enabled: !State.game_running;
                clicked => {
                    input_profile_button_clicked();
                }
            }
        }

        GroupBox {
            title: @tr("Controller Settings");

            ControllerSettingsGrid {
                transferpak_toggled(index, enabled) => {
                    root.transferpak_toggled(index, enabled);
                }
            }
        }

        GroupBox {
            title: @tr("Controller Assignment");

            ControllerAssignmentGrid { }
        }
    }
}
