<script setup lang="ts">
import { BufferGeometry, Matrix4, Vector3 } from 'three';

const props = defineProps(['index', 'models'])

const lamelles_count = use_lamelles_count()
const fence_section = use_fence_section()
const section_count = use_section_count()
const extra_section = use_extra_section()
const remove_pillar = use_remove_pillar()

const lSize = 0.115
const bSize = 0.0235
const pillar_size = 80 * 0.001

const pillar_one_pos = ref(fence_section.value * -0.5 - 0.015)
const pillar_two_pos = ref(fence_section.value * 0.5 + pillar_size + bSize - 0.01)

const scale_koef = 2.5
const show_pillar_one = ref(props.index == 1)
const show_pillar_two = ref(true)

const make_translate_to_section = (source = fence_section.value) => {
    const one_s = (source + pillar_size + bSize)
    let r = (props.index - 1) * one_s * scale_koef
    return r
}
const translate_to_section = ref(make_translate_to_section())

watch(fence_section, () => {
    translate_to_section.value = make_translate_to_section()
})

const extra = ref((extra_section && props.index == (section_count.value + 1)) ? extra_section.value * 0.001 : false)
if (extra.value) {
    pillar_one_pos.value = (extra.value as number) * -0.5 - 0.015
    pillar_two_pos.value = (extra.value as number) * 0.5 + pillar_size + bSize - 0.01
}

const instanced_lamelle = shallowRef();
const instanced_lamelle_geometry = Object.assign(new BufferGeometry, props.models.lamelle.children[0].geometry)
const instanced_lamelle_material = props.models.lamelle.children[0].material
const instanced_lamelle_count = 24
const instanced_v = [instanced_lamelle_geometry, instanced_lamelle_material, instanced_lamelle_count]
watch([instanced_lamelle, fence_section, extra_section, lamelles_count, extra], () => {
    for (let i = 0; i < instanced_lamelle_count; i++) {
        if (instanced_lamelle.value) {
            const scale_x = (((extra.value as number) || fence_section.value) * 10)
            const pos_x = pillar_size * 0.5
            const pos_y = (lSize * i)
            const pos_z = 0.02
            const newmatrix = new Matrix4().fromArray([
                scale_x, 0, 0, 0,
                0, 1, 0, 0,
                0, 0, 1, 0,
                pos_x, pos_y, pos_z, 1
            ])
            instanced_lamelle.value.setMatrixAt(i, newmatrix);

            if (i >= lamelles_count.value) {
                instanced_lamelle.value.setMatrixAt(i, new Matrix4().makeTranslation(new Vector3(0, 2, 2)));
            }
        }
        instanced_lamelle.value.instanceMatrix.needsUpdate = true
    }
});

watch([section_count, fence_section, extra_section], () => {
    extra.value = (extra_section.value && props.index == (section_count.value + 1)) ? extra_section.value * 0.001 : false
    if (extra_section && props.index == (section_count.value + 1)) {
        pillar_one_pos.value = (extra.value as number) * -0.5 - 0.015
        pillar_two_pos.value = (extra.value as number) * 0.5 + pillar_size + bSize - 0.01
        // translate_to_section.value = make_translate_to_section(extra_section.value * 0.001)
    } else {
        pillar_one_pos.value = fence_section.value * -0.5 - 0.015
        pillar_two_pos.value = fence_section.value * 0.5 + pillar_size + bSize - 0.01
        // translate_to_section.value = make_translate_to_section()
    }
})
</script>
<template>
    <TresGroup :scale="scale_koef" :position-x="translate_to_section" :name="`fence ${index}`"
        :position-y="extra ? 1 : 0">

        <TresGroup name="pillar_one" v-if="!remove_pillar && show_pillar_one" :position-x="pillar_one_pos"
            :position-z="0">
            <TresGroup :position-y="(lSize * -0.5)" :scale="[1, 0.5, 1]">
                <TresObject3D v-bind="props.models.fence.children[0]" />
            </TresGroup>
            <TresObject3D v-bind="props.models.fence.children[0]" :scale-y="lamelles_count" />
            <TresGroup :position-y="(lSize * lamelles_count)" :scale="[1, 0.5, 1]">
                <TresObject3D v-bind="props.models.fence.children[0]" />
            </TresGroup>
        </TresGroup>

        <TresGroup name="pillar_two" v-if="!remove_pillar && show_pillar_two" :position-x="pillar_two_pos"
            :position-z="0">
            <TresGroup :position-y="(lSize * -0.5)" :scale="[-1, 0.5, 1]">
                <TresObject3D v-bind="props.models.fence.children[0]" />
            </TresGroup>
            <TresObject3D v-bind="props.models.fence.children[0]" :scale="[-1, lamelles_count, 1]" />
            <TresGroup :position-y="(lSize * lamelles_count)" :scale="[-1, 0.5, 1]">
                <TresObject3D v-bind="props.models.fence.children[0]" />
            </TresGroup>
        </TresGroup>

        <TresGroup name="lamelles">
            <TresInstancedMesh ref="instanced_lamelle" :args="instanced_v" cast-shadow />
        </TresGroup>
        <TresGroup name="lam_fastening_one" :position-x="pillar_one_pos" :scale-y="lamelles_count">
            <TresObject3D v-bind="props.models.fastening.children[0]" />
        </TresGroup>
        <TresGroup name="lam_fastening_two" :position-x="pillar_two_pos" :scale="[-1, lamelles_count, 1]">
            <TresObject3D v-bind="props.models.fastening.children[0]" />
        </TresGroup>
        <TresGroup name="top_section" :scale-x="((extra as number) || fence_section) * 10"
            :position="[pillar_size * 0.5, lamelles_count * lSize, 0]">
            <TresObject3D v-bind="props.models.top.children[0]" />
        </TresGroup>
    </TresGroup>
</template>