package config

import (
	"fmt"
	"os"
	"time"

	"github.com/BurntSushi/toml"
)

var (
	// Conf crocodile conf
	Conf *conf
)

// Init Config
func Init(conf string) {
	_, err := toml.DecodeFile(conf, &Conf)
	if err != nil {
		fmt.Printf("Err %v", err)
		os.Exit(1)
	}
}

type conf struct {
	Preset preset
	Es     es
	Mysql  mysql
	Mail   mail
	Log    log
}

type preset struct {
	Bidday   int64
	Bidfield int
	Accuracy float64
}

type es struct {
	Addr     string
	Size     int
	Username string
	Password string
	Bidding  string
	Project  string
	Qyxy     string
	Buyer    string
}

type mysql struct {
	Addr     string
	Dbname   string
	Size     int
	User     string
	Password string
}

type mail struct {
	Send bool
	To   string
	Api  string
}

// Log Config
type log struct {
	LogPath    string
	MaxSize    int
	Compress   bool
	MaxAge     int
	MaxBackups int
	LogLevel   string
	Format     string
}

type mgo struct {
	Addr     string
	Dbname   string
	Size     int
	User     string
	Password string
}

type duration struct {
	time.Duration
}

// UnmarshalText parse 10s to time.Time
func (d *duration) UnmarshalText(text []byte) error {
	var err error
	d.Duration, err = time.ParseDuration(string(text))
	return err
}
