<script setup lang="ts">
import type { Object3D } from 'three';
import { getColorHexFromRal, type ralTypes } from '../ral';

const props = defineProps(['models'])

const pillar_color = use_pillar_color()
const pillar_pattern = use_pattern()
const lamelle_color = use_lamelle_color()
const lamelle_count = use_lamelles_count()

const { top, fence, fence_top, fence_bottom, fence_inner, fastening, lamelle } = props.models
const { scene } = useTresContext()
const { seekAll } = useSeek()

if (!pillar_color.value) {
    const r = Math.floor(Math.random() * predefPillarColors.length)
    pillar_color.value = predefPillarColors[r] as ralTypes
    lamelle_color.value = predefLamelleColors[r] as ralTypes
}

set_material(lamelle.value, getColorHexFromRal(lamelle_color.value));
[lamelle].map(
    (el: Object3D) => set_material(el, getColorHexFromRal(lamelle_color.value)));
[top, fastening, fence, fence_bottom, fence_top].map(
    (el: Object3D) => { set_material(el, getColorHexFromRal(pillar_color.value)) });
[fence_inner].map(
    (el: Object3D) => { set_material(el, getColorHexFromRal(pillar_color.value)) });

watch(lamelle_color, () => {
    [
        lamelle,
        ...seekAll(scene.value, 'name', "lamelles"),
    ].map((el: Object3D) => { set_material(el, getColorHexFromRal(lamelle_color.value)) });
})
watch(pillar_color, () => {
    [
        top, fastening, fence, fence_bottom, fence_top,
        ...seekAll(scene.value, 'name', "pillar_one"),
        ...seekAll(scene.value, 'name', "pillar_two"),
        ...seekAll(scene.value, 'name', "lam_fastening_one"),
        ...seekAll(scene.value, 'name', "lam_fastening_two"),
        ...seekAll(scene.value, 'name', "top_section"),
    ].map((el: Object3D) => { set_material(el, getColorHexFromRal(pillar_color.value)) });

})
watch([pillar_color, pillar_pattern, lamelle_count], () => {
    [fence_inner].map(
        (el: Object3D) => {
            set_material(el, getColorHexFromRal(pillar_color.value), {
                pattern: pillar_pattern.value,
                count: lamelle_count.value
            })
        });
})
</script>
<template>
</template>