import QtQuick
import QtQuick.Window
import QtQuick3D

Window {
    visible: true
    visibility: Window.FullScreen
    width: 1280
    height: 720

    color: "black"

    View3D {
        anchors.fill: parent

        environment: SceneEnvironment {
            clearColor: "#202020"
            backgroundMode: SceneEnvironment.Color
        }

        PerspectiveCamera {
            id: camera
            position: Qt.vector3d(0, 100, 300)
            eulerRotation.x: -20
        }

        DirectionalLight {
            eulerRotation.x: -45
            brightness: 2
        }

        Node {
            id: rootNode

            Repeater3D {
                model: gcodeData

                delegate: Model {
                    property real dx: modelData.x2 - modelData.x1
                    property real dy: modelData.y2 - modelData.y1
                    property real dz: modelData.z2 - modelData.z1

                    property real length: Math.sqrt(dx*dx + dy*dy + dz*dz)

                    source: "#Cylinder"

                    position: Qt.vector3d(
                        (modelData.x1 + modelData.x2)/2,
                        (modelData.z1 + modelData.z2)/2,
                        (modelData.y1 + modelData.y2)/2
                    )

                    scale: Qt.vector3d(0.2, length / 100.0, 0.2)

                    materials: [
                        DefaultMaterial {
                            diffuseColor: "#00ff88"
                        }
                    ]
                }
            }
        }

        MouseArea {
            anchors.fill: parent

            property real lastX
            property real lastY

            onPressed: {
                lastX = mouse.x
                lastY = mouse.y
            }

            onPositionChanged: {
                let dx = mouse.x - lastX
                let dy = mouse.y - lastY

                rootNode.eulerRotation.y += dx * 0.3
                rootNode.eulerRotation.x += dy * 0.3

                lastX = mouse.x
                lastY = mouse.y
            }

            onWheel: {
                camera.position.z += wheel.angleDelta.y * -0.1
            }
        }
    }
}