// 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, CosmicFontSettings } from "styling.slint";
import { StateLayerBase } from "components.slint";
import { TabBarBase } from "../common/tabwidget-base.slint";

export component TabWidgetImpl inherits Rectangle {
    in property <length> tabbar-preferred-height;
    in property <length> tabbar-preferred-width;
    in property <length> content-min-height;
    in property <length> content-min-width;
    in property <int> current-index;
    in property <int> current-focused;
    out property <length> content-x: 0;
    out property <length> content-y: root.tabbar-preferred-height;
    out property <length> content-height: root.height - root.tabbar-preferred-height;
    out property <length> content-width: root.width;
    out property <length> tabbar-x: 0;
    out property <length> tabbar-y: 0;
    out property <length> tabbar-height: root.tabbar-preferred-height;
    out property <length> tabbar-width: root.width;

    preferred-width: root.content-min-width;
    min-width: max(root.content-min-width, root.tabbar-preferred-width);
    preferred-height: root.content-min-height + root.tabbar-preferred-height;
    min-height: root.content-min-height + root.tabbar-preferred-height;
}

export component TabImpl inherits Rectangle {
    // The currently focused tab
    in property <int> current-focused;
    // The index of this tab
    in property <int> tab-index;
    // The total number of tabs
    in property <int> num-tabs;
    in property <string> title <=> text.text;
    in property <bool> enabled: true;
    out property <bool> has-focus: root.current-focused == root.tab-index;
    // The currently selected tab
    in-out property <int> current;

    private property <bool> show-left-border: root.tab-index == 0 || root.is-current;
    private property <bool> show-right-border: root.current != root.tab-index + 1;
    private property <bool> is-current: root.tab-index == root.current;
    private property <length> inner-border-radius: 8px;

    min-width: max(160px, text.min-width);
    min-height: max(44px, text.min-height);
    horizontal-stretch: 0;
    vertical-stretch: 1;
    accessible-role: tab;
    accessible-label: root.title;

    Rectangle {
        y: 0;
        height: root.height - root.inner-border-radius;
        clip: true;

        state-layer := StateLayerBase {
            y: 0;
            width: 100%;
            height: root.height;
            has-hover: touch-area.has-hover;
            pressed: touch-area.pressed;
            checked: root.is-current;
            border-radius: root.inner-border-radius;
        }
    }

    Rectangle {
        y: root.height - self.height;
        height: root.inner-border-radius;
        background: state-layer.background;
    }

    if root.has-focus: Rectangle {
        Rectangle {
            y: 0;
            height: root.height - root.inner-border-radius;
            clip: true;

            Rectangle {
                y: 0;
                height: root.height;
                border-width: 1px;
                border-radius: root.inner-border-radius;
                border-color: CosmicPalette.accent-background;
            }
        }

        Rectangle {
            x: 0;
            y: root.height - self.height;
            width: 1px;
            height: root.inner-border-radius;
            background: CosmicPalette.accent-background;
        }

        Rectangle {
            x: root.width - self.width;
            y: root.height - self.height;
            width: 1px;
            height: root.inner-border-radius;
            background: CosmicPalette.accent-background;
        }
    }

    Rectangle {
        y: parent.height - self.height;
        background: CosmicPalette.accent-background;
        height: root.is-current ? 4px : 1px;
    }

    touch-area := TouchArea {
        enabled <=> root.enabled;

        clicked => {
            root.current = root.tab-index;
        }
    }

    layout := HorizontalLayout {
        text := Text {
            vertical-alignment: center;
            horizontal-alignment: center;
            font-size: CosmicFontSettings.body.font-size;
            font-weight: root.is-current ? CosmicFontSettings.body-strong.font-weight : CosmicFontSettings.body.font-weight;
            color: root.is-current ? CosmicPalette.accent-background : CosmicPalette.control-foreground;
        }
    }
}

export component TabBarImpl inherits TabBarBase {
    // injected properties:
    // The currently focused tab
    in-out property <int> current-focused: focus-scope.has-focus ? focus-scope.focused-tab : -1;

    accessible-role: tab-list;
    accessible-delegate-focus: root.current-focused >= 0 ? root.current-focused : root.current;
    preferred-height: 44px;

    HorizontalLayout {
        @children
    }

    if focus-scope.has-focus: Rectangle {
        y: root.height - self.height;
        height: 1px;
        background: CosmicPalette.accent-background;
    }

    focus-scope := FocusScope {
        property <int> focused-tab: 0;

        x: 0;
        // Do not react on clicks
        width: 0px;

        key-pressed(event) => {
            if (event.text == "\n") {
                root.current = root.current-focused;
                return accept;
            }
            if (event.text == Key.LeftArrow) {
                 self.focused-tab = Math.max(self.focused-tab - 1,  0);
                 return accept;
            }
            if (event.text == Key.RightArrow) {
                 self.focused-tab = Math.min(self.focused-tab + 1, root.num-tabs - 1);
                 return accept;
            }
            return reject;
        }

        key-released(event) => {
            if (event.text == " ") {
                root.current = root.current-focused;
                return accept;
            }
            return reject;
        }
    }
}

export component TabWidget inherits TabWidget { }
