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

Window {
	visible: true
	id: mainWindow

    width: 250
    height: 250  // Increased height to accommodate the circle and buttons
	
	property string armType: ""
    property string armAction: ""
    property string armParameters: ""	
	property string armScript: ""
	
	// Instruction text property
    property string instructionText: 
		"<h3>Audio Trigger Interface</h3><p>  
		<p>This interface is used to arm a Record action. 
		<p>Select the track and the audio threshold. 
		<p>Then click on the Arm record button"
	
	// Signal to LoopToGo
	signal ltgExecute(ltgType: string, ltgAction: string, ltgParameters: string)
	
	// Variables from LoopToGo that will be used in this qml file
	
	Component.onCompleted: {
		// Register the "nbTracks" property (even if already initialized by C++)
		loopToGo.registerProperty("nbTracks", "int");
		loopToGo.registerProperty("isArmedForRecording", "bool");
		loopToGo.registerProperty("g_armScriptMessage", "string");
		
		// Loop to register track name properties
		for (var i = 1; i <= loopToGo.properties["nbTracks"]; ++i) {
			var fullName = "trackName" + i;
			loopToGo.registerProperty(fullName, "string");
		}
	}

    Rectangle {
		id: mainRectangle
        width: parent.width
        height: parent.height
        color: "#353535"  // Dark background color
        anchors.centerIn: parent
		
		// Title
		Text {
			id: titleText
			
			text: loopToGo.properties["g_armScriptMessage"] ?? "Audio Trigger"
			color: "#eeeeee"			
			horizontalAlignment: Text.AlignHCenter  // Ensures text alignment inside the box
			anchors.top: parent.top	
			anchors.horizontalCenter: parent.horizontalCenter  // Centers the text horizontally				
			anchors.margins: mainRectangle.height*.08				
			font.pixelSize: mainRectangle.height*.07
			
            font.bold: true			
			
		}
		
				
		// Small Button with Image (Bottom-left corner)
		LtgLogoButton {
			width: mainRectangle.width * 0.07
			height: width
		}        
		
        // Help Button (Top-right corner)
        LtgHelpButton {
			
			width: mainRectangle.width * 0.07

			
            // Show instructions when clicked
			onClicked: {
				messageWindow.show();
			}
        }

		// Message dialog to display instructions		
		LtgMessageWindow {
			id: messageWindow
			title: "Instructions"
			
		}	

		Slider {
			id: trackSlider
			width: mainRectangle.width * 0.8
			anchors.horizontalCenter: parent.horizontalCenter
			anchors.top: parent.top
			anchors.topMargin: mainRectangle.height * 0.35

			from: 0  // Set the starting value
			to: .5  // Set the ending value
			value: .1  // Initial value
			stepSize: .01

			//onValueChanged: {
			//	console.log("Slider value:", value);
			//}
		}
		
		ComboBox {
			id: trackSelector
			width: mainRectangle.width*.8
			
			anchors.horizontalCenter: parent.horizontalCenter
			anchors.top: parent.top
			anchors.topMargin: mainRectangle.height*.2  // adjust as needed

			
			
			currentIndex: 0 // default value
			
			model: ListModel {
				Component.onCompleted: {
					// Append hardcoded track names directly when the ComboBox is created
					for (var i = 1; i <= loopToGo.properties["nbTracks"]; ++i) {
						var trackValue = loopToGo.properties["trackName" + i];

						// Append the track names from loopToGo.properties
						trackSelector.model.append({ name: trackValue });
						
					}

				}
			}

			
				
			textRole: "name"
					
			font.pixelSize: 14
			 
			
		}

		// Button 1 (placed in row 1, column 0)
		LtgButton {
			text: "Arm\nRecord"

			ltgType: "loadScript"
			ltgAction: armScript
			ltgParameters: (trackSelector.currentIndex + 1) + " " + trackSlider.value + " " + armType + " \"" + armAction + "\" \"" + armParameters + "\""
			
			width: mainRectangle.width*.4
			height: mainRectangle.height*.4
			
			anchors.horizontalCenter: parent.horizontalCenter
			anchors.bottom: parent.bottom
			anchors.bottomMargin: mainRectangle.height*.07  // adjust as needed
			

			font.pixelSize: mainRectangle.width*.06
			background: Rectangle {
				color: parent.pressed
						? Qt.darker("red", 1.5)
						: (loopToGo.properties["isArmedForRecording"] === undefined
							? "green"
							: (loopToGo.properties["isArmedForRecording"] ? "red" : "green"))				
				radius: 10
			}
			font.bold: true
						
		}

            
    }
}

