package precomputed_shade

import (
	"fmt"
	"lab.zaar.be/thefish/alchemyst-go/engine/gamemap"
	"lab.zaar.be/thefish/alchemyst-go/engine/types"
	"testing"
)

func TestPsDistance(t *testing.T) {
    iterCoords := types.Coords{X: 0, Y: 0}

	fmt.Printf("\n dto: \t %v", iterCoords.DistanceTo(types.Coords{X: 0, Y: 1}))
	fmt.Printf("\n dto: \t %v", iterCoords.DistanceTo(types.Coords{X: 0, Y: 5}))
	fmt.Printf("\n dto: \t %v", iterCoords.DistanceTo(types.Coords{X: 3, Y: 3}))
	fmt.Printf("\n dto: \t %v", iterCoords.DistanceTo(types.Coords{X: 100, Y: 0}))
}

func TestPrecompShade(t *testing.T) {
	ppFov := NewPrecomputedShade(15)
	_ = ppFov
	t.Log("ok")

	//level := gamemap.NewLevel(util.ClientCtx{}, "test", 1)

	level := &gamemap.Level{
		Name:  "test1",
		Depth: 1,
		Rect:  types.NewRect(0, 0, 20, 20),
	}

	level.Tiles = make([]*gamemap.Tile, level.W * level.H)

	var tile func() (*gamemap.Tile)

	for x := 0; x < level.W; x++ {
		for y := 0; y < level.H; y++ {
			if x == 0 || y == 0 || x == (level.W-1) || y == (level.H-1) {
				tile = gamemap.NewWall
			} else {
				tile = gamemap.NewFloor
			}
			level.SetTileByXY(x, y, tile())
		}
	}

	playerCoords := types.Coords{X: 10, Y: 10}

	level.SetTileByXY(8, 12, gamemap.NewWall())
	level.SetTileByXY(10, 8, gamemap.NewWall())

	level.SetTileByXY(7, 9, gamemap.NewWall())
	level.SetTileByXY(7, 11, gamemap.NewWall())
	level.SetTileByXY(5, 10, gamemap.NewWall())

	level.SetTileByXY(10, 11, gamemap.NewWall())
	level.SetTileByXY(10, 12, gamemap.NewWall())
	level.SetTileByXY(10, 13, gamemap.NewWall())

	level.SetTileByXY(11, 10, gamemap.NewWall())

	ppFov.ComputeFov(level, playerCoords, 12)

	fmt.Printf("\n\n")

	var render = func(x, y int) string {
		if playerCoords.X == x && playerCoords.Y == y {
			return "@"
		}
		result := level.GetTileByXY(x, y).Glyph.GetGlyph()
		if !level.GetTileByXY(x, y).Visible {
			result = "?"
		}
		return result
	}

	for y := 0; y < level.H; y++ {
		for x := 0; x < level.W; x++ {

			fmt.Printf("%s", render(x, y))
		}
		fmt.Printf("\n")
	}

}
