package worldgen

import (
	"sync"

	packworld "edgaru089.ml/go/gl01/internal/world"
	"github.com/aquilax/go-perlin"
)

const (
	Alpha = 5 // Weight forming the noise sum
	Beta  = 2 // harmonic scaling/spacing
	N     = 3 // iterations

	AltitudeDensity = 10 // Density of the noise in altitude

	Sealevel = 63 // Space with Y=63 and lower should be the sea
	Highest  = 72 // Highest part of the terrain
	Lowest   = 56 // Lowest part of the terrain
)

var perlins map[int64]*perlin.Perlin
var perlinsLock sync.RWMutex

func init() {
	perlins = make(map[int64]*perlin.Perlin)
}

// Chunk generates the chunk (must with chunkID set!!) with seed.
func Chunk(chunk *packworld.Chunk, world *packworld.World, seed int64) {

	perlinsLock.RLock()
	p, ok := perlins[seed]
	if !ok {
		perlinsLock.RUnlock()
		perlinsLock.Lock()
		p = perlin.NewPerlin(Alpha, Beta, N, seed)
		perlins[seed] = p
		perlinsLock.Unlock()
	} else {
		perlinsLock.RUnlock()
	}

	//log.Printf("noise=%.5f", p.Noise2D(0.1, 0.1))

	offX := packworld.ChunkSizeX * chunk.X
	offZ := packworld.ChunkSizeZ * chunk.Z

	for x := 0; x < packworld.ChunkSizeX; x++ {
		for z := 0; z < packworld.ChunkSizeZ; z++ {

			height := Lowest + int(float64(Highest-Lowest)*p.Noise2D(
				float64(offX+x)/AltitudeDensity,
				float64(offZ+z)/AltitudeDensity))
			//log.Printf("height = %d (noise=%.5f)", height, p.Noise2D(float64(offX+x), float64(offZ+z)))

			// covering dirt
			chunk.Id[x][height][z] = packworld.BlockGrass
			//chunk.Id[x][height][z] = packworld.BlockDebugDir
			chunk.Id[x][height-1][z] = packworld.BlockDirt
			chunk.Id[x][height-2][z] = packworld.BlockDirt
			chunk.Id[x][height-3][z] = packworld.BlockDirt
			height -= 4

			// stone in the middle
			for y := height; y >= 1; y-- {
				chunk.Id[x][y][z] = packworld.BlockStone
			}

			// bedrock at the bottom
			chunk.Id[x][0][z] = packworld.BlockBedrock

			// plant trees
			if height >= 50 && height <= 70 {
				if p.Noise2D(float64(offX+x), float64(offZ+z)) > 0.9 {
				}
			}
		}
	}

	chunk.InvalidateRender()
}
