package sms

import (
	"applet/app/db"
	"applet/app/lib/zhimeng"
	"applet/app/svc"
	"applet/app/utils/cache"
	"applet/app/utils/logx"
	"code.fnuoos.com/go_rely_warehouse/zyos_go_third_party_api.git/sms"
	"errors"
	"fmt"
	"github.com/tidwall/gjson"

	"github.com/gin-gonic/gin"
)

// NewZhimengSMS is 智盟的短信服务
func NewZhimengSMS(c *gin.Context) *zhimeng.SDK {
	sms := new(zhimeng.SDK)
	key := svc.SysCfgGet(c, "third_zm_sms_key")
	secret := svc.SysCfgGet(c, "third_zm_sms_secret")
	if key == "" || secret == "" {
		_ = logx.Warn("短信服务配置错误")
	}
	sms.Init("send_msg", key, secret)
	return sms
}
func GetSmsPlatform(c *gin.Context) string {
	var smsPlatform = "ljioe"
	key := fmt.Sprintf("%s:sms_platform", c.GetString("mid"))
	smsPlatformTmp, _ := cache.GetString(key)
	if smsPlatformTmp == "" {
		smsPlatformTmp = svc.GetWebSiteAppSmsPlatform(c.GetString("mid"))
		if smsPlatformTmp != "" {
			cache.SetEx(key, smsPlatformTmp, 300)
		}
	}
	if smsPlatformTmp != "" {
		smsPlatform = smsPlatformTmp
	}
	return smsPlatform
}
func GetTplId(c *gin.Context, zone, types string) string {
	// 校验短信验证码
	tplId := ""
	if zone != "86" {
		tplId = svc.SysCfgGet(c, "mob_sms_sdk_international_template_id")
	} else {
		tplId = svc.SysCfgGet(c, "mob_sms_sdk_template_id")
	}
	if c.GetString("app_type") == "o2o" {
		tplId = db.SysCfgGet(c, "biz_mob_sms_sdk_template_id")
	}
	normal := gjson.Get(tplId, types).String()
	if normal == "" {
		normal = gjson.Get(tplId, "normal").String()
	}
	return normal
}
func GetSmsConfig(c *gin.Context, zone string, postData map[string]interface{}) error {
	m := db.SysCfgGet(c, "third_app_push_set")
	if c.GetString("app_type") == "o2o" {
		m = db.SysCfgGet(c, "biz_third_app_push_set")
	}
	key := gjson.Get(m, "mobAppKey").String()
	postData["is_mob"] = "1"
	postData["type"] = "mob"
	postData["sms_type"] = "putong"
	smsPlatform := GetSmsPlatform(c)
	if smsPlatform == "ljioe" {
		postData["is_mob"] = "0"
		postData["type"] = ""
	}
	token := c.GetHeader("Authorization")
	if zone == "" && token != "" {
		arkToken, _ := db.UserProfileFindByArkToken(svc.MasterDb(c), token)
		if arkToken != nil && arkToken.Uid > 0 {
			user, _ := db.UserFindByID(svc.MasterDb(c), arkToken.Uid)
			if user != nil && user.Uid > 0 {
				zone = user.Zone
			}
		}
	}
	if zone == "" {
		zone = "86"
	}
	if zone != "86" { //国际短信
		postData["is_sales"] = "2"
		postData["sms_type"] = "international"
	}
	postData["templateCode"] = GetTplId(c, zone, postData["templateCode"].(string))
	postData["zone"] = zone
	if key != "" {
		postData["smsmsg_key"] = key
	}
	if c.GetString("sms_type") == "1" { //新的
		postData["uid"] = c.GetString("mid")
		err := sms.SmsSend(db.Db, postData)
		if err != nil {
			return err
		}
		return nil
	}
	fmt.Println("===短信", postData, c.ClientIP())
	sdk, err := NewZhimengSMS(c).SelectFunction("msg_doing").WithSMSArgs(postData).Result()
	if err != nil {
		msg := gjson.Get(err.Error(), "msg").String()
		if msg == "" {
			msg = err.Error()
		}
		fmt.Println("===短信", err)
		errs := errors.New(msg)
		return errs
	}
	rmap := sdk.ToInterface().(map[string]interface{})
	fmt.Println("===短信", rmap)

	if rmap["status"] == "" {
		return err
	}
	return nil
}
