<template>
  <div>
    <object3d ref="block3d" name="Project" :obj="building" :position="building_position"/>
    <mesh ref="label3d" name="Label" :position="label_position">
      <geometry type="Plane" :args="[label_size.x, label_size.y]" />
      <material type="MeshLambert" :options="label_opts">
        <texture :options="label_texture_opts" />
      </material>
    </mesh>
    <div v-if="isOpened">
      <ContentBlock
        v-for="item in data.visibles"
        :key="item.id"
        :prtPosition="position"
        :prtSize="size"
        type="visible"
        :data="item"
        />
      <ContentBlock
        v-for="item in data.contexts"
        :key="item.id"
        :prtPosition="position"
        :prtSize="size"
        type="context"
        :data="item"
        />
      <ContentBlock
        v-for="item in data.processes"
        :key="item.id"
        :prtPosition="position"
        :prtSize="size"
        type="process"
        :data="item"
        />
    </div>
  </div>
</template>

<script>
import * as THREE from 'three'
// import { toCSG, fromCSG } from 'three-csg'
import { ThreeBSP } from 'three-js-csg-es6'

import mixins from 'components/mixins'
import ContentBlock from 'components/objects/ContentBlock'

export default {
  name: 'Project',
  components: {
    ContentBlock
  },
  mixins: [mixins],
  // props: { size: Object, texture: String, position: Object, color: Number },
  props: { data: Object, len: Number, index: Number },
  data () {
    console.log('Project data() : data', this.data)
    // get size and positions from project store
    let size = { ...this.data.size }
    let position = { ...this.data.position }

    let materialOpts = {
      color: 0xffffff,
      side: THREE.DoubleSide
      // wireframe: true,
      // transparent: true,
      // opacity: 0.6
      // renderOrder: 0
    }
    let material = new THREE.MeshLambertMaterial(materialOpts)

    // http://learningthreejs.com/blog/2011/12/10/constructive-solid-geometry-with-csg-js/
    console.log('ThreeBSP', ThreeBSP)

    let wallW = 0.001

    let backGeom = new THREE.BoxGeometry(size.x, size.y, wallW)
    let backMesh = new THREE.Mesh(backGeom)
    backMesh.position.z = -0.5 * size.z
    let backBSP = new ThreeBSP(backMesh)

    let rightGeom = new THREE.BoxGeometry(wallW, size.y, size.z)
    let rightMesh = new THREE.Mesh(rightGeom)
    rightMesh.position.x = 0.5 * size.x
    // rightMesh.position.z = 0.5 * size.z
    let rightBSP = new ThreeBSP(rightMesh)

    let faceGeom = new THREE.BoxGeometry(size.x, size.y, wallW)
    let faceMesh = new THREE.Mesh(faceGeom)
    faceMesh.position.z = 0.5 * size.z
    let faceBSP = new ThreeBSP(faceMesh)

    let leftGeom = new THREE.BoxGeometry(wallW, size.y, size.z)
    let leftMesh = new THREE.Mesh(leftGeom)
    leftMesh.position.x = -0.5 * size.x
    // leftMesh.position.z = 0.5 * size.z
    let leftBSP = new ThreeBSP(leftMesh)

    let buildingBSP = backBSP.union(rightBSP)
    buildingBSP = buildingBSP.union(faceBSP)
    buildingBSP = buildingBSP.union(leftBSP)

    let holeGeom = new THREE.BoxGeometry(1000, 5, 5)
    let holeMesh = new THREE.Mesh(holeGeom)
    holeMesh.position.y = 50
    let holeBSP = new ThreeBSP(holeMesh)

    buildingBSP = buildingBSP.subtract(holeBSP)

    let building = buildingBSP.toMesh()
    building.material = material
    // building.geometry.computeVertexNormals()

    // let buildingGeom = fromCSG(boxCSG)
    // buildingGeom.computeVertexNormals()

    // result
    // let subtractCSG = boxMeshCSG.subtract(holeMeshCSG)
    // let building = fromCSG(boxMeshCSG) // converting CSG back into ThreeJS object
    //
    let buildingPos = { ...position }
    buildingPos.z -= 0.5 * size.z
    // building.castShadow = true
    // building.receiveShadow = true

    return {
      block3d: null,
      // block_opts: materialOpts,
      label3d: null,
      label_opts: {
        side: THREE.DoubleSide,
        // wireframe: false,
        transparent: true
        // opacity: 0.6
        // renderOrder: 0
      },
      // size and positions are defined in store project
      size: size,
      position: position,

      // geometry: geometry,
      building: building,
      building_position: buildingPos,
      label_position: { x: 0, y: 0, z: 0 },
      color: 0xffffff,
      label_canvas: null,
      label_size: null,
      isOpened: false
    }
  },
  computed: {
    label_texture_opts () {
      return {
        canvas: this.label_canvas,
        minFilter: THREE.LinearFilter,
        wrapS: THREE.ClampToEdgeWrapping,
        wrapT: THREE.ClampToEdgeWrapping
      }
    }
  },
  created () {
    console.log('Project created: data', this, this.data)

    this.label_canvas = this.createLabelCanvas(this.data.Titre.replace(/ /g, '\n').toUpperCase(), 48, 21, '#000000')
    this.label_position.x = this.position.x - this.size.x / 2 + this.label_size.x / 2 + 1
    this.label_position.y = this.position.y + this.size.y / 2 - this.label_size.y / 2 - 1
    this.label_position.z = this.position.z + this.size.z / 2 + 0.5
    // console.log('this.label_canvas', this.label_canvas)
  },
  mounted () {
    console.log('project mounted', this)
    // record self references

    this.block3d = this.$refs.block3d.curObj
    this.block3d.userData = {
      vnode: this
    }
    this.label3d = this.$refs.label3d.curObj

    // light
    // console.log('project mounted', this.$env3d)
    // var light = new THREE.AmbientLight(0xbf1a1a) // soft white light
    // this.$env3d.scene.add(light)
  }
  // ,
  // methods: {
  //
  // }
}
</script>
