import QtQuick
import QtQuick.Layouts
import Quickshell

import "../../config" as Cfg

Rectangle {
    id: root

    property real panelRadius: Cfg.Config.radius / 2
    property int  borderWidth: Cfg.Config.panelBorderWidth

    property bool isTop:        true
    property var  targetScreen: null
    property int  barHeight:    Cfg.Config.barHeight
    property int  barMargin:    Cfg.Config.margin

    readonly property var t: Cfg.Config.theme

    property var launcher: null

    readonly property bool launcherActive: !!launcher && launcher.isOpen
    readonly property bool isHovered:      hoverHandler.hovered

    property real spinTarget: 0

    onTargetScreenChanged: Qt.callLater(() => {
        launcher = (targetScreen && targetScreen.launcher) ? targetScreen.launcher : null
    })

    onLauncherActiveChanged: {
        spinTarget += launcherActive ? 360 : -360
        if (launcherActive && !clickAnim.running)
            clickAnim.restart()
    }

    width:  Cfg.Config.logoIconSize * 2 + 4
    height: parent.height

    color:  isHovered ? t.archLogoBgHover : t.archLogoBg
    radius: panelRadius

    border.color: launcherActive ? t.archLogoBorderActive : t.archLogoBorder
    border.width: borderWidth

    Behavior on color        { ColorAnimation { duration: 250 } }
    Behavior on border.color { ColorAnimation { duration: 250 } }

    HoverHandler { id: hoverHandler }

    TapHandler {
        onTapped: {
            clickAnim.restart()
            if (root.targetScreen && root.targetScreen.launcher) {
                root.targetScreen.launcher.toggle()
            }
        }
    }

    SequentialAnimation {
        id: clickAnim
        NumberAnimation { target: logoText; property: "scale"; from: 1.0; to: 0.7; duration: 60;  easing.type: Easing.OutQuad }
        NumberAnimation { target: logoText; property: "scale"; from: 0.7; to: 1.2; duration: 150; easing.type: Easing.OutBack }
        NumberAnimation { target: logoText; property: "scale"; to: 1.0;           duration: 100 }
    }

    Text {
        id: logoText
        anchors.centerIn: parent
        text:           Cfg.Config.logoIcon

        color:          launcherActive ? t.archLogoIconActive : t.archLogoIcon
        font.pixelSize: Cfg.Config.logoIconSize

        Behavior on color { ColorAnimation { duration: 250 } }

        scale: clickAnim.running ? logoText.scale : 1.0
        Behavior on scale {
            enabled: !clickAnim.running
            NumberAnimation { duration: 250; easing.type: Easing.OutBack }
        }

        rotation: root.spinTarget
        Behavior on rotation {
            NumberAnimation {
                duration: 600
                easing.type: Easing.InOutBack
            }
        }
    }
}
