import { Theme } from "../theme.slint";

export component NeonToggle {
    in property <string> label: "";
    in-out property <bool> checked: false;
    in property <bool> enabled: true;
    in property <bool> has-focus: false;
    callback toggled;

    HorizontalLayout {
        spacing: Theme.sp-2;
        alignment: start;
        if (root.label != "") : Text {
            text: root.label;
            color: root.enabled ? Theme.text-primary : Theme.text-dim;
            font-size: Theme.font-base;
            vertical-alignment: center;
        }
        Rectangle {
            width: 32px; height: 18px;
            border-radius: 9px;
            background: !root.enabled ? Theme.border :
                        root.checked ? Theme.neon-cyan : Theme.border;
            border-width: root.has-focus ? 1px : 0px;
            border-color: root.has-focus ? Theme.border-focus : transparent;
            opacity: root.enabled ? 1.0 : 0.4;
            Rectangle {
                width: 14px; height: 14px;
                border-radius: 7px;
                background: !root.enabled ? Theme.text-dim : Theme.text-inverse;
                x: root.checked ? 16px : 2px;
                y: 2px;
            }
            touch := TouchArea {
                clicked => {
                    if (root.enabled) {
                        root.checked = !root.checked;
                        root.toggled();
                    }
                }
                enabled: root.enabled;
                mouse-cursor: root.enabled ? MouseCursor.pointer : MouseCursor.default;
            }
        }
    }
}

export component NeonCheckbox {
    in property <string> label: "";
    in-out property <bool> checked: false;
    in property <bool> enabled: true;
    in property <bool> has-focus: false;
    callback toggled;

    HorizontalLayout {
        spacing: Theme.sp-2;
        alignment: start;
        Rectangle {
            width: 16px; height: 16px;
            border-radius: Theme.radius-sm;
            border-width: 1px;
            border-color: root.has-focus ? Theme.border-focus :
                          root.checked ? Theme.neon-cyan : Theme.border;
            background: root.checked ? Theme.neon-cyan : transparent;
            opacity: root.enabled ? 1.0 : 0.4;
            Text {
                text: "✓";
                color: Theme.text-inverse;
                font-size: 11px;
                font-weight: 700;
                horizontal-alignment: center;
                vertical-alignment: center;
                visible: root.checked;
            }
            touch := TouchArea {
                clicked => {
                    if (root.enabled) {
                        root.checked = !root.checked;
                        root.toggled();
                    }
                }
                enabled: root.enabled;
                mouse-cursor: root.enabled ? MouseCursor.pointer : MouseCursor.default;
            }
        }
        if (root.label != "") : Text {
            text: root.label;
            color: root.enabled ? Theme.text-primary : Theme.text-dim;
            font-size: Theme.font-base;
            vertical-alignment: center;
        }
    }
}

export component NeonRadioGroup {
    in property <[string]> items;
    in-out property <int> selected-index: -1;
    in property <bool> enabled: true;
    callback index-changed(int);

    VerticalLayout {
        spacing: Theme.sp-2;
        for item[idx] in root.items : HorizontalLayout {
            spacing: Theme.sp-2;
            alignment: start;
            min-height: 28px;
            Rectangle {
                width: 16px; height: 16px;
                border-radius: 8px;
                border-width: 1px;
                border-color: idx == root.selected-index ? Theme.neon-cyan : Theme.border;
                opacity: root.enabled ? 1.0 : 0.4;
                if (idx == root.selected-index) : Rectangle {
                    width: 8px; height: 8px;
                    border-radius: 4px;
                    background: root.enabled ? Theme.neon-cyan : Theme.text-dim;
                    x: 4px; y: 4px;
                }
                touch := TouchArea {
                    clicked => {
                        if (root.enabled) {
                            root.selected-index = idx;
                            root.index-changed(idx);
                        }
                    }
                    enabled: root.enabled;
                    mouse-cursor: root.enabled ? MouseCursor.pointer : MouseCursor.default;
                }
            }
            Text {
                text: item;
                color: idx == root.selected-index ? (root.enabled ? Theme.neon-cyan : Theme.text-dim) : (root.enabled ? Theme.text-primary : Theme.text-dim);
                font-size: Theme.font-base;
                font-weight: idx == root.selected-index ? 600 : 400;
                vertical-alignment: center;
            }
        }
    }
}
