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 int segs: 128
    property real step: 10
    property real step2: 30
    property real near: step
    property real far: step * 11
    property real near2: step * 12
    property real far2: near2 + step2*10

    property int circleCount: (far - near) / step + 1
    property int circleCount2: (far2 - near2) / step2 + 1
    property int vertexCount: ( ((far - near) / step + 1) + ((far2 - near2) / step2 + 1) ) * (segs+1) + 1
    property int indexCount: vertexCount
    property vector3d colorBoundp: Qt.vector3d( 0.7, 0.7, 0.7 )
    property var pbrightness: 0.0
    property var ptransparency: 0.5

    NodeInstantiator {
        active: parent.visible
        model: circleCount
        delegate: Entity {
            id: axisText
            property real pointSize: step/5
            property real range: near + step*index
            Entity {
                components: [ Transform{
                    translation: Qt.vector3d(0, axisText.range, 0)
                    rotationZ: 0
                    rotationY: camera.position.z<0?180:0
                }]
                Text2DEntity {
                    color: Qt.rgba(colorBoundp.x,colorBoundp.y,colorBoundp.z,ptransparency)
                    
                    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(colorBoundp.x,colorBoundp.y,colorBoundp.z,ptransparency)
                    
                    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(colorBoundp.x,colorBoundp.y,colorBoundp.z,ptransparency)
                    
                    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(colorBoundp.x,colorBoundp.y,colorBoundp.z,ptransparency)
                    
                    font.bold: true
                    font.pointSize: axisText.pointSize
                    text: axisText.range
                    width: text.length * font.pointSize
                    height: font.pointSize*2
                }
            }
        }
    }

    NodeInstantiator {
        // active: parent.visible
        model: circleCount2
        delegate: Entity {
            id: axisText
            property real pointSize: step2/5
            property real range: near2 + step2*index
            Entity {
                components: [ Transform{
                    translation: Qt.vector3d(0, axisText.range, 0)
                    rotationZ: 0
                    rotationY: camera.position.z<0?180:0
                }]
                Text2DEntity {
                    color: Qt.rgba(colorBoundp.x,colorBoundp.y,colorBoundp.z,ptransparency)
                    
                    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(colorBoundp.x,colorBoundp.y,colorBoundp.z,ptransparency)
                    
                    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(colorBoundp.x,colorBoundp.y,colorBoundp.z,ptransparency)
                    
                    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(colorBoundp.x,colorBoundp.y,colorBoundp.z,ptransparency)
                    
                    font.bold: true
                    font.pointSize: axisText.pointSize
                    text: axisText.range
                    width: text.length * font.pointSize
                    height: font.pointSize*2
                }
            }
        }
    }
    LineMaterial {
        id: line_material
        colorBound:colorBoundp
        brightness:pbrightness
        transparency:ptransparency
    }
    PhongAlphaMaterial  {
        id: phong_alpha_material
        alpha: 1
        ambient: Qt.rgba(colorBoundp.x, colorBoundp.y, colorBoundp.z, ptransparency)
        diffuse: Qt.rgba(colorBoundp.x, colorBoundp.y, colorBoundp.z, ptransparency)
        specular: Qt.rgba(0, 0, 0, 0)
    }
    GeometryRenderer {
        id: cartesion_geometry
        primitiveType: GeometryRenderer.LineLoop
        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
                }
            ]
        }
        Buffer {
            id: vertexBuffer
            type: Buffer.VertexBuffer
            data: {
                let vertexes = new Float32Array(vertexCount * 3)
                let index = 0
                vertexes[index++] = vertexes[index++] = vertexes[index++] = 0
                for (let dis = near; dis <= far; dis += step) {
                    for (let seg = 0; seg <= segs; seg++) {
                        let theta = 2*Math.PI*seg/segs;
                        vertexes[index++] = dis * Math.sin(theta)
                        vertexes[index++] = dis * Math.cos(theta)
                        vertexes[index++] = 0
                    }
                }
                for (let dis = near2; dis <= far2; dis += step2) {
                    for (let seg = 0; seg <= segs; seg++) {
                        let theta = 2*Math.PI*seg/segs;
                        vertexes[index++] = dis * Math.sin(theta)
                        vertexes[index++] = dis * Math.cos(theta)
                        vertexes[index++] = 0
                    }
                }
                return vertexes
            }
        }
    }

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