<template>
  <div>
    <mesh ref="block3d" name="Project" :position="position">
      <geometry type="Box" :args="[size.x, size.y, size.z]" />
      <material type="MeshLambert" :color="color" :options="block_opts">
        <!-- <texture :options="label_texture_opts" /> -->
      </material>
    </mesh>
    <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="n in 10" :key="n" :prtPosition="position" :prtSize="size" />
    </div>
  </div>
</template>

<script>
import * as THREE from 'three'
// import { Base } from 'vue-threejs'

import ContentBlock from 'components/objects/ContentBlock'

export default {
  name: 'Project',
  components: {
    ContentBlock
  },
  // mixins: [Base],
  // props: { size: Object, texture: String, position: Object, color: Number },
  props: { data: Object, len: Number, index: Number },
  data: () => ({
    // mesh_opts: {
    //   userData: {
    //     test: 'Hello!'
    //   },
    //   receiveShadow: true,
    //   castShadow: true
    // },
    block3d: null,
    block_opts: {
      side: THREE.DoubleSide
      // wireframe: true,
      // transparent: true,
      // opacity: 0.6
      // renderOrder: 0
    },
    label3d: null,
    label_opts: {
      side: THREE.DoubleSide,
      // wireframe: false,
      transparent: false
      // opacity: 0.6
      // renderOrder: 0
    },
    size: { x: 35, y: 10, z: 10 },
    position: { x: 5, y: 5, z: 0 },
    label_position: { x: 5, y: 5, z: 6 },
    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('this.index', this.index)
    // randomize size and positions
    this.size.y = 100 + Math.random() * 90
    this.size.z = 10 + Math.random() * 30
    this.position.x = this.label_position.x = (-this.len + 1) / 2 * (this.size.x + 5) + (this.size.x + 5) * this.index
    this.position.y = -1 * this.size.y / 2 + 10 + Math.random() * 30// -10 + Math.random() * this.size.y / 2
    this.position.z = -10 + Math.random() * 10
    this.label_position.y = 5
    this.label_position.z = this.position.z + this.size.z / 2 + 0.1
    this.label_canvas = this.createLabelCanvas()
    // console.log('this.label_canvas', this.label_canvas)
    // mesh options
    this.block3d = this.$refs.block3d.curObj
    this.block3d.castShadow = true
    this.block3d.receiveShadow = true
    // record self references
    this.block3d.userData = {
      vnode: this
    }
    this.label3d = this.$refs.label3d.curObj
  },
  mounted () {
    // console.log('project mounted', this)

  },
  methods: {
    createLabelCanvas () {
      // console.log('createLabelCanvas', this.data.Titre)
      const size = 48
      const borderSize = 5
      const ctx = document.createElement('canvas').getContext('2d')
      const font = `${size}px bold noto_sans`
      ctx.font = font
      // measure how long the name will be
      const doubleBorderSize = borderSize * 2
      const width = ctx.measureText(this.data.Titre).width + doubleBorderSize
      const height = size + doubleBorderSize
      this.label_size = { x: width / 21, y: height / 21 }
      ctx.canvas.width = width
      ctx.canvas.height = height

      // need to set font again after resizing canvas
      ctx.font = font
      ctx.textBaseline = 'top'

      ctx.fillStyle = 'white'
      ctx.fillRect(0, 0, width, height)
      ctx.fillStyle = 'black'
      ctx.fillText(this.data.Titre, borderSize, borderSize)
      // console.log('createLabelCanvas', ctx)
      return ctx.canvas
    }
  }
}
</script>
