package security

import (
	"sync"

	"github.com/fiskerinc/cloud-services/pkg/utils/envtool"
)

var envOnce sync.Once
var master_key = []byte{}
var master_auth = []byte{}
var master_nouce = []byte{}

type Encrypt struct{}

func (enc *Encrypt) GetEncryptor() (IEncryptor, error) {
	envOnce.Do(func() {
		master_key = []byte(envtool.GetEnv("MASTER_KEY", "REPLACE_ME_REPLACE_ME_REPLACE_ME"))
		master_auth = []byte(envtool.GetEnv("MASTER_KEY_AUTH", "REPLACE_ME_REPLACE_ME"))
		master_nouce = []byte(envtool.GetEnv("MASTER_KEY_NOUNCE", "_REPLACE_ME_"))
	})
	key, auth, nonce := enc.getMasterKey()

	encryptor, _, err := NewEncryptor([]byte(key), []byte(auth), []byte(nonce))
	return encryptor, err
}

func (enc *Encrypt) getMasterKey() ([]byte, []byte, []byte) {
	return master_key, master_auth, master_nouce
}
