package config

import (
	"github.com/spf13/viper"
)

var (
	PageSize uint = 10
	C        *Config
)

const (
	VERSION           = "0.1.2"
	PAGE_SIZE_VISITOR = 8
	DIR               = "config"
)

type DatabaseConf struct {
	Driver   string
	Server   string
	Port     string
	Db       string
	Username string
	Password string
}

type QiniuConf struct {
	Access string
	Secret string
	Bucket string
	Zone   string
	Domain string
}

type ImapConf struct {
	Server, Email, Password string
}

type BasicConf struct {
	Addr         string
	Daemon       bool
	Upload       string
	NoticeServer bool
	LangDir      string
	DataDir      string
	ResDir       string
}

type Config struct {
	Basic    *BasicConf
	Database *DatabaseConf
	Imap     *ImapConf
	Qiniu    *QiniuConf
	Twong    *DatabaseConf
}

func LoadConf(f string) (*Config, error) {
	v := viper.New()
	v.SetConfigType("toml")
	v.AddConfigPath(".")
	v.SetConfigFile(f)

	err := v.ReadInConfig()
	if err != nil {
		return nil, err
	}

	conf := &Config{}
	err = v.Unmarshal(conf)
	if err != nil {
		return nil, err
	}

	C = conf

	return conf, nil
}

//func GetUserInfo(uid string) map[string]string {
//	var userInfo map[string]string
//	userFile := DIR + "sess_" + uid + ".json"
//	isExist, _ := tools.IsFileExist(userFile)
//	if !isExist {
//		return userInfo
//	}
//	info, err := ioutil.ReadFile(userFile)
//	if err != nil {
//		return userInfo
//	}
//
//	err = json.Unmarshal(info, &userInfo)
//	return userInfo
//}

//func SetUserInfo(uid string, info map[string]string) {
//	userFile := DIR + "sess_" + uid + ".json"
//	isExist, _ := tools.IsFileExist(DIR)
//	if !isExist {
//		os.Mkdir(DIR, os.ModePerm)
//	}
//	file, _ := os.OpenFile(userFile, os.O_RDWR|os.O_CREATE, os.ModePerm)
//	str := "{\r\n"
//	for k, v := range info {
//		str += fmt.Sprintf(`"%s":"%s",`, k, v)
//	}
//	str += fmt.Sprintf(`"session_id":"%s"%s}`, uid, "\r\n")
//	file.WriteString(str)
//}
