package config

import (
	"flag"
	"fmt"
	"os"
	"strings"

	"ems.agt/lib/global"
	"ems.agt/lib/log"
	"ems.agt/sshsvc/logmml"

	"gopkg.in/yaml.v3"
)

// Yaml struct of config
type YamlConfig struct {
	Logger struct {
		File     string `yaml:"file"`
		Level    string `yaml:"level"`
		Duration int    `yaml:"duration"`
		Count    int    `yaml:"count"`
	} `yaml:"logger"`

	Logmml struct {
		File     string `yaml:"file"`
		Duration int    `yaml:"duration"`
		Count    int    `yaml:"count"`
		Level    string `yaml:"level"`
	} `yaml:"logmml"`

	Sshd struct {
		ListenAddr string `yaml:"listenAddr"`
		ListenPort uint16 `yaml:"listenPort"`
		PrivateKey string `yaml:"privateKey"`
		MaxConnNum uint8  `yaml:"maxConnNum"`
		Timeout    uint16 `yaml:"timeout"`
		Session    string `yaml:"session"`
		MmlHome    string `yaml:"mmlHome"`
	} `yaml:"sshd"`

	Database struct {
		Type     string `yaml:"type"`
		User     string `yaml:"user"`
		Password string `yaml:"password"`
		Host     string `yaml:"host"`
		Port     string `yaml:"port"`
		Name     string `yaml:"name"`
		Backup   string `yaml:"backup"`
	} `yaml:"database"`

	OMC struct {
		HttpUri   string `yaml:"httpUri"`
		UserCrypt string `yaml:"userCrypt"`
	} `yaml:"omc"`
}

var yamlConfig YamlConfig

func ReadConfig(configFile string) {
	yamlFile, err := os.ReadFile(configFile)
	if err != nil {
		fmt.Println("Read yaml config file error:", err)
		os.Exit(2)
	}

	err = yaml.Unmarshal(yamlFile, &yamlConfig)
	if err != nil {
		fmt.Println("Unmarshal error:", err)
		os.Exit(3)
	}
}

func GetYamlConfig() *YamlConfig {
	return &yamlConfig
}

func GetLogLevel() log.LogLevel {
	var logLevel log.LogLevel
	switch strings.ToLower(yamlConfig.Logger.Level) {
	case "trace":
		logLevel = log.LOG_TRACE
	case "info":
		logLevel = log.LOG_INFO
	case "debug":
		logLevel = log.LOG_DEBUG
	case "warn":
		logLevel = log.LOG_WARN
	case "error":
		logLevel = log.LOG_ERROR
	case "fatal":
		logLevel = log.LOG_FATAL
	case "off":
		logLevel = log.LOG_OFF
	default:
		logLevel = log.LOG_DEBUG
	}
	return logLevel
}

func GetLogMmlLevel() logmml.LogLevel {
	var logLevel logmml.LogLevel
	switch strings.ToLower(yamlConfig.Logmml.Level) {
	case "cmd", "command":
		logLevel = logmml.LOG_CMD
	case "ret", "result":
		logLevel = logmml.LOG_RET
	default:
		logLevel = logmml.LOG_CMD
	}
	return logLevel
}

func GetDefaultUserAgent() string {
	return "OMC-sshsvc/" + global.Version
}

const DefaultConfigFile = "./etc/sshsvc.yaml"

func init() {
	cfile := flag.String("c", DefaultConfigFile, "config file")
	pv := flag.Bool("v", false, "print version")
	ph := flag.Bool("h", false, "print help")

	flag.Parse()
	if *pv {
		fmt.Printf("OMC sshsvc version: %s\n%s\n%s\n\n", global.Version, global.BuildTime, global.GoVer)
		os.Exit(0)
	}
	if *ph {
		flag.Usage()
		os.Exit(0)
	}

	ReadConfig(*cfile)
}
