import QtQuick

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

ModuleChip {
    id: root

    property var launcher: null

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

    property real spinTarget: 0

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

    onLauncherActiveChanged: {
        spinTarget += launcherActive ? 360 : -360
    }

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

    chipColor:        t.logoBg
    chipColorHovered: t.logoBgHover
    chipBorderColor:  t.logoBorder
    chipBorderColorHovered: t.logoBorderActive
    popupOpen:        launcherActive
    chipAnimDuration: 250

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

    Item {
        id: logoWrapper
        anchors.centerIn: parent
        width:  Cfg.Config.logoIconSize
        height: Cfg.Config.logoIconSize

        transformOrigin: Item.Center
        rotation: root.spinTarget
        Behavior on rotation {
            NumberAnimation {
                duration: Cfg.Config.logoSpinDuration
                easing.type: Easing.InOutBack
            }
        }

        Text {
            id: logoText
            anchors.centerIn: parent
            text:           Cfg.Config.logoIcon
            color:          launcherActive ? t.logoIconActive : t.logoIcon
            font.pixelSize: Cfg.Config.logoIconSize
            renderType:     Text.QtRendering
            Behavior on color { ColorAnimation { duration: 250 } }
        }
    }
}
