package config

import (
	"eta/eta_mini_ht_api/common/contants"
	"sync"
)

var (
	smsOnce   sync.Once
	smsConfig *SMSConfig
)

type SMSOpts struct {
	SmsType    string
	JhConfig   JUHE
	EMASConfig EMAS
}
type SMSConfig struct {
	BaseConfig
	opts SMSOpts
}

type JUHE struct {
	Key          string
	ExpireMinute int
	TlpId        string
}

type EMAS struct {
	url          string
	ExpireMinute int
	Template     string
}

func (r *SMSConfig) GetSmsType() string {
	return r.opts.SmsType
}
func (r *SMSConfig) GetJhTlpId() string {
	return r.opts.JhConfig.TlpId
}
func (r *SMSConfig) GetJhExpireMinute() int {
	return r.opts.JhConfig.ExpireMinute
}

func (r *SMSConfig) GetEMASExpireMinute() int {
	return r.opts.EMASConfig.ExpireMinute
}
func (r *SMSConfig) GetEMASTemplate() string {
	return r.opts.EMASConfig.Template
}
func (r *SMSConfig) GetEMASUrl() string {
	return r.opts.EMASConfig.url
}
func (r *SMSConfig) GetKey() string {
	return r.opts.JhConfig.Key
}
func (r *SMSConfig) InitConfig() {
	opts := SMSOpts{
		SmsType: r.GetString("sms_type"),
	}
	switch opts.SmsType {
	case "juhe":
		opts.JhConfig.ExpireMinute = r.GetInt("juhe.expire_minute")
		opts.JhConfig.Key = r.GetString("juhe.key")
		opts.JhConfig.TlpId = r.GetString("juhe.tlp_id")
	case "emas":
		opts.EMASConfig.ExpireMinute = r.GetInt("emas.expire_minute")
		opts.EMASConfig.url = r.GetString("emas.url")
		opts.EMASConfig.Template = r.GetString("emas.template")
	default:
	}
	r.opts = opts
}

func NewSMSConfig() Config {
	if smsConfig == nil {
		smsOnce.Do(func() {
			smsConfig = &SMSConfig{
				BaseConfig: BaseConfig{prefix: contants.SMS},
				opts:       SMSOpts{},
			}
		})
	}
	return smsConfig
}

func init() {
	Register(contants.SMS, NewSMSConfig)
}
