package sms

import (
	"eta/eta_mini_ht_api/common/component/config"
	"github.com/beego/beego/v2/core/logs"
)

var (
	smsClient SMSClient
)

const (
	Juhe = "juhe"
	Emas = "emas"
)

type SMSClient interface {
	InitSender(config *config.SMSConfig)
	GetExpireMinute() int
	SendSms(mobile string, code string, smId int) (rs string, err error)
}

var clients = make(map[string]Instance)

type Instance func() SMSClient

func getClient(config *config.SMSConfig) (client SMSClient) {
	name := config.GetSmsType()
	instanceFunc, ok := clients[name]
	if !ok {
		logs.Error("不支持当前的短信平台", name)
		return nil
	}
	client = instanceFunc()
	client.InitSender(config)
	return client
}
func GetInstance() SMSClient {
	return smsClient
}
func InitSmsSender(config *config.SMSConfig) {
	smsClient = getClient(config)
}
func Register(name string, adapter Instance) {
	if adapter == nil {
		panic("不支持当前的短信平台")
	}
	if _, ok := clients[name]; ok {
		panic("当前短信平台类型已注册:" + name)
	}
	clients[name] = adapter
}
