package lsproduct

import "fmt"

// TestVersion defines version testing.
const TestVersion = 1

// LargestSeriesProduct Takes an input string of digits
// and a length for the series. It returns the largest
// series product from that string of that length.
func LargestSeriesProduct(input string, size int) (int, error) {
	if size == 0 {
		return 1, nil
	} else if size > len(input) {
		return 0, fmt.Errorf("Invalid Span Size")
	}
	var high int
	for i := 0; i < (len(input) - size + 1); i++ {
		try := GetProduct(input[i : i+size])
		if try < 0 {
			return 0, fmt.Errorf("Invalid input")
		}
		if try > high {
			high = try
		}
	}
	return high, nil
}

// GetProduct takes a string of numbers, splits it up
// and returns the product of them
func GetProduct(input string) int {
	if len(input) > 0 {
		total := 1
		for i := range input {
			next := input[i] - 48
			if next < 0 || next > 9 {
				return -1
			}
			total = total * int(next)
		}
		return total
	}
	return 0
}
