package svc

import (
	"applet/app/db"
	"applet/app/db/model"
	"applet/app/db/offical"
	"applet/app/md"
	"applet/app/utils"
	"applet/app/utils/logx"
	"github.com/gin-gonic/gin"
	"github.com/tidwall/gjson"
	"strings"
)

// 获取指定类型的域名：admin、wap、api
func GetWebSiteDomainInfo(c *gin.Context, domainType string) string {
	if domainType == "" {
		domainType = "wap"
	}

	domainSetting := SysCfgGet(c, "domain_setting")

	domainTypePath := domainType + ".type"
	domainSslPath := domainType + ".isOpenHttps"
	domainPath := domainType + ".domain"

	domainTypeValue := gjson.Get(domainSetting, domainTypePath).String()
	domainSslValue := gjson.Get(domainSetting, domainSslPath).String()
	domain := gjson.Get(domainSetting, domainPath).String()

	scheme := "http://"
	if domainSslValue == "1" {
		scheme = "https://"
	}

	// 有自定义域名 返回自定义的
	if domainTypeValue == "own" && domain != "" {
		return scheme + domain
	}
	// 否则返回官方的
	official, err := db.GetOfficialDomainInfoByType(db.Db, c.GetString("mid"), domainType)
	if err != nil {
		_ = logx.Errorf("Get Official Domain Fail! %s", err)
		return ""
	}
	if strings.Contains(official, "http") {
		return official
	}
	return scheme + official
}
func GetWebSiteDomainInfoToAgent(c *gin.Context, domainType string) string {
	if domainType == "" {
		domainType = "wap"
	}

	domainSetting := SysCfgGet(c, "domain_setting")

	domainTypePath := domainType + ".type"
	domainSslPath := domainType + ".isOpenHttps"
	domainPath := domainType + ".domain"

	domainTypeValue := gjson.Get(domainSetting, domainTypePath).String()
	domainSslValue := gjson.Get(domainSetting, domainSslPath).String()
	domain := gjson.Get(domainSetting, domainPath).String()

	scheme := "http://"
	if domainSslValue == "1" {
		scheme = "https://"
	}

	// 有自定义域名 返回自定义的
	if domainTypeValue == "own" && domain != "" {
		return scheme + domain
	}
	// 否则返回官方的
	puid := AppUserListPuid(c)
	var official = ""
	var err error
	if puid != "" && puid != "0" {
		official, err = db.GetOfficialDomainInfoByTypeToAgent(db.Db, c.GetString("mid"), puid, domainType)
		if err != nil {
			_ = logx.Errorf("Get Official Domain Fail! %s", err)
			return ""
		}
	} else {
		official, err = db.GetOfficialDomainInfoByType(db.Db, c.GetString("mid"), domainType)
		if err != nil {
			_ = logx.Errorf("Get Official Domain Fail! %s", err)
			return ""
		}
	}
	if strings.Contains(official, "http") {
		return official
	}
	return scheme + official
}
func GetWebSiteDomainInfoOfficial(c *gin.Context, domainType string) string {
	if domainType == "" {
		domainType = "wap"
	}

	domainSetting := SysCfgGet(c, "domain_setting")

	domainSslPath := domainType + ".isOpenHttps"

	domainSslValue := gjson.Get(domainSetting, domainSslPath).String()

	scheme := "http://"
	if domainSslValue == "1" {
		scheme = "https://"
	}

	// 有自定义域名 返回自定义的
	// 否则返回官方的
	official, err := db.GetOfficialDomainInfoByType(db.Db, c.GetString("mid"), domainType)
	if err != nil {
		_ = logx.Errorf("Get Official Domain Fail! %s", err)
		return ""
	}
	if strings.Contains(official, "http") {
		return official
	}
	return scheme + official
}

// 获取指定类型的域名对应的masterId：admin、wap、api
func GetWebSiteDomainMasterId(domainType string, host string) string {
	obj := new(model.UserAppDomain)
	has, err := db.Db.Where("domain=? and type=?", host, domainType).Get(obj)
	if err != nil || !has {
		return ""
	}
	return utils.AnyToString(obj.Uuid)
}

func GetWebSiteAppSmsPlatform(mid string) string {
	obj := new(model.UserAppList)
	has, err := db.Db.Where("uuid=? ", mid).Asc("id").Get(obj)
	if err != nil || !has {
		return ""
	}
	return utils.AnyToString(obj.SmsPlatform)
}

// 获取指定类型的域名：admin、wap、api
func GetWebSiteLiveBroadcastDomainInfo(c *gin.Context, domainType, mid string) string {
	if domainType == "" {
		domainType = "wap"
	}

	domainSetting := SysCfgGet(c, "domain_setting")
	domainSslPath := domainType + ".isOpenHttps"
	domainSslValue := gjson.Get(domainSetting, domainSslPath).String()
	//masterid.izhyin.cn
	domain := mid + ".izhim.net"

	scheme := "http://"
	if domainSslValue == "1" {
		scheme = "https://"
	}
	if c.GetHeader("platform") == md.PLATFORM_WX_APPLET { //小程序需要https
		scheme = "https://"
	}
	return scheme + domain
}
func GetWebSiteDomainInfoSecond(c *gin.Context, domainType string) string {
	if domainType == "" {
		domainType = "wap"
	}

	domainSetting := SysCfgGet(c, "domain_setting")
	domainSslPath := domainType + ".isOpenHttps"
	domainSslValue := gjson.Get(domainSetting, domainSslPath).String()
	domain := c.GetString("mid") + ".izhim.net"
	domainTypePath := domainType + ".type"
	domainTypeValue := gjson.Get(domainSetting, domainTypePath).String()
	scheme := "http://"
	if domainSslValue == "1" {
		scheme = "https://"
	}
	if c.GetHeader("platform") == md.PLATFORM_WX_APPLET { //小程序需要https
		scheme = "https://"
	}
	// 有自定义域名 返回自定义的
	if domainTypeValue == "own" {
		domainPath := domainType + ".domain"
		domain = gjson.Get(domainSetting, domainPath).String()
	}
	return scheme + domain
}
func AppUserListPuid(c *gin.Context) string {
	appList := offical.GetUserAppList(c.GetString("mid"))
	uid := "0"
	if appList != nil && appList.Puid > 0 {
		uid = utils.IntToStr(appList.Puid)
	}
	return uid
}
func AppUserListPuidWithDb(dbName string) string {
	appList := offical.GetUserAppList(dbName)
	uid := "0"
	if appList != nil && appList.Puid > 0 {
		uid = utils.IntToStr(appList.Puid)
	}
	return uid
}
