package flags

import (
	"fmt"
	"os"
)

// Validate if a row is properly set.
// This check for:
// - Correct length
// - Correct numbers
// - Numbers only present once
func (flags *Flags) validateRow(name string, row string) {

	var found bool
	var double bool
	count := make(map[rune]int)

	// 1. Make sure the row is 9 in length
	if len(row) != 9 {
		fmt.Printf("ERROR: Invalid length of %s (%s), must be 9 numbers\n\n", name, row)
		flags.printUsage()
		os.Exit(1)
	}

	// 2. Ensure all digits are numbers
	for _, value := range row {
		found = flags.validChar(value)
	}

	if !found {
		fmt.Printf("ERROR: Invalid character of %s (%s), must be 9 numbers\n\n", name, row)
		flags.printUsage()
		os.Exit(1)
	}

	// 3. Ensure all digits (except zero) are there only once
	for _, digits := range row {
		count[digits] = count[digits] + 1
	}

	for key, value := range count {
		if value > 1 && key != 48 {
			double = true
		}
	}

	if double {
		fmt.Printf("ERROR: Double character of %s (%s), numbers between 1 and 9 may only be entered once\n\n", name, row)
		flags.printUsage()
		os.Exit(1)
	}

}
