// 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 { CosmicPalette } from "styling.slint";
import { StateLayerBase } from "components.slint";
import { SliderBase } from "../common/slider-base.slint";

export component Slider {
    in property <Orientation> orientation <=> base.orientation;
    in property <float> maximum <=> base.maximum;
    in property <float> minimum <=> base.minimum;
    in property <bool> enabled <=> base.enabled;
    out property <bool> has-focus: base.has-focus;
    in-out property <float> value <=> base.value;

    callback changed <=> base.changed;
    callback released <=> base.released;

    min-width: base.vertical ? 20px : 0px;
    min-height: base.vertical ? 0px : 20px;
    vertical-stretch: base.vertical ? 1 : 0;
    horizontal-stretch: base.vertical ? 0 : 1;
    accessible-role: slider;
    accessible-value: root.value;
    accessible-value-minimum: root.minimum;
    accessible-value-maximum: root.maximum;
    accessible-value-step: (root.maximum - root.minimum) / 100;
    forward-focus: base;

    states [
        disabled when !root.enabled : {
            opacity: 0.5;
        }
    ]

    rail := Rectangle {
        width: base.vertical ? 4px : parent.width;
        height: base.vertical ? parent.height : 4px;
        background: CosmicPalette.neutral-6-background;
        border-radius: 2px;
    }

    track := Rectangle {
        x: base.vertical ? (parent.width - self.width) / 2 : 0;
        y: base.vertical ? 0 : (parent.height - self.height) / 2;
        width: base.vertical ? rail.width : thumb.x + (thumb.width / 2);
        height: base.vertical ? thumb.y + (thumb.height / 2) : rail.height;
        background: CosmicPalette.secondary-accent-background;
        border-radius: rail.border-radius;
    }

    thumb := Rectangle {
        x: base.vertical ? (parent.width - self.width) / 2 : (parent.width - self.width) * (root.value - root.minimum) / (root.maximum - root.minimum);
        y: base.vertical ? (parent.height - self.height) * (root.value - root.minimum) / (root.maximum - root.minimum) : (parent.height - self.height) / 2;
        width: 20px;
        height: self.width;
        border-radius: 10px;
        background: CosmicPalette.accent-background;

        StateLayerBase {
            width: parent.width + 8px;
            height: parent.height + 8px;
            border-radius: max(self.width, self.height) / 2;
            enabled: root.enabled;
            focus-boder-margin: 0px;
            pressed: base.handle-pressed;
            has-hover: base.has-hover;
            has-focus: root.has-focus;
        }
    }

    base := SliderBase {
        width: 100%;
        height: 100%;
        handle-x: thumb.x;
        handle-y: thumb.y;
        handle-width: thumb.width;
        handle-height: thumb.height;
    }
}
