// Copyright © SixtyFPS GmbH <info@slint.dev>
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-1.2 OR LicenseRef-Slint-commercial

import { FluentFontSettings, FluentPalette, Icons } from "styling.slint";
import { MenuBorder, ListItem, FocusBorder } from "components.slint";
import { ComboBoxBase } from "../common/combobox-base.slint";

export component ComboBox {
    in property <[string]> model <=> i-base.model;
    in property <bool> enabled <=> i-base.enabled;
    out property <bool> has-focus <=> i-base.has-focus;
    in-out property <int> current-index <=> i-base.current-index;
    in-out property <string> current-value <=> i-base.current-value;

    callback selected <=> i-base.selected;

    min-width: max(160px, i-layout.min-height);
    min-height: max(32px, i-layout.min-height);
    horizontal-stretch: 1;
    vertical-stretch: 0;
    forward-focus: i-base;

    accessible-role: combobox;

    states [
        disabled when !root.enabled : {
            i-background.background: FluentPalette.control-disabled;
            i-background.border-color: FluentPalette.border;
            i-text.color: FluentPalette.text-disabled;
            i-icon.colorize: FluentPalette.text-disabled;
        }
        pressed when i-base.pressed : {
            i-background.background: FluentPalette.control-alt-tertiary;
            i-background.border-color: FluentPalette.border;
            i-text.color: FluentPalette.text-secondary;
            i-icon.colorize: FluentPalette.text-tertiary;
        }
        hover when i-base.has-hover : {
            i-background.background: FluentPalette.control-secondary;
        }
    ]

    i-base := ComboBoxBase {
        width: 100%;
        height: 100%;

        show-popup => {
            i-popup.show();
        }
    }

    i-background := Rectangle {
        border-radius: 3px;
        background: FluentPalette.control-background;
        border-width: 1px;
        border-color: FluentPalette.control-border;

        animate border-color { duration: 200ms; }

        i-layout := HorizontalLayout {
            padding-left: 11px;
            padding-right: 11px;
            spacing: 8px;

            i-text := Text {
                horizontal-alignment: left;
                vertical-alignment: center;
                font-size: FluentFontSettings.body.font-size;
                font-weight: FluentFontSettings.body.font-weight;
                color: FluentPalette.control-foreground;
                text: root.current-value;
            }

            i-icon := Image {
                colorize: FluentPalette.text-secondary;
                width: 12px;
                source: Icons.dropdown;
                y: 2px;

                animate colorize { duration: 150ms; }
            }
        }
    }

    // focus border
    if (root.has-focus && root.enabled) : FocusBorder {
        border-radius: i-background.border-radius;
    }

    i-popup := PopupWindow {
        x: 0;
        // Position the popup so that the first element is over the popup.
        // Ideally it should be so that the current element is over the popup.
        y: -4px;
        width: root.width;

        MenuBorder {
            VerticalLayout {
                padding: 4px;

                for value[index] in root.model : ListItem {
                    item: { text: value };
                    is-selected: index == root.current-index;
                    has-hover: i-touch-area.has-hover;
                    pressed: i-touch-area.pressed;

                    i-touch-area := TouchArea {
                        clicked => {
                            i-base.select(index);
                        }
                    }
                }
            }
        }
    }
}
