package runtime

import "github.com/sirupsen/logrus"

// Config is an internal configuration modifier. It can be instantiated on a
// command call and can be changed on the fly to help make decisions further
// down in the internals, e.g. whether or not to clone the recipe locally or
// not.
type Config struct {
	EnsureRecipeExists bool // ensure that the recipe is cloned locally
	EnsureRecipeLatest bool // ensure the local recipe has latest changes
}

// Option modified a Config. The convetion for passing Options to functions is
// so far, only used in internal/validate.go. A Config is then constructed and
// passed down further into the code. This may change in the future but this is
// at least the abstraction so far.
type Option func(c *Config)

// New instantiates a new Config.
func New(opts ...Option) *Config {
	conf := &Config{
		EnsureRecipeExists: true,
	}

	for _, optFunc := range opts {
		optFunc(conf)
	}

	return conf
}

// WithEnsureRecipeExists determines whether or not we should be cloning the
// local recipe or not. This can be useful for being more adaptable to offline
// scenarios.
func WithEnsureRecipeExists(ensureRecipeExists bool) Option {
	return func(c *Config) {
		if ensureRecipeExists {
			logrus.Debugf("runtime config: EnsureRecipeExists = %v, ensuring recipes are cloned", ensureRecipeExists)
		} else {
			logrus.Debugf("runtime config: EnsureRecipeExists = %v, not cloning recipes", ensureRecipeExists)
		}

		c.EnsureRecipeExists = ensureRecipeExists
	}
}

// WithEnsureRecipeLatest determines whether we should update the local recipes
// remotely via Git. This can be useful when e.g. ensuring we have the latest
// changes before making new ones.
func WithEnsureRecipeLatest(ensureRecipeLatest bool) Option {
	return func(c *Config) {
		if ensureRecipeLatest {
			logrus.Debugf("runtime config: EnsureRecipeLatest = %v, ensuring recipes have latest changes", ensureRecipeLatest)
		} else {
			logrus.Debugf("runtime config: EnsureRecipeLatest = %v, leaving recipes alone", ensureRecipeLatest)
		}

		c.EnsureRecipeLatest = ensureRecipeLatest
	}
}