// 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 } from "styling.slint";
import { LineEditBase} from "../common/lineedit-base.slint";

export component LineEdit {
    in property <bool> enabled <=> i-base.enabled;
    in property <InputType> input-type <=> i-base.input-type;
    in property <TextHorizontalAlignment> horizontal-alignment <=> i-base.horizontal-alignment;
    in property <bool> read-only <=> i-base.read-only;
    in property <length> font-size <=> i-base.font-size;
    in property <string> placeholder-text <=> i-base.placeholder-text;
    out property <bool> has-focus <=> i-base.has-focus;
    in-out property <string> text <=> i-base.text;

    callback accepted <=> i-base.accepted;
    callback edited <=> i-base.edited;
    accessible-role: text-input;
    accessible-value <=> text;
    accessible-label: placeholder-text;
    accessible-action-set-value(v) => { text = v; edited(v); }

    public function set-selection-offsets(start: int, end: int) {
        i-base.set-selection-offsets(start, end);
    }

    public function select-all() {
        i-base.select-all();
    }

    public function clear-selection() {
        i-base.clear-selection();
    }

    public function cut() {
        i-base.cut();
    }

    public function copy() {
        i-base.copy();
    }

    public function paste() {
        i-base.paste();
    }

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

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

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

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

            i-base := LineEditBase {
                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: i-layout.padding-left + i-layout.padding-right;
            }
        }

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