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

import { FluentFontSettings, FluentPalette } from "styling.slint";
import { LineEditBase, LineEditClearIcon, LineEditPasswordIcon } from "../common/lineedit-base.slint";

import { LineEditInterface } from "../common/std-widget-interfaces.slint";
import { Palette } from "std-widgets.slint";
import { Data } from "../../data.slint";

export component LineEdit uses { LineEditInterface from base } {

    in property <brush> border-color;
    callback on-focus-change();

    accessible-role: text-input;
    accessible-enabled: root.enabled;
    accessible-value <=> text;
    accessible-placeholder-text: placeholder-text;
    accessible-read-only: root.read-only;
    accessible-action-set-value(v) => { text = v; edited(v); }

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

    states [
        disabled when !root.enabled : {
            background.background: FluentPalette.control-disabled;
            base.text-color: FluentPalette.text-disabled;
            base.selection-foreground-color: FluentPalette.text-accent-foreground-disabled;
            base.placeholder-color: FluentPalette.text-disabled;
        }
        focused when root.has-focus : {
            background.background: FluentPalette.control-input-active;
            focus-border.background: FluentPalette.accent-background;
        }
    ]

    background := Rectangle {
        border-radius: 4px;
        background: FluentPalette.control-background;
        border-width: 1px;
        border-color: root.has-focus ? FluentPalette.text-control-border.mix(root.border-color, 0.65) : FluentPalette.text-control-border;

        animate border-color {
            duration: 150ms;
        }

        drop-shadow-blur: 5px;
        drop-shadow-color: rgba(10, 10, 10, 0.25);
        drop-shadow-offset-y: -1px;

        layout := HorizontalLayout {
            padding-left: 12px;
            padding-right: 12px;

            base := LineEditBase {
                changed has-focus => {
                    root.on-focus-change();
                }
                font-size: FluentFontSettings.body.font-size;
                font-weight: FluentFontSettings.body.font-weight;
                selection-background-color: FluentPalette.selection-background;
                selection-foreground-color: FluentPalette.accent-foreground;
                text-color: FluentPalette.foreground;
                placeholder-color: FluentPalette.text-secondary;
                margin: layout.padding-left + layout.padding-right;
                horizontal-stretch: 1;
            }

            if !root.text.is-empty && root.input-type != InputType.password && root.enabled && !root.read-only && root.has-focus: LineEditClearIcon {
                width: 16px;
                text: base.text;
                source: @image-url("_dismiss.svg");
                colorize: base.text-color;
                clear => {
                    base.text = "";
                    root.edited("");
                    base.focus();
                }
            }

            if root.input-type == InputType.password && !root.text.is-empty && root.has-focus: LineEditPasswordIcon {
                width: self.source.width * 1px;
                show-password-image: @image-url("_eye_show.svg");
                hide-password-image: @image-url("_eye_hide.svg");
                colorize: base.text-color;
                show-password <=> base.password-revealed;
            }

            Image {
                source: @image-url("caps.svg");
                width: self.source.width * 1px;
                colorize: base.text-color;
                visible: Data.caps-lock-state && root.has-focus;
            }
        }

        focus-border := Rectangle {
            x: parent.border-radius;
            y: parent.height - self.height;
            width: parent.width - 2 * parent.border-radius;
            height: 2px;
            border-radius: 1px;
        }
    }
}