package blocks

import (
	"edgaru089.ink/go/gl01/internal/asset"
	"edgaru089.ink/go/gl01/internal/util/itype"
	"edgaru089.ink/go/gl01/internal/world"
)

// texname is the full filename of the texture file (with .png)
// faceOffset is added to each vertex
func appendFace(face itype.Direction, pos itype.Vec3i, texname string, faceOffset itype.Vec3f, arr []world.Vertex) []world.Vertex {

	switch face {
	case itype.XPlus: // X+
		arr = append(arr,
			//     Vertex Position  Normal Vector(normalized)  Texture Coord  Light
			world.Vertex{pos.Addv(1, 0, 0).ToFloat32().Add(faceOffset), itype.Vec3f{1, 0, 0}, itype.Vec2f{1, 1}, 16},
			world.Vertex{pos.Addv(1, 1, 0).ToFloat32().Add(faceOffset), itype.Vec3f{1, 0, 0}, itype.Vec2f{1, 0}, 16},
			world.Vertex{pos.Addv(1, 1, 1).ToFloat32().Add(faceOffset), itype.Vec3f{1, 0, 0}, itype.Vec2f{0, 0}, 16},
			world.Vertex{pos.Addv(1, 0, 0).ToFloat32().Add(faceOffset), itype.Vec3f{1, 0, 0}, itype.Vec2f{1, 1}, 16},
			world.Vertex{pos.Addv(1, 1, 1).ToFloat32().Add(faceOffset), itype.Vec3f{1, 0, 0}, itype.Vec2f{0, 0}, 16},
			world.Vertex{pos.Addv(1, 0, 1).ToFloat32().Add(faceOffset), itype.Vec3f{1, 0, 0}, itype.Vec2f{0, 1}, 16},
		)
	case itype.XMinus: // X-
		arr = append(arr,
			world.Vertex{pos.Addv(0, 0, 0).ToFloat32().Add(faceOffset), itype.Vec3f{-1, 0, 0}, itype.Vec2f{0, 1}, 16},
			world.Vertex{pos.Addv(0, 1, 1).ToFloat32().Add(faceOffset), itype.Vec3f{-1, 0, 0}, itype.Vec2f{1, 0}, 16},
			world.Vertex{pos.Addv(0, 1, 0).ToFloat32().Add(faceOffset), itype.Vec3f{-1, 0, 0}, itype.Vec2f{0, 0}, 16},
			world.Vertex{pos.Addv(0, 0, 0).ToFloat32().Add(faceOffset), itype.Vec3f{-1, 0, 0}, itype.Vec2f{0, 1}, 16},
			world.Vertex{pos.Addv(0, 0, 1).ToFloat32().Add(faceOffset), itype.Vec3f{-1, 0, 0}, itype.Vec2f{1, 1}, 16},
			world.Vertex{pos.Addv(0, 1, 1).ToFloat32().Add(faceOffset), itype.Vec3f{-1, 0, 0}, itype.Vec2f{1, 0}, 16},
		)
	case itype.YPlus: // Y+
		arr = append(arr,
			world.Vertex{pos.Addv(0, 1, 0).ToFloat32().Add(faceOffset), itype.Vec3f{0, 1, 0}, itype.Vec2f{0, 0}, 16},
			world.Vertex{pos.Addv(0, 1, 1).ToFloat32().Add(faceOffset), itype.Vec3f{0, 1, 0}, itype.Vec2f{0, 1}, 16},
			world.Vertex{pos.Addv(1, 1, 0).ToFloat32().Add(faceOffset), itype.Vec3f{0, 1, 0}, itype.Vec2f{1, 0}, 16},
			world.Vertex{pos.Addv(0, 1, 1).ToFloat32().Add(faceOffset), itype.Vec3f{0, 1, 0}, itype.Vec2f{0, 1}, 16},
			world.Vertex{pos.Addv(1, 1, 1).ToFloat32().Add(faceOffset), itype.Vec3f{0, 1, 0}, itype.Vec2f{1, 1}, 16},
			world.Vertex{pos.Addv(1, 1, 0).ToFloat32().Add(faceOffset), itype.Vec3f{0, 1, 0}, itype.Vec2f{1, 0}, 16},
		)
	case itype.YMinus: // Y-
		arr = append(arr,
			world.Vertex{pos.Addv(0, 0, 0).ToFloat32().Add(faceOffset), itype.Vec3f{0, -1, 0}, itype.Vec2f{1, 0}, 16},
			world.Vertex{pos.Addv(1, 0, 0).ToFloat32().Add(faceOffset), itype.Vec3f{0, -1, 0}, itype.Vec2f{0, 0}, 16},
			world.Vertex{pos.Addv(1, 0, 1).ToFloat32().Add(faceOffset), itype.Vec3f{0, -1, 0}, itype.Vec2f{0, 1}, 16},
			world.Vertex{pos.Addv(0, 0, 0).ToFloat32().Add(faceOffset), itype.Vec3f{0, -1, 0}, itype.Vec2f{1, 0}, 16},
			world.Vertex{pos.Addv(1, 0, 1).ToFloat32().Add(faceOffset), itype.Vec3f{0, -1, 0}, itype.Vec2f{0, 1}, 16},
			world.Vertex{pos.Addv(0, 0, 1).ToFloat32().Add(faceOffset), itype.Vec3f{0, -1, 0}, itype.Vec2f{1, 1}, 16},
		)
	case itype.ZPlus: // Z+
		arr = append(arr,
			world.Vertex{pos.Addv(0, 0, 1).ToFloat32().Add(faceOffset), itype.Vec3f{0, 0, 1}, itype.Vec2f{0, 1}, 16},
			world.Vertex{pos.Addv(1, 0, 1).ToFloat32().Add(faceOffset), itype.Vec3f{0, 0, 1}, itype.Vec2f{1, 1}, 16},
			world.Vertex{pos.Addv(0, 1, 1).ToFloat32().Add(faceOffset), itype.Vec3f{0, 0, 1}, itype.Vec2f{0, 0}, 16},
			world.Vertex{pos.Addv(1, 1, 1).ToFloat32().Add(faceOffset), itype.Vec3f{0, 0, 1}, itype.Vec2f{1, 0}, 16},
			world.Vertex{pos.Addv(0, 1, 1).ToFloat32().Add(faceOffset), itype.Vec3f{0, 0, 1}, itype.Vec2f{0, 0}, 16},
			world.Vertex{pos.Addv(1, 0, 1).ToFloat32().Add(faceOffset), itype.Vec3f{0, 0, 1}, itype.Vec2f{1, 1}, 16},
		)
	case itype.ZMinus: // Z-
		arr = append(arr,
			world.Vertex{pos.Addv(0, 0, 0).ToFloat32().Add(faceOffset), itype.Vec3f{0, 0, -1}, itype.Vec2f{1, 1}, 16},
			world.Vertex{pos.Addv(0, 1, 0).ToFloat32().Add(faceOffset), itype.Vec3f{0, 0, -1}, itype.Vec2f{1, 0}, 16},
			world.Vertex{pos.Addv(1, 1, 0).ToFloat32().Add(faceOffset), itype.Vec3f{0, 0, -1}, itype.Vec2f{0, 0}, 16},
			world.Vertex{pos.Addv(0, 0, 0).ToFloat32().Add(faceOffset), itype.Vec3f{0, 0, -1}, itype.Vec2f{1, 1}, 16},
			world.Vertex{pos.Addv(1, 1, 0).ToFloat32().Add(faceOffset), itype.Vec3f{0, 0, -1}, itype.Vec2f{0, 0}, 16},
			world.Vertex{pos.Addv(1, 0, 0).ToFloat32().Add(faceOffset), itype.Vec3f{0, 0, -1}, itype.Vec2f{0, 1}, 16},
		)
	}

	texrect := asset.WorldTextureAtlas.RectNormalized(texname)
	for i := len(arr) - 6; i < len(arr); i++ {
		arr[i].Texture[0] = texrect.Left + arr[i].Texture[0]*texrect.Width
		arr[i].Texture[1] = texrect.Top + arr[i].Texture[1]*texrect.Height
	}

	return arr
}

// same as appendFace
// faceSize is in [0, 1]
// textureSubrect is normalized, also in [0, 1]; coords start from the left-top
func appendFaceSized(face itype.Direction, pos itype.Vec3i, texname string, faceSize itype.Vec3f, faceOffset itype.Vec3f, textureSubrect itype.Rectf, arr []world.Vertex) []world.Vertex {

	switch face {
	case itype.XPlus: // X+
		arr = append(arr,
			//     Vertex Position  Normal Vector(normalized)  Texture Coord  Light
			world.Vertex{pos.ToFloat32().Addv(1, 0, 0).
				Add(faceOffset), itype.Vec3f{1, 0, 0}, itype.Vec2f{1, 1}, 16},
			world.Vertex{pos.ToFloat32().Addv(1, faceSize[1], 0).
				Add(faceOffset), itype.Vec3f{1, 0, 0}, itype.Vec2f{1, 0}, 16},
			world.Vertex{pos.ToFloat32().Addv(1, faceSize[1], faceSize[2]).
				Add(faceOffset), itype.Vec3f{1, 0, 0}, itype.Vec2f{0, 0}, 16},
			world.Vertex{pos.ToFloat32().Addv(1, 0, 0).
				Add(faceOffset), itype.Vec3f{1, 0, 0}, itype.Vec2f{1, 1}, 16},
			world.Vertex{pos.ToFloat32().Addv(1, faceSize[1], faceSize[2]).
				Add(faceOffset), itype.Vec3f{1, 0, 0}, itype.Vec2f{0, 0}, 16},
			world.Vertex{pos.ToFloat32().Addv(1, 0, faceSize[2]).
				Add(faceOffset), itype.Vec3f{1, 0, 0}, itype.Vec2f{0, 1}, 16},
		)
	case itype.XMinus: // X-
		arr = append(arr,
			world.Vertex{pos.ToFloat32().Addv(0, 0, 0).
				Add(faceOffset), itype.Vec3f{-1, 0, 0}, itype.Vec2f{0, 1}, 16},
			world.Vertex{pos.ToFloat32().Addv(0, faceSize[1], faceSize[2]).
				Add(faceOffset), itype.Vec3f{-1, 0, 0}, itype.Vec2f{1, 0}, 16},
			world.Vertex{pos.ToFloat32().Addv(0, faceSize[1], 0).
				Add(faceOffset), itype.Vec3f{-1, 0, 0}, itype.Vec2f{0, 0}, 16},
			world.Vertex{pos.ToFloat32().Addv(0, 0, 0).
				Add(faceOffset), itype.Vec3f{-1, 0, 0}, itype.Vec2f{0, 1}, 16},
			world.Vertex{pos.ToFloat32().Addv(0, 0, faceSize[2]).
				Add(faceOffset), itype.Vec3f{-1, 0, 0}, itype.Vec2f{1, 1}, 16},
			world.Vertex{pos.ToFloat32().Addv(0, faceSize[1], faceSize[2]).
				Add(faceOffset), itype.Vec3f{-1, 0, 0}, itype.Vec2f{1, 0}, 16},
		)
	case itype.YPlus: // Y+
		arr = append(arr,
			world.Vertex{pos.ToFloat32().Addv(0, 1, 0).
				Add(faceOffset), itype.Vec3f{0, 1, 0}, itype.Vec2f{0, 0}, 16},
			world.Vertex{pos.ToFloat32().Addv(0, 1, faceSize[2]).
				Add(faceOffset), itype.Vec3f{0, 1, 0}, itype.Vec2f{0, 1}, 16},
			world.Vertex{pos.ToFloat32().Addv(faceSize[0], 1, 0).
				Add(faceOffset), itype.Vec3f{0, 1, 0}, itype.Vec2f{1, 0}, 16},
			world.Vertex{pos.ToFloat32().Addv(0, 1, faceSize[2]).
				Add(faceOffset), itype.Vec3f{0, 1, 0}, itype.Vec2f{0, 1}, 16},
			world.Vertex{pos.ToFloat32().Addv(faceSize[0], 1, faceSize[2]).
				Add(faceOffset), itype.Vec3f{0, 1, 0}, itype.Vec2f{1, 1}, 16},
			world.Vertex{pos.ToFloat32().Addv(faceSize[0], 1, 0).
				Add(faceOffset), itype.Vec3f{0, 1, 0}, itype.Vec2f{1, 0}, 16},
		)
	case itype.YMinus: // Y-
		arr = append(arr,
			world.Vertex{pos.ToFloat32().Addv(0, 0, 0).
				Add(faceOffset), itype.Vec3f{0, -1, 0}, itype.Vec2f{1, 0}, 16},
			world.Vertex{pos.ToFloat32().Addv(faceSize[0], 0, 0).
				Add(faceOffset), itype.Vec3f{0, -1, 0}, itype.Vec2f{0, 0}, 16},
			world.Vertex{pos.ToFloat32().Addv(faceSize[0], 0, faceSize[2]).
				Add(faceOffset), itype.Vec3f{0, -1, 0}, itype.Vec2f{0, 1}, 16},
			world.Vertex{pos.ToFloat32().Addv(0, 0, 0).
				Add(faceOffset), itype.Vec3f{0, -1, 0}, itype.Vec2f{1, 0}, 16},
			world.Vertex{pos.ToFloat32().Addv(faceSize[0], 0, faceSize[2]).
				Add(faceOffset), itype.Vec3f{0, -1, 0}, itype.Vec2f{0, 1}, 16},
			world.Vertex{pos.ToFloat32().Addv(0, 0, faceSize[2]).
				Add(faceOffset), itype.Vec3f{0, -1, 0}, itype.Vec2f{1, 1}, 16},
		)
	case itype.ZPlus: // Z+
		arr = append(arr,
			world.Vertex{pos.ToFloat32().Addv(0, 0, 1).
				Add(faceOffset), itype.Vec3f{0, 0, 1}, itype.Vec2f{0, 1}, 16},
			world.Vertex{pos.ToFloat32().Addv(faceSize[0], 0, 1).
				Add(faceOffset), itype.Vec3f{0, 0, 1}, itype.Vec2f{1, 1}, 16},
			world.Vertex{pos.ToFloat32().Addv(0, faceSize[1], 1).
				Add(faceOffset), itype.Vec3f{0, 0, 1}, itype.Vec2f{0, 0}, 16},
			world.Vertex{pos.ToFloat32().Addv(faceSize[0], faceSize[1], 1).
				Add(faceOffset), itype.Vec3f{0, 0, 1}, itype.Vec2f{1, 0}, 16},
			world.Vertex{pos.ToFloat32().Addv(0, faceSize[1], 1).
				Add(faceOffset), itype.Vec3f{0, 0, 1}, itype.Vec2f{0, 0}, 16},
			world.Vertex{pos.ToFloat32().Addv(faceSize[0], 0, 1).
				Add(faceOffset), itype.Vec3f{0, 0, 1}, itype.Vec2f{1, 1}, 16},
		)
	case itype.ZMinus: // Z-
		arr = append(arr,
			world.Vertex{pos.ToFloat32().Addv(0, 0, 0).
				Add(faceOffset), itype.Vec3f{0, 0, -1}, itype.Vec2f{1, 1}, 16},
			world.Vertex{pos.ToFloat32().Addv(0, faceSize[1], 0).
				Add(faceOffset), itype.Vec3f{0, 0, -1}, itype.Vec2f{1, 0}, 16},
			world.Vertex{pos.ToFloat32().Addv(faceSize[0], faceSize[1], 0).
				Add(faceOffset), itype.Vec3f{0, 0, -1}, itype.Vec2f{0, 0}, 16},
			world.Vertex{pos.ToFloat32().Addv(0, 0, 0).
				Add(faceOffset), itype.Vec3f{0, 0, -1}, itype.Vec2f{1, 1}, 16},
			world.Vertex{pos.ToFloat32().Addv(faceSize[0], faceSize[1], 0).
				Add(faceOffset), itype.Vec3f{0, 0, -1}, itype.Vec2f{0, 0}, 16},
			world.Vertex{pos.ToFloat32().Addv(faceSize[0], 0, 0).
				Add(faceOffset), itype.Vec3f{0, 0, -1}, itype.Vec2f{0, 1}, 16},
		)
	}

	texrect := asset.WorldTextureAtlas.RectNormalized(texname)
	for i := len(arr) - 6; i < len(arr); i++ {
		arr[i].Texture[0] = texrect.Left + (textureSubrect.Left+textureSubrect.Width*arr[i].Texture[0])*texrect.Width
		arr[i].Texture[1] = texrect.Top + (textureSubrect.Top+textureSubrect.Height*arr[i].Texture[1])*texrect.Height
	}

	return arr
}
