package randomvalues

import (
	"math/rand"
	"sync"
	"time"
)

type NonCryptoGenerator struct {
	characters string
	*rand.Rand
	*sync.Mutex
}

// Only set seed when you need a non-random value
func NewNonCryptoGenerator(allowedchars string, seed int64) NonCryptoGenerator {
	if len(allowedchars) == 0 {
		allowedchars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
	}
	instance := NonCryptoGenerator{
		characters: allowedchars,
	}

	if seed != 0 {
		instance.Rand = rand.New(rand.NewSource(seed))
	}else{
		instance.Rand = rand.New(rand.NewSource(time.Now().UnixMilli()))
	}
	instance.Mutex = &sync.Mutex{}

	return instance
}

func (g *NonCryptoGenerator) GetString(length int) (string) {
	g.Lock()
	defer g.Unlock()
	result := make([]byte, length)
	for i := 0; i < length; i++ {
		num := g.Intn(len(g.characters))
		result[i] = g.characters[num]
	}

	return string(result)
}