// showMessage.qml

import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Controls.Fusion 6.5

Item {
    id: root
	
	// Define constants for padding and minimum width
    property int paddingWidth: 80
    property int minWidth: 200
	
    width: Math.max(label.paintedWidth + paddingWidth, minWidth)
    height: 200  

    Component.onCompleted: {
        loopToGo.registerProperty("g_myMessageText", "string")
		
    }

    // Background
    Rectangle {
        anchors.fill: parent
        color: "#232323"
        border.color: "#888"
    }

    // Text label
    Text {
        id: label
        text: {
            if (!loopToGo.properties || loopToGo.properties["g_myMessageText"] === undefined)
                return "Hello world!"
            return loopToGo.properties["g_myMessageText"] === "0"
                ? "Hello world!"
                : (loopToGo.properties["g_myMessageText"] || "Hello world!")
        }
        font.pointSize: 18
        color: "white"
        width: implicitWidth
        height: implicitHeight

        anchors.horizontalCenter: parent.horizontalCenter
        anchors.verticalCenterOffset: -20
        anchors.verticalCenter: parent.verticalCenter

		onPaintedWidthChanged: {
			if (quickView != undefined) {			
				// Compute current center
				var centerX = quickView.x + quickView.width / 2
				
				// Compute new width (height stays the same)
				var newWidth = Math.max(label.paintedWidth + paddingWidth, minWidth)

				// Resize window
				quickView.setWidth(newWidth)

				// Move window so center stays the same
				if(quickView.x != 0 && quickView.y != 0) // Make sure the window is set to a valid position before changing it!
				{
					quickView.x = centerX - quickView.width / 2					
				}
			}
		}

    }

    // Close button
    Button {
        id: closeButton
        anchors.horizontalCenter: parent.horizontalCenter
        anchors.bottom: parent.bottom
        anchors.bottomMargin: 20
        width: 120
        height: 40

        // Button background
        background: Rectangle {
            anchors.fill: parent
            color: "#AAAAAA"
            radius: 5
        }

        // Button text
        contentItem: Text {
            text: qsTr("Close")
            color: "black"
            font.pointSize: 14
            font.bold: true
            anchors.fill: parent
            horizontalAlignment: Text.AlignHCenter
            verticalAlignment: Text.AlignVCenter
        }

        onClicked: {
            if (typeof quickView !== "undefined") quickView.close()
            else if (typeof quickWidget !== "undefined") quickWidget.close()
        }
    }

    // Signal to notify C++ (optional)
    signal closeRequested()    
}
