// 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 { CupertinoPalette, CupertinoFontSettings, Icons } from "styling.slint";
import { FocusBorder } from "components.slint";
import { SpinBoxBase } from "../common/spinbox-base.slint";

component SpinBoxButton {
    in property <bool> enabled <=> i-touch-area.enabled;
    in property <image> icon <=> i-icon.source;

    callback clicked <=> i-touch-area.clicked;

    private property <brush> background: CupertinoPalette.accent-background;
    private property <brush> icon-color: CupertinoPalette.accent-foreground;

    min-width: 16px;
    horizontal-stretch: 0;

    states [
        disabled when !i-touch-area.enabled : {
            opacity: 0.5;
            icon-color: CupertinoPalette.foreground-secondary;
        }
        pressed when i-touch-area.pressed : {
            root.background: CupertinoPalette.secondary-accent-background;
        }
    ]

    Rectangle {
        y: (parent.height - self.height) / 2;
        width: 14px;
        height: self.width;

        animate background { duration: 150ms; }

        if (root.enabled) : Rectangle {
            width: 100%;
            height: 100%;
            border-radius: 4px;
            background: root.background;
            drop-shadow-blur: 3px;
            drop-shadow-color: #00000066;
            drop-shadow-offset-y: 0.5px;

            Rectangle {
                drop-shadow-blur: 2px;
                drop-shadow-color: #00000026;
                drop-shadow-offset-y: 1px;
                border-radius: parent.border-radius;
                background: root.background;
            }

            Rectangle {
                drop-shadow-blur: 1px;
                drop-shadow-color: #00000026;
                drop-shadow-offset-y: 0.5px;
                border-radius: parent.border-radius;
                background: root.background;
            }

            Rectangle {
                border-radius: parent.border-radius;
                background: CupertinoPalette.dimmer;
                opacity: 0.17;
            }

        }

        i-icon := Image {
            image-fit: contain;
            colorize: root.icon-color;
            width: 12px;

            animate colorize { duration: 150ms; }
        }
    }

    i-touch-area := TouchArea {}
}

export component SpinBox {
    in property <int> minimum <=> i-base.minimum;
    in property <int> maximum <=> i-base.maximum;
    in property <bool> enabled <=> i-base.enabled;
    out property <bool> has-focus <=> i-base.has-focus;
    in-out property <int> value <=> i-base.value;

    callback edited <=> i-base.edited;

    private property <brush> background: CupertinoPalette.control-background;

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

    accessible-role: spinbox;
    accessible-value: root.value;
    accessible-value-minimum: root.minimum;
    accessible-value-maximum: root.maximum;
    accessible-value-step: (root.maximum - root.minimum) / 100;
    accessible-action-set-value(v) => { if v.is-float() { i-base.update-value(v.to-float()); } }
    accessible-action-increment => { i-base.increment(); }
    accessible-action-decrement => { i-base.decrement(); }

    states [
        disabled when !root.enabled : {
            i-base.color: CupertinoPalette.foreground-secondary;
            root.background: CupertinoPalette.tertiary-control-background;
        }
    ]

    FocusBorder {
        x: (parent.width - self.width) / 2;
        y: (parent.height - self.height) / 2;
        width: parent.width + 6px;
        height: parent.height + 6px;
        border-radius: 8px;
        has-focus: root.has-focus;
    }

    Rectangle {
        drop-shadow-blur: 0.25px;
        drop-shadow-color: #00000066;
        drop-shadow-offset-y: 0.25px;
        border-radius: 5px;
        background: root.background;

        Rectangle {
            drop-shadow-blur: 1px;
            drop-shadow-color: #00000026;
            drop-shadow-offset-y: 1px;
            border-radius: parent.border-radius;
            background: root.background;
            border-width: 1px;
            border-color: CupertinoPalette.decent-border;
            opacity: root.enabled ? 1 : 0.5;
        }
    }

    i-layout := HorizontalLayout {
        padding-left: 7px;
        padding-right: 2px;
        spacing: 2px;

        Rectangle {
            clip: true;
            horizontal-stretch: 1;

            i-base := SpinBoxBase {
                width: 100%;
                color: CupertinoPalette.foreground;
                font-size: CupertinoFontSettings.body.font-size;
                font-weight: CupertinoFontSettings.body.font-weight;
                selection-background-color: CupertinoPalette.selection-background;
                selection-foreground-color: self.color;
            }
        }

        SpinBoxButton {
            visible: root.enabled;
            icon: Icons.chevron-up;
            enabled: root.enabled;

            clicked => {
                i-base.increment();
            }
        }

        SpinBoxButton {
            visible: root.enabled;
            icon: Icons.chevron-down;
            enabled: root.enabled;

            clicked => {
                i-base.decrement();
            }
        }
    }
}
