<script setup lang="ts">
import {
    ReinhardToneMapping, PCFSoftShadowMap, RepeatWrapping,
    Color, DataTexture,
    PMREMGenerator, Euler,
    FrontSide,
    CanvasTexture
} from 'three';
import { EXRLoader } from 'three/examples/jsm/Addons.js';
import { useTexture } from '@tresjs/core'
import { useGLTF, } from '@tresjs/cientos'
const section_count = useState('section_count')

const { scene, renderer, camera } = useTresContext()
renderer.value.toneMapping = ReinhardToneMapping

renderer.value.shadowMap.enabled = true
// renderer.value.shadowMap.type = PCFSoftShadowMap

// const pbrTexture = await useTexture({
//     map: '/texture/Grass01_MR_2K/Grass01_2K_BaseColor.png ',
//     displacementMap: '/texture/Grass01_MR_2K/Grass01_2K_Height.png ',
//     roughnessMap: '/texture/Grass01_MR_2K/Grass01_2K_Roughness.png',
//     normalMap: '/texture/Grass01_MR_2K/Grass01_2K_Normal.png ',
//     aoMap: '/texture/Grass01_MR_2K/Grass01_2K_AO.png ',
//     // metalnessMap: '/texture/Ground039_4K-JPG_Color.jpg',
//     // matcap: '/textures/Ground039_4K-JPG_Color.jpg',
//     // alphaMap: '/textures/myAlphaMapTexture.jpg'
// })
const pbrTexture = await useTexture({
    map: '/texture/Grass004_2K-PNG/Grass004_2K-PNG_Color.png',
    displacementMap: '/texture/Grass004_2K-PNG/Grass004_2K-PNG_Displacement.png',
    roughnessMap: '/texture/Grass004_2K-PNG/Grass004_2K-PNG_Roughness.png',
    normalMap: '/texture/Grass004_2K-PNG/Grass004_2K-PNG_NormalDX.png',
    aoMap: '/texture/Grass004_2K-PNG/Grass004_2K-PNG_AmbientOcclusion.png',
})
const groundMaterial = {
    map: pbrTexture.map,
    displacementMap: pbrTexture.displacementMap,
    normalMap: pbrTexture.normalMap,
    color: "#555",
    roughness: 0.25,
    metalness: 0,
    side: FrontSide
}
const repeat = 10
for (const key in pbrTexture) {
    if (Object.prototype.hasOwnProperty.call(pbrTexture, key)) {
        const key_p = key as keyof typeof pbrTexture
        const element = pbrTexture[key_p]
        if (element && element.wrapS) {
            element.wrapS = RepeatWrapping
            element.wrapT = RepeatWrapping
            element.repeat.x = repeat
            element.repeat.y = repeat
            element.flipY = false
        }
    }
}
const { scene: top } = await useGLTF('/models_one/verh_100.glb')
const { scene: fence } = await useGLTF('/models_one/fence.glb')
const { scene: fastening } = await useGLTF('/models_one/krepleniye_planok (1).glb')
const { scene: lamelle } = await useGLTF('/models_one/lamel_100.glb')
const { scene: light } = await useGLTF('/models_light/zabor_so_svetom.glb')

const { seek, seekAll } = useSeek()
watch(section_count, () => {
    const fences = seekAll(scene.value, 'name', 'fence')
    const base = seek(scene.value, 'name', 'base')
    const n = (section_count.value as number)
    if (fences.length > n) {
        for (let i = fences.length; i > n; i--) {
            const item = fences[i - 1]
            if (item) {
                base?.remove(item)
            }
        }
    }
})

const pmremGenerator = new PMREMGenerator(renderer.value);
pmremGenerator.compileEquirectangularShader();
onMounted(() => {
    new EXRLoader()
        .load('/hdrmaps/kloppenheim_06_4k.exr', function (texture) {
            const exrCubeRenderTarget = pmremGenerator.fromEquirectangular(texture);
            const exrBackground = exrCubeRenderTarget.texture;
            const newEnvMap = exrCubeRenderTarget ? exrCubeRenderTarget.texture : null;

            scene.value.environment = newEnvMap
            scene.value.background = exrBackground
            scene.value.backgroundRotation = new Euler(0, 5, 0)
            texture.dispose();
        });
})

const pointLight = light.children[2]
pointLight.color = '#f0dea9'
pointLight.shadow.camera.near = 50
pointLight.shadow.bias = -0.001
</script>
<template>
    <TresGroup :translate-y="-3.25" name="base">
        <TresMesh receive-shadow :position-y="-0.65" name="ground">
            <TresCircleGeometry :args="[100, 32]" :rotate-x="-Math.PI * 0.5" />
            <TresMeshStandardMaterial v-bind="groundMaterial" />
        </TresMesh>
        <TresMesh :position="Object.values(pointLight.position).map((el: any) => el * 2.5)" cast-shadow receive-shadow>
            <TresBoxGeometry :args="[1, 1, 1]" />
            <TresMeshStandardMaterial />
        </TresMesh>

        <TresPointLight v-bind="pointLight" :intensity="pointLight.intensity * 0.5"
            :position="Object.values(pointLight.position).map((el: any) => el * 3)" cast-shadow />

        <TresAmbientLight color="rgb(191,231,255)" :intensity="5" />
        <template v-if="false">
            <template v-for="i in light.children[1].children">
                <TresDirectionalLight :position="i.position" :intensity="5" color="rgb(191,231,255)" cast-shadow />
            </template>
        </template>

        <template v-for="i in section_count">
            <template v-if="i <= 20">
                <ModelFence :index="i" :models="{ top, fence, fastening, lamelle }" />
            </template>
        </template>
    </TresGroup>
</template>