<script setup lang="ts">
import { ralClassicPallette } from '@/components/ral'
const props = defineProps(['index', 'models'])

const lamelles_count = useState<number>('lamelles_count')
const fence_section = useState<number>('fence_section')
const pillar_color = useState('pillar_color')
const lamelle_color = useState('lamelle_color')

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.01)
const pillar_two_pos = ref(fence_section.value * 0.5 + pillar_size + bSize - 0.01)

watch([fence_section, lamelles_count], () => {
    pillar_one_pos.value = fence_section.value * -0.5 - 0.01
    pillar_two_pos.value = fence_section.value * 0.5 + pillar_size + bSize - 0.01
})
const scale_koef = 3
const show_pillar_one = ref(props.index == 1)
const show_pillar_two = ref(true)
const make_translate_to_section = () => {
    let r = ((props.index - 1) / 2) * (fence_section.value + pillar_size + bSize) * scale_koef
    if (props.index % 2 == 0) {
        show_pillar_two.value = false
        show_pillar_one.value = true
        r += (fence_section.value * 0.5 + bSize) * scale_koef
        return -1 * r
    }
    return r
}
const translate_to_section = ref(make_translate_to_section())

watch(fence_section, () => {
    translate_to_section.value = make_translate_to_section()
})
</script>
<template>
    <TresGroup :scale="scale_koef" :position-x="translate_to_section" :name="`fence ${index}`">
        <TresGroup name="pillar_one" v-if="show_pillar_one" :position-x="pillar_one_pos" :position-z="0">
            <TresGroup :position-y="(lSize * -0.5)" :scale="[1, 0.5, 1]">
                <ModelItem :model="props.models.fence" :color="ralClassicPallette[pillar_color].hex" />
            </TresGroup>
            <template v-for="(n, i) in lamelles_count">
                <TresGroup :position-y="(lSize * i)">
                    <ModelItem :model="props.models.fence" :color="ralClassicPallette[pillar_color].hex" />
                </TresGroup>
            </template>
            <TresGroup :position-y="(lSize * lamelles_count)" :scale="[1, 0.5, 1]">
                <ModelItem :model="props.models.fence" :color="ralClassicPallette[pillar_color].hex" />
            </TresGroup>
        </TresGroup>

        <TresGroup name="pillar_two" v-if="show_pillar_two" :position-x="pillar_two_pos" :position-z="0">
            <TresGroup :position-y="(lSize * -0.5)" :scale="[-1, 0.5, 1]">
                <ModelItem :model="props.models.fence" :color="ralClassicPallette[pillar_color].hex" />
            </TresGroup>
            <template v-for="(n, i) in lamelles_count">
                <TresGroup :position-y="(lSize * i)" :scale="[-1, 1, 1]">
                    <ModelItem :model="props.models.fence" :color="ralClassicPallette[pillar_color].hex" />
                </TresGroup>
            </template>
            <TresGroup :position-y="(lSize * lamelles_count)" :scale="[-1, 0.5, 1]">
                <ModelItem :model="props.models.fence" :color="ralClassicPallette[pillar_color].hex" />
            </TresGroup>
        </TresGroup>

        <TresGroup name="lamelles">
            <template v-for="(n, i) in lamelles_count">
                <TresGroup :position="[pillar_size * 0.5, (lSize * i), 0.02]" :scale-x="fence_section * 10">
                    <ModelItem :model="props.models.lamelle" :color="ralClassicPallette[lamelle_color].hex" />
                </TresGroup>
            </template>
        </TresGroup>
        <TresGroup name="lamelles_fastening_one">
            <template v-for="(n, i) in lamelles_count">
                <TresGroup :position-y="(lSize * i)" :position-x="pillar_one_pos">
                    <ModelItem :model="props.models.fastening" :color="ralClassicPallette[pillar_color].hex" />
                </TresGroup>
            </template>
        </TresGroup>
        <TresGroup name="lamelles_fastening_two">
            <template v-for="(n, i) in lamelles_count">
                <TresGroup :position-y="(lSize * i)" :scale="[-1, 1, 1]" :position-x="pillar_two_pos">
                    <ModelItem :model="props.models.fastening" :remove-pos="true"
                        :color="ralClassicPallette[pillar_color].hex" />
                </TresGroup>
            </template>
        </TresGroup>
        <TresGroup name="top_section" :scale-x="fence_section * 10"
            :position="[pillar_size * 0.5, lamelles_count * lSize, 0]">
            <ModelItem :model="props.models.top" :color="ralClassicPallette[pillar_color].hex" />
        </TresGroup>
    </TresGroup>
</template>