package config

import (
	"fmt"
	"local/args"
	"os"
	"strings"
)

var (
	Port      string
	Store     string
	StoreAddr string
	StoreUser string
	StorePass string
	SecretA   string
	SecretB   string
)

func init() {
	Refresh()
}

func Refresh() {
	if strings.Contains(fmt.Sprint(os.Args), "-test") {
		return
	}
	defer func() {
		if err := recover(); err != nil {
			panic(err)
		}
	}()

	as := args.NewArgSet()
	as.Append(args.STRING, "port", "port to listen on", "23456")
	as.Append(args.STRING, "secretA", "secret A", "secret")
	as.Append(args.STRING, "secretB", "secret B", "secret")
	as.Append(args.STRING, "store", "type of DB", "map")
	as.Append(args.STRING, "storeAddr", "addr of DB", "/tmp/oauth2server.db")
	as.Append(args.STRING, "storeUser", "user of DB", "")
	as.Append(args.STRING, "storePass", "pass of DB", "")
	if err := as.Parse(); err != nil {
		panic(err)
	}

	Port = ":" + strings.TrimPrefix(as.Get("port").GetString(), ":")
	Store = as.Get("store").GetString()
	StoreAddr = as.Get("storeaddr").GetString()
	StoreUser = as.Get("storeuser").GetString()
	StorePass = as.Get("storepass").GetString()
	SecretA = as.Get("secreta").GetString()
	SecretB = as.Get("secretb").GetString()
}
