<script setup lang="ts">
//@ts-ignore
import { useGLTF } from '@tresjs/cientos'
import { getColorHexFromRal } from '../ral';

const explosion_state = use_explosion_state()
const globalFenceType = useGlobalFenceType()

const k = 1.5
const targetExplosion = {
    kosynka: [0 * k, 0 * k, 0.75 * k],
    krepleniye_planok: [0 * k, 0 * k, 0.5 * k],
    osnova_stolba: [0 * k, 0 * k, 0 * k],
    planki: [0 * k, 0 * k, -0.5 * k],
    stolb: [0 * k, 0 * k, 1 * k],
    verh: [0 * k, 0.25 * k, 0 * k],
}
const { scene: standart_kosynka } = await useGLTF('/models_exp/kosynka.glb')
const { scene: standart_krepleniye_planok } = await useGLTF('/models_exp/krepleniye_planok.glb')
const { scene: standart_osnova_stolba } = await useGLTF('/models_exp/osnova_stolba.glb')
const { scene: standart_planki } = await useGLTF('/models_exp/planki.glb')
const { scene: standart_stolb } = await useGLTF('/models_exp/stolb.glb')
const { scene: standart_verh } = await useGLTF('/models_exp/verh.glb')

const { scene: aristo_kosynka } = await useGLTF('/models_aristo_exp/kosynka.glb')
const { scene: aristo_krepleniye_planok } = await useGLTF('/models_aristo_exp/krepleniye_planok.glb')
const { scene: aristo_osnova_stolba } = await useGLTF('/models_aristo_exp/osnova_stolba.glb')
const { scene: aristo_planki } = await useGLTF('/models_aristo_exp/planki.glb')
const { scene: aristo_stolb } = await useGLTF('/models_aristo_exp/stolb.glb')
const { scene: aristo_verh } = await useGLTF('/models_aristo_exp/verh.glb')

const kosynka = ref()
const krepleniye_planok = ref()
const osnova_stolba = ref()
const planki = ref()
const stolb = ref()
const verh = ref()

const setModels = () => {
    const t = globalFenceType.value?.type || 'aristo'
    if (t == 'aristo') {
        kosynka.value = aristo_kosynka
        krepleniye_planok.value = aristo_krepleniye_planok
        osnova_stolba.value = aristo_osnova_stolba
        planki.value = aristo_planki
        stolb.value = aristo_stolb
        verh.value = aristo_verh
    }
    if (t == "standart") {
        kosynka.value = standart_kosynka
        krepleniye_planok.value = standart_krepleniye_planok
        osnova_stolba.value = standart_osnova_stolba
        planki.value = standart_planki
        stolb.value = standart_stolb
        verh.value = standart_verh
    }
}

setModels()

watch(() => globalFenceType.value?.type, (t) => {
    setModels()
    set_material(planki.value, getColorHexFromRal(lamelle_color.value));
    [stolb, verh, krepleniye_planok].map(el => set_material(el.value, getColorHexFromRal(pillar_color.value)))
})


const lamelle_color = use_lamelle_color()
const pillar_color = use_pillar_color()

set_material(planki.value, getColorHexFromRal(lamelle_color.value));
[stolb, verh, krepleniye_planok].map(el => set_material(el.value, getColorHexFromRal(pillar_color.value)))

</script>
<template>
    <Suspense>
        <TresGroup :position-y="globalFenceType && globalFenceType.type == 'aristo' ? -2 : -3.5" :scale="2"
            :key="globalFenceType?.type">
            <ModelItem :model="kosynka" :target="explosion_state ? targetExplosion.kosynka : [0, 0, 0]" />
            <ModelItem :model="krepleniye_planok"
                :target="explosion_state ? targetExplosion.krepleniye_planok : [0, 0, 0]" />
            <ModelItem :model="osnova_stolba" />
            <ModelItem :model="planki" :target="explosion_state ? targetExplosion.planki : [0, 0, 0]" />
            <ModelItem :model="stolb" :target="explosion_state ? targetExplosion.stolb : [0, 0, 0]" />
            <ModelItem :model="verh" :target="explosion_state ? targetExplosion.verh : [0, 0, 0]" />
        </TresGroup>
    </Suspense>
</template>