// LtgButton.qml
import QtQuick 6.5
import QtQuick.Controls.Fusion 6.5 // So we can overload the native style!

// Inherit directly from Button
Button {
	id: ltgButton
	
	property string ltgItemType: "button" // For tutorials
	
    property string ltgType: ""
    property string ltgAction: ""
    property string ltgParameters: ""
    property bool ltgArmEnabled: false
	property string ltgArmScript: "LoopToGo/audioTrigger.ltgs"
	property string ltgShowArmDialog: ""
	
	palette.buttonText: "white"

	
	flat: true // To disable the change of button color when hovering the mouse over the button
		
	property string baseToolTip: "" // This property can be used by the derived Item to add info in the tool tip
	// Expose a property to control the tooltip visibility
    property bool showToolTip: true  // Default is to show the tooltip
	
	Component.onCompleted: {
		if(typeof quickView !== "undefined" && ltgShowArmDialog != "") {
			Qt.callLater(() => {
				armButtonClicked(ltgShowArmDialog)
			})
		}			
	}
	
	// Customize button's content item (this will replace the default background)
    background: Rectangle {
		radius: width*.03		
		color: parent.pressed ? "dark green" : "green"
	}
		
	// Right click handler (context menu)
    MouseArea {
		id: ltgMouseArea
        anchors.fill: parent
        acceptedButtons: Qt.RightButton
        onClicked: (mouse) => {
            if (parent.ltgType != "") {
                ltgMenuHandler.showContextMenu(Qt.point(mouse.x, mouse.y), parent.ltgType, parent.ltgAction, parent.ltgParameters);
            }
        }
    }

	// Left click handler
    onClicked: {
        if (ltgType != "") {
            loopToGo.ltgExecute(ltgType, ltgAction, ltgParameters);
			//console.log(ltgType + " " + ltgAction + " " + ltgParameters)
        }
    }
	
	ToolTip {
		visible: showToolTip ? hovered : false
		delay: 2000
		timeout: 3000
		width: toolTipText.width + 20
        height: toolTipText.height + 16
		
		y: toolTipText.height * -1.3
		
		Text{
			id: toolTipText
			text: {
			
				if (ltgType != "") {
					if(baseToolTip != "") {
						return baseToolTip + "\n\nType: " + ltgType + "\nAction: " + ltgAction+ "\nParameters: " + ltgParameters;
					}
					return "Type: " + ltgType + "\nAction: " + ltgAction+ "\nParameters: " + ltgParameters;
				}
				return baseToolTip;
			}
			
			// Set the font and text color
			//font.bold: true  // Optional: Makes the font bold
			font.pointSize: 10  // Adjust the font size as needed
			color: "green"
		}
		
		
	}
	
	
	    // Arm indicator button (visible when ltgArmEnabled is true)
    Rectangle {
        visible: ltgArmEnabled
        width: parent.width * 0.27
        height: parent.height * 0.20
        radius: 5
        anchors.bottom: parent.bottom
        anchors.right: parent.right
        anchors.margins: parent.width * 0.03
        color: ltgButton.palette.buttonText
        border.color: "transparent"
        z: 1

        Text {
            anchors.centerIn: parent
            text: "Arm"
            font.pointSize: Math.max(8, parent.height * 0.25 * 0.5)
            color:  ltgButton.background.color
        }

        MouseArea {
            anchors.fill: parent
            onClicked: {
                //console.log("Arm button clicked")				
				
				armButtonClicked("left")
            }
        }
				
    }
	
	function findRootItem(item) {
		let current = item;
		while (current.parent) {
			current = current.parent;
		}	
		return current;
	}
	
	function armButtonClicked(position) {
		
		let type = ltgType;
		let action = ltgAction;
		let parameters = ltgParameters;
		let script = ltgArmScript;

		Qt.callLater(() => {
			
			const rootItem = findRootItem(ltgButton);
			const buttonGlobalPos = rootItem.mapToGlobal(Qt.point(0, 0));
			
			if (buttonGlobalPos.x === 0 && buttonGlobalPos.y === 0) {
				// If window not ready, try again later
				Qt.callLater(armButtonClicked.bind(this, position));
				return;
			}
			
			const component = Qt.createComponent("ArmDialog.qml");
			if (component.status === Component.Ready) {
				const dialog = component.createObject(ltgButton, {
					armType: type,
					armAction: action,
					armParameters: parameters,
					armScript: script
				});

				if (dialog) {
					// Default gap between button and dialog
					const gap = 10;
					
					if (position === "left") {
						dialog.x = buttonGlobalPos.x - dialog.width - gap;
					} else {
						// Default is right
						dialog.x = buttonGlobalPos.x + ltgButton.parent.width + gap;
					}
					dialog.y = buttonGlobalPos.y;
				} else {
					console.error("Failed to create dialog object");
				}
			} else {
				console.error("Component loading error:", component.errorString());
			}
		})
	}
	
	
}
