package bitmap_test

import (
	"fmt"
	"math/rand"
	"os"
	"strings"
	"testing"

	"galaxy/game/internal/bitmap"
)

func TestBitVectorSize(t *testing.T) {
	type testCase struct {
		width, height uint32
		expectSize    int
	}
	for _, tc := range []testCase{
		{
			width:      10,
			height:     10,
			expectSize: 4,
		},
		{
			width:      1,
			height:     1,
			expectSize: 1,
		},
		{
			width:      32,
			height:     32,
			expectSize: 32,
		},
	} {
		t.Run(fmt.Sprintf("w=%d h=%d s=%d", tc.width, tc.height, tc.expectSize), func(t *testing.T) {
			bm := bitmap.NewBitmap(tc.width, tc.height)
			l := len(bitmap.Value(bm))
			if tc.expectSize != l {
				t.Errorf("expected bitmap size: %d, got: %d", tc.expectSize, l)
			}
		})
	}
}

func TestSetPixel(t *testing.T) {
	type coord struct {
		x, y int
	}
	type testCase struct {
		width, height uint32
		pixels        []coord
		bits          []int
	}
	asMap := func(bits []int) map[int]bool {
		result := make(map[int]bool)
		for i := range bits {
			if _, ok := result[bits[i]]; ok {
				t.Fatalf("source bits duplicate at idx=%d", i)
			} else {
				result[bits[i]] = true
			}
		}
		return result
	}
	asUint32 := func(v bool) uint32 { return map[bool]uint32{true: 1, false: 0}[v] }
	for i, tc := range []testCase{
		{
			width:  5,
			height: 5,
			pixels: []coord{{0, 0}},
			bits:   []int{0},
		},
		{
			width:  5,
			height: 5,
			pixels: []coord{{1, 0}},
			bits:   []int{1},
		},
		{
			width:  5,
			height: 5,
			pixels: []coord{{2, 0}},
			bits:   []int{2},
		},
		{
			width:  5,
			height: 5,
			pixels: []coord{{0, 1}},
			bits:   []int{5},
		},
		{
			width:  5,
			height: 5,
			pixels: []coord{{4, 4}},
			bits:   []int{24},
		},
		{
			width:  8,
			height: 8,
			pixels: []coord{{7, 7}},
			bits:   []int{63},
		},
	} {
		t.Run(fmt.Sprintf("tc#%d", i), func(t *testing.T) {
			bm := bitmap.NewBitmap(tc.width, tc.height)
			for _, c := range tc.pixels {
				if bm.IsSet(c.x, c.y) {
					t.Errorf("expected pixel to be clear at x=%d y=%d", c.x, c.y)
				}
				bm.Set(c.x, c.y)
				if !bm.IsSet(c.x, c.y) {
					t.Errorf("expected pixel to be set at x=%d y=%d", c.x, c.y)
				}
			}
			bitVector := bitmap.Value(bm)
			bitNum := 0
			expected := asMap(tc.bits)
			for bi := range bitVector {
				for ; bitNum < (bi+1)*32; bitNum++ {
					if (bitVector[bi]>>(bitNum%32))&1 != asUint32(expected[bitNum]) {
						t.Errorf("expected: bit #%d to be %t, got %v", bitNum, expected[bitNum], uint32(1<<(bitNum%32)))
					}
				}
			}
		})
	}
}

func TestFreeCount(t *testing.T) {
	bm := bitmap.NewBitmap(10, 10)
	type testCase struct {
		x, y, expect int
	}
	for _, tc := range []testCase{
		{0, 0, 99},
		{5, 5, 98},
		{9, 9, 97},
		{10, 10, 97},
		{15, 6, 96},
		{9, 9, 96},
		{3, 8, 95},
	} {
		t.Run(fmt.Sprintf("x=%d,y=%d", tc.x, tc.y), func(t *testing.T) {
			bm.Set(tc.x, tc.y)
			count := bm.FreeCount()
			if tc.expect != count {
				t.Errorf("expected: %d, actual %d", tc.expect, count)
			}
		})
	}
}

func TestSetFreeN(t *testing.T) {
	bm := bitmap.NewBitmap(5, 5)
	type testCase struct {
		x, y, number int
	}
	for i, tc := range []testCase{
		{0, 0, 0},
		{1, 0, 0},
		{4, 0, 2},
		{2, 2, 9},
		{1, 1, 3},
		{3, 1, 4},
		{3, 2, 7},
		{4, 4, 17},
		{3, 0, 1},
	} {
		t.Run(fmt.Sprintf("tc#%d", i), func(t *testing.T) {
			if x, y, err := bm.GetFreeN(tc.number); err != nil {
				t.Errorf("get free by number=%d: %s", tc.number, err)
			} else if tc.x != x || tc.y != y {
				t.Fatalf("expected: x=%d, y=%d by number=%d, got: x=%d, y=%d", tc.x, tc.y, tc.number, x, y)
			}
			if err := bm.SetFreeN(tc.number); err != nil {
				t.Errorf("set free by number=%d: %s", tc.number, err)
			} else if !bm.IsSet(tc.x, tc.y) {
				t.Errorf("expected to be set: free_number=%d @ x=%d,y=%d bitmap:\n%s", tc.number, tc.x, tc.y, bm)
			}
		})
	}

	bm = bitmap.NewBitmap(2, 2)
	bm.Set(0, 0)
	bm.Set(1, 0)
	bm.Set(0, 1)
	if err := bm.SetFreeN(1); err == nil {
		t.Errorf("expected: error when free pixel number greater than free pixels count")
	}
	if _, _, err := bm.GetFreeN(1); err == nil {
		t.Errorf("expected: error when free pixel number greater than free pixels count")
	}
	bm.Set(1, 1)
	if err := bm.SetFreeN(0); err == nil {
		t.Errorf("expected: error when no free pixels left")
	}
}

func TestClear(t *testing.T) {
	var bm = bitmap.NewBitmap(10, 10)
	for range 50 {
		bm.Set(rand.Intn(10), rand.Intn(10))
	}
	var acc uint32
	for _, holder := range bitmap.Value(bm) {
		acc |= holder
	}
	if acc == 0 {
		t.Errorf("some pixels should be set")
	}
	bm.Clear()
	acc = 0
	for _, holder := range bitmap.Value(bm) {
		acc |= holder
	}
	if acc != 0 {
		t.Errorf("all pixels should be clear")
	}
}

func TestCircle(t *testing.T) {
	type testCase struct {
		x, y   int
		r      float64
		filled bool
	}
	for i, tc := range []testCase{
		{2, 2, 2, false},
		{0, 2, 2, true},
		{5, 5, 5, true},
		{5, 0, 5, false},
		{7, 7, 6.6, false},
	} {
		exampleFile := fmt.Sprintf("assets_test/circle_case_%02d.txt", i)
		t.Run(exampleFile, func(t *testing.T) {
			size := uint32(tc.r*2) + 2
			bm := bitmap.NewBitmap(size, size)
			bm.Circle(tc.x, tc.y, tc.r, tc.filled)
			b, err := os.ReadFile(exampleFile)
			if err != nil {
				t.Fatal(err)
			}
			expect := strings.TrimSpace(string(b))
			if expect != strings.TrimSpace(bm.String()) {
				t.Errorf("expect:\n%s\ngot:\n%s\n", expect, bm)
			}
		})
	}
}
