package conf

import (
	"errors"
	"fmt"
	"github.com/cihub/seelog"
	"gopkg.in/yaml.v2"
	"io/ioutil"
	//"git.loafle.net/overflow/overflow_probe/discovery/communicate"
	//"git.loafle.net/overflow/overflow_probe/overflow.collector_backup2/src/git.loafle.net/overflow/overflow_probe/discovery/communicate"
)

type Config struct {
	Central struct {
		Address string
		Port    int
	}
	Log_Path string
}

var c Config

func LoadConfig(f string) error {
	if len(f) <= 0 {
		return errors.New("conf file path is nil")
	}
	c = Config{}
	data, err := ioutil.ReadFile(f)
	if err != nil {
		return err
	}
	err = yaml.Unmarshal([]byte(data), &c)
	if err != nil {
		return err
	}

	err = loadLogConfig(c.Log_Path)
	if err != nil {
		fmt.Println("Log Config Load Error : ", err)
		return err
	}
	fmt.Println(c)

	//communicate.SetRootURL(c.Central.Address)
	return nil
}

func loadLogConfig(path string) error {

	l, err := seelog.LoggerFromConfigAsFile(path)
	if err != nil {
		fmt.Println("Error : ", err)
		return err
	}
	seelog.ReplaceLogger(l)
	return err
}
