package config

import (
	"io/ioutil"

	yaml "gopkg.in/yaml.v2"
)

const (
	LOG_LEVEL_OFF = iota
	LOG_LEVEL_INFO
	LOG_LEVEL_DEBUG
)

type ConfigT struct {

	// Domain

	Domain string

	// Logging

	LogLevel int `yaml:"log_level"`

	// Admin credentials

	Admin struct {
		Username string
		Password string
	}

	// Database

	Orm struct {
		Connection  string
		Options     string
		Reset       bool
		AutoMigrate bool
		Regenerate  bool
	}

	// LDAP

	Ldap struct {
		Host         string
		Port         string
		BindPassword string `yaml:"bind_password"`
		BindUser     string `yaml:"bind_user"`
		RootDN       string `yaml:"root_dn"`

		OuPeople        string `yaml:"ou_people"`
		OuTeacher       string `yaml:"ou_teacher"`
		OuGroup         string `yaml:"ou_group"`
		OuTeachersGroup string `yaml:"ou_teachers_group"`
	}

	// LimeSurvey Remote Control URL

	Limesurvey struct {
		Url      string
		Username string
		Password string
	}

	// Cloud

	Cloud struct {
		Url      string
		Username string
		Password string
	}
}

var Config *ConfigT

func init() {
	Config = new(ConfigT)
}

// ReadFile reads the config file placed at the given path.
func ReadFile(path string, config *ConfigT) error {
	cf, err := ioutil.ReadFile(path)
	if err != nil {
		return err
	}
	if err := yaml.Unmarshal(cf, config); err != nil {
		return err
	}
	return nil
}

// Read reads the config data from the given slice.
func Read(data []byte, config *ConfigT) error {
	if err := yaml.Unmarshal(data, config); err != nil {
		return err
	}
	return nil
}
