package settings

import (
	"log"
	"os"

	"github.com/go-ini/ini"
)

// Settings represents a app settings
type Settings struct {
	IsInit                 bool
	AppName                string
	ServerProtocol         string
	ServerHost             string
	ServerPort             string
	AuthAcceptHeader       string
	AuthPrefixHeader       string
	AuthSecretKey          string
	DatabaseDialect        string
	DatabaseHost           string
	DatabasePort           string
	DatabaseUser           string
	DatabasePasswd         string
	DatabaseLogger         bool
	DatabaseAutomigrate    bool
	DockerSockDir          string
	DockerHostIP           string
	DockerNetworkName      string
	DockerNetworkIP        string
	DockerHiddenContainers []string
	OdooImageName          string
	OdooRootPath           string
	OdooConfigPath         string
	OdooAddonsPath         string
	OdooFilesPath          string
	OdooPortsRange         []int
	OdooCfgName            string
	OdooAdminPasswd        string
	OdooDbContainerName    string
	OdooDbContainerIP      string
	OdooDbContainerPort    string
	OdooDbUser             string
	OdooDbPasswd           string
	GitReposPath           string
	MailerHost             string
	MailerPort             string
	MailerUser             string
	MailerPasswd           string
}

// Ctx is a global context settings instance
var Ctx = Settings{
	IsInit: false,
}

// Init initialize all app configuration
func Init() {
	if Ctx.IsInit {
		return
	}

	cfg, err := ini.Load("data/app.ini")

	if err != nil {
		log.Fatal("Cannot read file")
		os.Exit(1)
	}

	// Read root section
	rootSection := cfg.Section("")
	Ctx.AppName = rootSection.Key("appName").MustString("No name")

	// Read server section
	srvSection := cfg.Section("server")
	Ctx.ServerProtocol = srvSection.Key("protocol").MustString("http")
	Ctx.ServerHost = srvSection.Key("host").MustString("127.0.0.1")
	Ctx.ServerPort = srvSection.Key("port").MustString("8080")

	// Read auth section
	authSection := cfg.Section("auth")
	Ctx.AuthAcceptHeader = authSection.Key("acceptHeader").String()
	Ctx.AuthPrefixHeader = authSection.Key("prefixHeader").String()
	Ctx.AuthSecretKey = authSection.Key("secretKey").String()

	// Read database section
	dbSection := cfg.Section("database")
	Ctx.DatabaseDialect = dbSection.Key("dbDialect").MustString("sqlite")
	Ctx.DatabaseHost = dbSection.Key("dbHost").String()
	Ctx.DatabasePort = dbSection.Key("dbPort").String()
	Ctx.DatabaseUser = dbSection.Key("dbUser").String()
	Ctx.DatabasePasswd = dbSection.Key("dbPasswd").String()
	Ctx.DatabaseLogger = dbSection.Key("dbLogger").MustBool(true)
	Ctx.DatabaseAutomigrate = dbSection.Key("dbAutomigrate").MustBool(true)

	// Read docker section
	dockerSection := cfg.Section("docker")
	Ctx.DockerSockDir = dockerSection.Key("sockDir").MustString("/var/run/docker.sock")
	Ctx.DockerHostIP = dockerSection.Key("hostIP").MustString("127.0.0.1")
	Ctx.DockerNetworkName = dockerSection.Key("networkName").MustString("bridge")
	Ctx.DockerNetworkIP = dockerSection.Key("networkIP").MustString("172.10.0.1")
	Ctx.DockerHiddenContainers = dockerSection.Key("hiddenContainers").Strings(",")

	// Read odoo section
	odooSection := cfg.Section("odoo")
	Ctx.OdooImageName = odooSection.Key("imageName").String()
	Ctx.OdooRootPath = odooSection.Key("rootPath").String()
	Ctx.OdooConfigPath = odooSection.Key("configPath").String()
	Ctx.OdooAddonsPath = odooSection.Key("addonsPath").String()
	Ctx.OdooFilesPath = odooSection.Key("filesPath").String()
	Ctx.OdooPortsRange = odooSection.Key("portsRange").Ints(",")
	Ctx.OdooCfgName = odooSection.Key("cfgName").String()
	Ctx.OdooAdminPasswd = odooSection.Key("adminPasswd").String()
	Ctx.OdooDbContainerName = odooSection.Key("dbContainerName").String()
	Ctx.OdooDbContainerIP = odooSection.Key("dbContainerIP").String()
	Ctx.OdooDbContainerPort = odooSection.Key("dbContainerPort").String()
	Ctx.OdooDbUser = odooSection.Key("dbUser").String()
	Ctx.OdooDbPasswd = odooSection.Key("dbPasswd").String()

	// Read git section
	gitSection := cfg.Section("git")
	Ctx.GitReposPath = gitSection.Key("reposPath").String()

	// Read mailer section
	mailerSection := cfg.Section("mailer")
	Ctx.MailerHost = mailerSection.Key("emailHost").String()
	Ctx.MailerPort = mailerSection.Key("emailPort").String()
	Ctx.MailerUser = mailerSection.Key("emailHostUser").String()
	Ctx.MailerPasswd = mailerSection.Key("emailHostPasswd").String()

	Ctx.IsInit = true
}
