import QtQuick 2.14
import QtQuick.Controls 2.14
import QtQuick.Scene3D 2.14
import Qt3D.Core 2.14
import Qt3D.Render 2.14
import Qt3D.Input 2.14
import Qt3D.Extras 2.14

import QtQuick 2.14 as QQ2

Entity {
    enabled: visible
    property bool visible: false
    property real step: 30
    property int far: step*10
    property int vertexCount: ( 2*far / step + 1) * 4
    property int indexCount: vertexCount
    property vector3d colorBoundg: Qt.vector3d( 0.7, 0.7, 0.7 )
    property var gbrightness: 0.0
    property var gtransparency: 0.5

    NodeInstantiator {
        active: parent.visible
        model: far/step
        delegate: Entity {
            id: axisText
            property real pointSize: step/5
            property real range: step * (index+1)
            Entity {
                components: [ Transform{
                    translation: Qt.vector3d(0, axisText.range, 0)
                    rotationZ: 0
                    rotationY: camera.position.z<0?180:0
                }]
                Text2DEntity {
                    color: Qt.rgba(colorBoundg.x,colorBoundg.y,colorBoundg.z,gtransparency)
                    font.bold: true
                    font.pointSize: axisText.pointSize
                    text: axisText.range
                    width: text.length * font.pointSize
                    height: font.pointSize*2
                }
            }
            Entity {
                components: [ Transform{
                    translation: Qt.vector3d(-axisText.range, 0, 0)
                    rotationZ: 90
                    rotationX: camera.position.z<0?180:0
                }]
                Text2DEntity {
                    color: Qt.rgba(colorBoundg.x,colorBoundg.y,colorBoundg.z,gtransparency)
                    
                    font.bold: true
                    font.pointSize: axisText.pointSize
                    text: axisText.range
                    width: text.length * font.pointSize
                    height: font.pointSize*2
                }
            }
            Entity {
                components: [ Transform{
                    translation: Qt.vector3d(0, -axisText.range, 0)
                    rotationZ: 180
                    rotationY: camera.position.z<0?180:0
                }]
                Text2DEntity {
                    color: Qt.rgba(colorBoundg.x,colorBoundg.y,colorBoundg.z,gtransparency)
                    
                    font.bold: true
                    font.pointSize: axisText.pointSize
                    text: axisText.range
                    width: text.length * font.pointSize
                    height: font.pointSize*2
                }
            }
            Entity {
                components: [ Transform{
                    translation: Qt.vector3d(axisText.range, 0, 0)
                    rotationZ: 270
                    rotationX: camera.position.z<0?180:0
                }]
                Text2DEntity {
                    color: Qt.rgba(colorBoundg.x,colorBoundg.y,colorBoundg.z,gtransparency)
                    
                    font.bold: true
                    font.pointSize: axisText.pointSize
                    text: axisText.range
                    width: text.length * font.pointSize
                    height: font.pointSize*2
                }
            }
        }
    }
    LineMaterial {
        id: line_material
        colorBound:colorBoundg
        brightness:gbrightness
        transparency:gtransparency
    }
    PhongAlphaMaterial  {
        id: phong_alpha_material
        alpha: 1
        ambient: Qt.rgba(colorBoundg.x, colorBoundg.y, colorBoundg.z, gtransparency)
        diffuse: Qt.rgba(colorBoundg.x, colorBoundg.y, colorBoundg.z, gtransparency)
        specular: Qt.rgba(0, 0, 0, 0)
    }
    GeometryRenderer {
        id: cartesion_geometry
        primitiveType: GeometryRenderer.Lines
        geometry: Geometry {
            boundingVolumePositionAttribute: position
            attributes: [
                Attribute {
                    id: position
                    attributeType: Attribute.VertexAttribute
                    vertexBaseType: Attribute.Float
                    vertexSize: 3
                    count: vertexCount
                    byteOffset: 0
                    byteStride: 3 * 4 // 1 vertex (=3 coordinates) * sizeof(float)
                    name: defaultPositionAttributeName
                    buffer: vertexBuffer
                },
                Attribute {
                    attributeType: Attribute.IndexAttribute
                    vertexBaseType: Attribute.UnsignedInt
                    vertexSize: 1
                    count: vertexCount
                    byteOffset: 0
                    byteStride: 1 * 4 // 1 index * sizeof(Uint32)
                    buffer: indexBuffer
                }
            ]
        }
        Buffer {
            id: vertexBuffer
            type: Buffer.VertexBuffer
            data: {
                var vertexes = new Float32Array(vertexCount * 3)
                var index = 0
                for(var dis = -far; dis <= far; dis += step) {
                    vertexes[12 * index + 0 ] = dis
                    vertexes[12 * index + 1 ] = -far
                    vertexes[12 * index + 2 ] = 0
                    vertexes[12 * index + 3 ] = dis
                    vertexes[12 * index + 4 ] = far
                    vertexes[12 * index + 5 ] = 0
                    vertexes[12 * index + 6 ] = -far
                    vertexes[12 * index + 7 ] = dis
                    vertexes[12 * index + 8 ] = 0
                    vertexes[12 * index + 9 ] = far
                    vertexes[12 * index + 10] = dis
                    vertexes[12 * index + 11] = 0
                    index++
                }
                return vertexes
            }
        }
        Buffer {
            id: indexBuffer
            type: Buffer.IndexBuffer
            data: {
                var indexes = new Uint32Array(vertexCount)
                for(var index = 0; index < vertexCount; index++) {
                    indexes[index] = index
                }
                return indexes
            }
        }
    }

    ObjectPicker{
        id: cartesion_picker
        onPressed: {
            console.log("Cartesion Object Picked")
        }
    }
    components: [config.IsWinGpuAmdCard()?line_material:phong_alpha_material, cartesion_geometry, cartesion_picker]
}