package main

import (
	"fmt"
	"runtime"
	"strconv"

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

func main() {
	// Instantiate the interfaces
	controller := controller.Controller{}
	outp := outputter.Outputter{}
	export := export.Export{Controller: &controller}
	flags := flags.Flags{Controller: &controller}
	solver := solver.Solver{Controller: &controller, Outp: &outp}

	// Parse and handle flags
	flags.ParseFlags()

	// Tell outp what kind of output is expected.
	outp.OutputType = controller.Print

	// Report number of CPUs being used, if set.
	if runtime.NumCPU() != controller.NumCPUs {
		outp.Println("Using " + strconv.Itoa(controller.NumCPUs) + " CPUs, (was " + strconv.Itoa(runtime.NumCPU()) + ")")
	}

	// Load blocks from CSV file
	solver.LoadBlocks()

	// Find rows that fit with the entered rows
	solver.PopulateBlocks()

	// If needed, split the workload
	// May exit and throw an error if the work load isn't viable
	if controller.Split != 1 {
		solver.SelectWorkload()
	}

	// Print the total number of solutions to validate
	outp.Println("Number of (potential) solutions:", solver.Iter)

	// Check the number of solutions
	go solver.CheckCombinations()
	solver.Tracker()

	fmt.Println(export.Export())

}
