import QtQuick 
import QtQuick.Controls 

import "../LtgWidgets"



Item {
    width: 200
    height: 350
	

    // Instruction text property
    property string instructionText: 
		"<h3>How to use this 1 button looper emulator</h3><p>  
		<p>Create at least 1 track with input and output(s) 
		<p>Press the Loop button to start recording. 
		<p>Press again to stop recording and loop.
		<br>
		<p>Hold the Loop button to reset the song (only in the interface).
		<br>
		<h4>To use this looper with a MIDI pedal or MIDI controller</h4>
		<p>Right-click on the Loop button to assign the button to a MIDI event (MIDI learn...).
		<br>
		<p><b>Note:</b> It is possible to use this looper with many tracks. Use the mouse to select a new track"

    // Signal to LoopToGo
    signal ltgExecute(ltgType: string, ltgAction: string, ltgParameters: string)
	
	// Variables from LoopToGo that will be used in this qml file
	Component.onCompleted: {
        loopToGo.registerProperty("lastCreatedRecordLoopColor", "string");		
		
		Qt.callLater(() => {
					loopToGo.ltgExecute("scriptCommand", "setLooperMode", "1 1");  // 1 for FreeNoSync, 1 for add to stack
					});
	}
	
	Component.onDestruction: {
		loopToGo.ltgExecute("scriptCommand", "resetLooperMode", "");		
	}
	
    // Rectangle as the background
    Rectangle {
		id: mainRectangle
        width: parent.width
        height: parent.height
        color: "#353535"  // Dark background color
		
		// Title
		Text {
			id: titleText
			
			text: "Simple looper\n1b"			
			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*.17				
			font.pixelSize: mainRectangle.width * 0.09
            font.bold: true			
			
		}
		
		// Small Button with Image (Bottom-left corner)
		LtgLogoButton {
			width: mainRectangle.width * 0.12
			height: width			
		}

        // Help Button (Top-right corner)
        LtgHelpButton {
			
			width: mainRectangle.width * 0.12
			height: width
			
            // Show instructions when clicked
			onClicked: {
				messageWindow.show();
			}
        }

		// Message dialog to display instructions		
		LtgMessageWindow {
			id: messageWindow
			title: "Instructions"
			
		}
		
		// Rectang;le around loop button to indiqcate looper state
		Rectangle{
			width: loopButton.width*1.1
			height: loopButton.height*1.1
			
			anchors.horizontalCenter: loopButton.horizontalCenter
            anchors.verticalCenter: loopButton.verticalCenter
			
            color: (loopToGo.properties && loopToGo.properties["lastCreatedRecordLoopColor"] !== undefined) ?
				(loopToGo.properties["lastCreatedRecordLoopColor"] === "0" ? "#000000" : loopToGo.properties["lastCreatedRecordLoopColor"]) :
				"#000000"  // Default color before the property is available
            radius: 15			           
		}		
		 
        // Loop Button (Centered)
        LtgButton {
            text: "Loop"
            id: loopButton

            ltgType: "loadScript"
            ltgAction: "LoopToGo/LooperEmulations/dtOne.ltgs"
            ltgParameters: "-1" // -1 : selected track
			ltgArmEnabled: true // This buttons supports audio arming action
			ltgArmScript: "LoopToGo/audioTrigger.ltgs"
			
			property bool holding: false

			
            anchors.horizontalCenter: parent.horizontalCenter
			anchors.verticalCenter: parent.verticalCenter
			anchors.verticalCenterOffset: mainRectangle.height*.07  // Moves it downward


            width: parent.width * 0.5
            height: parent.height * 0.3

            font.pixelSize: 18

			background: Rectangle {
				color: parent.pressed ? "#388E3C" : "#4CAF50"
				radius: 10
			}
			
			
            font.bold: true
			
			Timer {
				id: holdTimer
				interval: 1500  // 1.5 seconds
				running: false
				repeat: false
				onTriggered: {
					//console.log("Button held for 2 seconds!")
					parent.holding = false
					ltgExecute("loadScript", "LoopToGo/LooperEmulations/resetSong.ltgs", "");
				}
			}
			
			MouseArea {
				anchors.fill: parent
				onPressed: {
					holdTimer.start()
					parent.holding = true
				}
				
				onReleased: {
					holdTimer.stop()  // Stop the timer if released before 2 seconds
					if(parent.holding != false){					
						if (parent.ltgType != "") {
							ltgExecute(parent.ltgType, parent.ltgAction, parent.ltgParameters);
						}
					}
					parent.holding = false
				}
			}
        }
		
		
    }
}
