package auth

import (
	"errors"
	"html/template"
	"log"

	"code.secondbit.org/events.hg"
)

var (
	// ErrInvalidLoginURI is returned when a Context is instantiated with a Config object that specifies a LoginURI that can't be parsed as a URL.
	ErrInvalidLoginURI = errors.New("invalid login URI")
	// ErrConfigNotInitialized is returned when a Context is instantiated with a Config object that hasn't had its Init function called.
	ErrConfigNotInitialized = errors.New("config not initialized")

	// Version is used to keep track of what version of the build this is
	Version string
)

// Config holds the configuration values necessary to run a server. A Config
// instance is the only way to instantiate a Context variable.
type Config struct {
	ClientStore     clientStore
	AuthCodeStore   authorizationCodeStore
	ProfileStore    profileStore
	TokenStore      tokenStore
	SessionStore    sessionStore
	ScopeStore      scopeStore
	EventsPublisher events.Publisher
	Template        *template.Template
	LoginURI        string
	JWTPrivateKey   []byte
	iterations      int
	secureCookie    bool
}

// Init is a function that preps the Config object to be used for Context creation, setting variables
// that are determined at the beginning of program execution.
func (c *Config) Init() error {
	scheme, ok := passphraseSchemes[CurPassphraseScheme]
	if !ok {
		return ErrInvalidPassphraseScheme
	}
	var err error
	c.iterations, err = scheme.calculateIterations()
	if err != nil {
		return err
	}
	log.Printf("Generating passphrases with %d iterations...\n", c.iterations)
	return nil
}
