package feature

import (
	"hash/crc32"
	"log"
)

type Flag struct {
	ID    string
	Limit int
}

func (f Flag) calcOffset() int {
	return calcValue(f.ID)
}

func (f Flag) Permit(candidate string) bool {
	if f.Limit >= 100 {
		return true
	}
	if f.Limit <= 0 {
		return false
	}
	return (calcValue(candidate)+f.calcOffset())%100 <= f.Limit
}

type FlagSet struct {
	flags map[string]bool
}

func (set FlagSet) Check(flag string) bool {
	pass, ok := set.flags[flag]
	if !ok {
		log.Println("Checked for flag that wasn't in response:", flag)
		pass = false
	}
	return pass
}

func calcValue(in string) int {
	return int(crc32.ChecksumIEEE([]byte(in)) % 100)
}

/*

Client has one function:

1. func (c Client) Load(in string) FlagSet -- loads the flagset with "in" as the parameter to gate on

// TODO Have the client cache FlagSets in memory by the parameter being gated on; we can reuse FlagSets for a minute or two

*/
