// 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 { Icons, CosmicPalette, CosmicFontSettings } from "styling.slint";

export component StateLayerBase {
    in property <length> border-radius <=> overlay.border-radius;
    in property <bool> has-focus;
    in property <bool> pressed;
    in property <bool> has-hover;
    in property <bool> checked;
    in property <bool> enabled: true;
    in property <length> focus-boder-margin: 3px;
    out property <brush> background <=> overlay.background;

    @children

    overlay := Rectangle {}

    if (root.has-focus && root.enabled) : Rectangle {
        width: root.width + root.focus-boder-margin * 2;
        height: root.height + root.focus-boder-margin * 2;
        border-width: 1px;
        border-radius: root.border-radius + root.focus-boder-margin;
        border-color: CosmicPalette.state-focus;
    }

    states [
        pressed when root.pressed : {
            overlay.background: CosmicPalette.state-pressed;
        }
        hover when root.has-hover : {
            overlay.background: CosmicPalette.state-hover;
        }
        checked when root.checked : {
            overlay.background: CosmicPalette.state-selected;
        }
    ]
}

export component StateLayer inherits TouchArea {
    in property <length> border-radius <=> base.border-radius;
    out property <bool> has-focus: focus-scope.has-focus;
    in-out property <bool> checked;
    in property <length> focus-boder-margin <=> base.focus-boder-margin;

    forward-focus: focus-scope;

    focus-scope := FocusScope {
        x: 0;
        width: 0; // Do not react on clicks
        enabled <=> root.enabled;

        key-pressed(event) => {
            if (event.text == " " || event.text == "\n") {
                root.clicked();
                 return accept;
            }

            return reject;
        }
    }

   base := StateLayerBase {
        width: 100%;
        height: 100%;
        has-focus: root.has-focus;
        pressed: root.pressed;
        has-hover: root.has-hover;
        checked: root.checked;
        enabled: root.enabled;

        @children
   }
}

export component MenuBorder inherits Rectangle {
    border-radius: 8px;
    background: CosmicPalette.alternate-background;
    drop-shadow-blur: 16px;
    drop-shadow-offset-y: 4px;
    drop-shadow-color: CosmicPalette.shadow;

    Rectangle {
        border-width: 1px;
        border-radius: parent.border-radius;
        border-color: CosmicPalette.control-divider;
    }
}

export component ListItem {
    in property <bool> is-selected;
    in property <StandardListViewItem> item;
    in property <bool> has-focus;
    in property <bool> has-hover;
    in property <bool> pressed;
    in property <int> index;
    in property <length> pressed-x;
    in property <length> pressed-y;

    min-width: layout.min-width;
    min-height: max(40px, layout.min-height);
    vertical-stretch: 0;
    horizontal-stretch: 1;

    states [
        is-selected when root.is-selected : {
           text.color: CosmicPalette.accent-text;
        }
    ]

    layout := HorizontalLayout {
        padding-bottom: 8px;

        StateLayerBase {
            width: 100%;
            pressed: root.pressed;
            has-hover: root.has-hover;
            has-focus: root.has-focus;
            checked: root.is-selected;
            border-radius: 16px;
            focus-boder-margin: 0;

            HorizontalLayout {
                padding-left: 16px;
                padding-right: 16px;
                spacing: 8px;

                text := Text {
                    text: root.item.text;
                    color: CosmicPalette.control-foreground;
                    font-size: CosmicFontSettings.body.font-size;
                    font-weight: CosmicFontSettings.body.font-weight;
                    vertical-alignment: center;
                    horizontal-alignment: left;
                    overflow: elide;
                }

                Image {
                    width: 16px;
                    height: 16px;
                    y: (parent.height - self.height) / 2;
                    visible: root.is-selected;
                    colorize: text.color;
                    source: Icons.check-mark;
                }
            }

            @children
        }
    }
}
