package config

import (
	"gopkg.in/yaml.v2"
	"io/ioutil"
	//"os"
	//"io/ioutil"

	//"encoding/json"
	l4g "proxy/logger"//"github.com/sirupsen/logrus"
)

type LogConf struct {
	//Output string `yaml:"output"`// file; console
	Level string `yaml:"level"`// debug; info; warn; error
	//Path string `yaml:"path"`
	//MaxAge int `yaml:"maxAge"`// per Hour
	//RotationTime int `yaml:"rotationTime"`// per Hour
}

type MysqlDb struct {
	Addr string `yaml:"addr"`
	Username string `yaml:"username"`
	Password string `yaml:"password"`
}

type RedisDb struct {
	NetType string `yaml:"netType"`
	Addr string `yaml:"addr"`
	Password string `yaml:"password,omitempty"`
	SentinelAddrs []string `yaml:"sentinelAddrs,omitempty"`
}

type TelnetServer struct {
	Addr string `yaml:"addr"`
}

type RestConf struct {
	Enabled bool `yaml:"enabled"`
	HttpAddr string `yaml:"httpAddr"`
	//EmsAddr string `yaml:"emsAddr,omitempty"`
	LocRzIp string `yaml:"locRzIp"`
	LocRzPort int `yaml:"locRzPort"`
	OcsRzIp string `yaml:"ocsRzIp"`
	OcsRzPort int `yaml:"ocsRzPort"`
	//EnableNotification bool `yaml:"enableNotification"`
}

type CanalServer struct {
	Enabled bool `yaml:"enabled"`

	Addr string `yaml:"addr"`
	Username string `yaml:"username"`
	Password string `yaml:"password"`

	Reinit bool `yaml:"reinit"`
	FlushBeforeInit bool `yaml:"flushBeforeInit"`
	//Standalone bool `yaml:"standalone"`
}

type CronCfg struct {
	Enabled bool `yaml:"enabled"`
	ClrExp string `yaml:"clrExp,omitempty"`
	NtfSms string `yaml:"ntfSms,omitempty"`
}

type ProvisionCfg struct {
	Enabled bool `yaml:"enabled"`
	Interval int `yaml:"interval"`
	ReadTimout int `yaml:"readTimout"`

	EmsIp string `yaml:"emsIp"`
	EmsPort int `yaml:"emsPort"`
	Username string `yaml:"username"`
	Password string `yaml:"password"`
	ConnectHss string `yaml:"connectHss,omitempty"`
	ConnectAuc string `yaml:"connectAuc,omitempty"`
	ConnectVms string `yaml:"connectVms,omitempty"`
	DisconnectHss string `yaml:"disconnectHss,omitempty"`
	DisconnectAuc string `yaml:"disconnectAuc,omitempty"`
	DisconnectVms string `yaml:"disconnectVms,omitempty"`

	SsEntryIdInCrm int `yaml:"ssEntryIdInCrm,omitempty"`
}

type BundleUsageNotify struct {
	//Enabled bool `yaml:"enabled"`
	Voice75Percent bool `yaml:"voice75Percent"`
	Data75Percent bool `yaml:"data75Percent"`
	Sms75Percent bool `yaml:"sms75Percent"`
}

type DbConfig struct {
	Log          *LogConf     `yaml:"log,omitempty"`
	MysqlDb      MysqlDb      `yaml:"mysqlDb"`
	RedisDb      RedisDb      `yaml:"redisDb"`
	TelnetServer TelnetServer `yaml:"telnetServer"`
	Rest         RestConf     `json:"rest"`
	CanalServer  CanalServer  `yaml:"canalServer"`
	CronCfg      CronCfg      `yaml:"cronCfg"`
	Provision  	 ProvisionCfg `yaml:"provision"`
	BundleUsageNotify BundleUsageNotify `yaml:"bundleUsageNotify"`
}

var Config = DbConfig{ /*mysql: "192.168.1.151:3306", username: "root", password: "rootaa"*/}

func ReadConfig() error {
	if content, err := ioutil.ReadFile("/usr/local/etc/restproxy/config.yaml"); err != nil {
		return err
	} else {
		if yamlErr := yaml.Unmarshal(content, &Config); yamlErr != nil {
			return yamlErr
		}
	}

	return nil
}

func SavePcfCfg() {
	if content, err := yaml.Marshal(&Config); err != nil {
		l4g.CfgLog.Errorf("Marshal failed error %s", err.Error())
		return
	} else if fileErr := ioutil.WriteFile("/usr/local/etc/restproxy/config.yaml", content, 0644); fileErr != nil {
		l4g.CfgLog.Errorf("WriteFile failed error %s", fileErr.Error())
		return
	}
}

func SetBundleUsageNtf(serviceType int, bEnable bool) {
	switch serviceType {
	case 1:
		Config.BundleUsageNotify.Voice75Percent = bEnable
	case 2:
		Config.BundleUsageNotify.Data75Percent = bEnable
	case 3:
		Config.BundleUsageNotify.Sms75Percent = bEnable
	default:
	}
	SavePcfCfg()
}

