package export

import (
	"testing"

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

// This function will test the Export function.
// Starts off by creating all the structs and intefaces needed.
// Set all the needed values.
// Execute the Export function.
// Check if the output is a string.
// Check if the output for "human" "flat" and "json" are valid.
func TestExport(t *testing.T) {

	// Create a new Export struct.
	export := Export{}

	// Create a new outputter struct.
	outp := outputter.Outputter{}

	// Set output type to "short".
	outp.OutputType = "short"

	// Create a new Controller struct and set the outputter.
	controller := controller.Controller{Outputter: &outp}

	// Set the Controller in the Export struct.
	export.Controller = &controller

	// Populate the Solutions slice.
	controller.Solutions = append(controller.Solutions, []string{"123456789", "987654321", "123456789", "987654321", "123456789", "987654321", "123456789", "987654321", "123456789"})

	// Set output type to "human".
	controller.Output = "human"

	// Execute the Export function.
	render := export.Export()

	// Check if the output is a string and not empty.
	if render == "" {
		t.Error("Expected a non-empty string, string was empty")
	}

	// Set the expected variable.
	expected := "\nSolution #1:\n╔═══════════╗\n║123│456│789╢\n║987│654│321╢\n║123│456│789╢\n╟───┼───┼───╢\n║987│654│321╢\n║123│456│789╢\n║987│654│321╢\n╟───┼───┼───╢\n║123│456│789╢\n║987│654│321╢\n║123│456│789╢\n╚═══════════╝\n"

	// Check if the output is the same as the expected variable.
	if render != expected {
		t.Error("Expected a ", expected, ", got", render)
	}

	// Set output type to "flat".
	controller.Output = "flat"

	// Execute the Export function.
	render = export.Export()

	// Check if the output for "flat" is non-empty.
	if render == "" {
		t.Error("Expected a non-empty string, string was empty")
	}

	expected = "\nSolution #1:\n[123456789 987654321 123456789 987654321 123456789 987654321 123456789 987654321 123456789]\n"

	if render != expected {
		t.Error("Expected a ", expected, ", got", render)
	}

	// Set output type to "json".
	controller.Output = "json"

	// Execute the Export function.
	render = export.Export()

	// Check if the output for "json" is valid.
	if render == "" {
		t.Error("Expected a non-empty string, got empty string")
	}

	// Set the expected variable.
	expected = "[{\"order\":0,\"row1\":\"123456789\",\"row2\":\"987654321\",\"row3\":\"123456789\",\"row4\":\"987654321\",\"row5\":\"123456789\",\"row6\":\"987654321\",\"row7\":\"123456789\",\"row8\":\"987654321\",\"row9\":\"123456789\"}]"

	// Check if what is rendered is the same as the expected variable.
	if render != expected {
		t.Error("Expected a ", expected, ", got", render)
	}
}
