package solver

import (
	"slices"
	"testing"

	"gitea.ligthert.net/golang/sudoku-funpark/controller"
	"gitea.ligthert.net/golang/sudoku-funpark/outputter"
)

// This functions tests the PopulateBlocks function.
// It does this by creating a small set of rows and then
// calling the PopulateBlocks function. It then checks
// if the number of blocks is as expected.
func TestPopulateBlocks(t *testing.T) {
	// Do all the necesarry steps to initialize the solver.
	solver := Solver{}
	outp := outputter.Outputter{}
	outp.OutputType = "short"
	solver.Outp = &outp
	controller := controller.Controller{}
	solver.Controller = &controller
	solver.LoadBlocks()

	// Create a set of rows
	// Using the following values: [769154832 154832769 832769154 671945328 945328671 328671945 597416283 416283597 283597416]
	solver.Controller.Row1 = "769154800"
	solver.Controller.Row2 = "154832700"
	solver.Controller.Row3 = "832769100"
	solver.Controller.Row4 = "671945300"
	solver.Controller.Row5 = "945328600"
	solver.Controller.Row6 = "328671900"
	solver.Controller.Row7 = "597416200"
	solver.Controller.Row8 = "416283500"
	solver.Controller.Row9 = "283597400"

	// Call the PopulateBlocks function
	solver.PopulateBlocks()

	// Check if the number of blocks is as expected
	if solver.Iter != 512 {
		t.Errorf("Expected 19683 blocks, got %d", solver.Iter)
	}

	// Check if solver.row1s is as expected
	if slices.Compare(solver.row1s, []string{"769154823", "769154832"}) != 0 {
		t.Errorf("Expected [769154832, 769154823], got %s", solver.row1s)
	}

	// check if solver.row2s is as expected
	if slices.Compare(solver.row2s, []string{"154832769", "154832796"}) != 0 {
		t.Errorf("Expected [154832769, 154832796], got %s", solver.row2s)
	}

	// check if solver.row3s is as expected
	if slices.Compare(solver.row3s, []string{"832769145", "832769154"}) != 0 {
		t.Errorf("Expected [832769154, 832769145], got %s", solver.row3s)
	}

	// check if solver.row4s is as expected
	if slices.Compare(solver.row4s, []string{"671945328", "671945382"}) != 0 {
		t.Errorf("Expected [671945328, 671945382], got %s", solver.row4s)
	}

	// check if solver.row5s is as expected
	if slices.Compare(solver.row5s, []string{"945328617", "945328671"}) != 0 {
		t.Errorf("Expected [945328617, 945328671], got %s", solver.row5s)
	}

	// check if solver.row6s is as expected
	if slices.Compare(solver.row6s, []string{"328671945", "328671954"}) != 0 {
		t.Errorf("Expected [328671945, 328671954], got %s", solver.row6s)
	}

	// check if solver.row7s is as expected
	if slices.Compare(solver.row7s, []string{"597416238", "597416283"}) != 0 {
		t.Errorf("Expected [597416238, 597416283], got %s", solver.row7s)
	}

	// check if solver.row8s is as expected
	if slices.Compare(solver.row8s, []string{"416283579", "416283597"}) != 0 {
		t.Errorf("Expected [416283579, 416283597], got %s", solver.row8s)
	}

	// check if solver.row9s is as expected
	if slices.Compare(solver.row9s, []string{"283597416", "283597461"}) != 0 {
		t.Errorf("Expected [283597416, 283597461], got %s", solver.row9s)
	}

}
