// Check if we follow the criteria of the exception 3
func FollowsException3(b m.BankAccount, scData m.SortCodeData) bool {
	c := b.NumberAtPosition("c")

	hasNextAnd3 := scData.HasNext() && scData.Next.IsException(3)

	return (scData.IsException(3) || hasNextAnd3) && (c == 6 || c == 9)
}
// Tell if the bank account is valid
func (e Exception5Checker) IsValid(b m.BankAccount, sc m.SortCodeData, attempt int) bool {
	if !e.Handles(b, sc, attempt) {
		panic("Should be exception of type 5")
	}

	if substitution, hasKey := e.Substitutions[b.SortCode]; hasKey {
		b.SortCode = substitution
	}

	// First attempt
	if attempt == 1 {
		checkDigit := b.NumberAtPosition("g")
		remainder := GeneralChecker{}.RemainderFromRegularCheck(b, sc)
		if remainder == 0 && checkDigit == 0 {
			return true
		}
		if remainder == 1 {
			return false
		}

		return (11 - remainder) == checkDigit
	}

	// Second attempt
	checkDigit := b.NumberAtPosition("h")
	remainder := GeneralChecker{}.RemainderFromRegularCheck(b, sc)
	if remainder == 0 && checkDigit == 0 {
		return true
	}

	return (10 - remainder) == checkDigit
}
// Get weights to use for a bank account following the exception 2 or 9.
func WeightsForException2Or9(b m.BankAccount, sc m.SortCodeData) (weights []int) {
	if !(sc.IsException(2) || sc.IsException(9)) {
		panic("Expected exception 2 or exception 9 sort code")
	}

	a := b.NumberAtPosition("a")
	g := b.NumberAtPosition("g")

	// Default weights
	weights = sc.Weights

	switch {
	case a != 0 && g != 9:
		weights = []int{
			0, 0, 1, 2, 5, 3,
			6, 4, 8, 7, 10, 9, 3, 1,
		}
	case a != 0 && g == 9:
		weights = []int{
			0, 0, 0, 0, 0, 0,
			0, 0, 8, 7, 10, 9, 3, 1,
		}
	}

	return
}
// Check if a bank account is in a foreign currency
func (e Exception6Checker) isForeignCurrency(b m.BankAccount) bool {
	// if a = 4, 5, 6, 7 or 8, and g and h are the same,
	// the accounts are for a foreign currency and the checks cannot be used
	a := b.NumberAtPosition("a")
	g := b.NumberAtPosition("g")
	h := b.NumberAtPosition("h")

	return a >= 4 && a <= 8 && (g == h)
}
// Tell if the bank account is valid
func (e Exception4Checker) IsValid(b m.BankAccount, sc m.SortCodeData, attempt int) bool {
	if !e.Handles(b, sc, attempt) {
		panic("Should be exception of type 4")
	}

	g := b.NumberAtPosition("g")
	h := b.NumberAtPosition("h")
	checkDigit := g*10 + h

	return GeneralChecker{}.RemainderFromRegularCheck(b, sc) == checkDigit
}
// Determine if the checker is able to validate the bank account
func (e Exception10Checker) Handles(account m.BankAccount, sc m.SortCodeData, attempt int) bool {
	if !sc.IsException(10) {
		return false
	}

	// if ab = 09 or ab = 99 and g = 9
	a := account.NumberAtPosition("a")
	b := account.NumberAtPosition("b")
	g := account.NumberAtPosition("g")

	return (a == 0 || a == 9) && b == 9 && g == 9
}
// Perform the double alternate algorithm and return
// the remainder of the operation
func DoubleAlternate(b m.BankAccount, data m.SortCodeData, sum int) (remainder int) {
	numbers := b.MergeAccountDetails()

	weights := data.Weights
	for i, nb := range numbers {
		sum += helpers.AddDigits(weights[i] * nb)
	}

	remainder = sum % 10

	return
}
// Perform the modulus algorithm with a given modulus
// and return the remainder of the operation
func Modulus(b m.BankAccount, modulus int, data m.SortCodeData) (remainder int) {
	numbers := b.MergeAccountDetails()
	sum := 0
	weights := data.Weights

	for i, nb := range numbers {
		sum += weights[i] * nb
	}

	remainder = sum % modulus

	return
}
// Tell if the bank account is valid
func (e Exception14Checker) IsValid(b m.BankAccount, sc m.SortCodeData, attempt int) bool {
	if !e.Handles(b, sc, attempt) {
		panic("Should be exception of type 14 at attempt 2")
	}

	h := b.NumberAtPosition("h")
	if h >= 2 && h <= 8 {
		return false
	}
	// Remove the 1st digit from the accout number and insert a 0
	// as the 1st digit for check purposes
	b.AccountNumber = "0" + b.AccountNumber[0:len(b.AccountNumber)-1]

	return GeneralChecker{}.IsValid(b, sc, attempt)
}
// Tell if the bank account is valid
func (e Exception9Checker) IsValid(b m.BankAccount, sc m.SortCodeData, attempt int) bool {
	if !e.Handles(b, sc, attempt) {
		panic("Should be exception of type 9")
	}

	sc.Weights = WeightsForException2Or9(b, sc)
	if (GeneralChecker{}.IsValid(b, sc, attempt)) {
		return true
	}

	// Try to replace the sort code
	b.SortCode = "309634"

	return GeneralChecker{}.IsValid(b, e.Weights[b.SortCode], attempt)
}
// Determine if the checker is able to validate the bank account
func (e Exception7Checker) Handles(account m.BankAccount, sc m.SortCodeData, attempt int) bool {
	// If g=9
	g := account.NumberAtPosition("g")

	return sc.IsException(7) && g == 9
}