package globals

import (
	"git.rosy.net.cn/baseapi"
	"github.com/astaxie/beego/core/logs"
	"github.com/astaxie/beego/server/web"
	_ "github.com/go-sql-driver/mysql" // import your used driver
	"go.uber.org/zap"
)

var (
	ReallyCallPlatformAPI bool
	ReallySendWeixinMsg   bool

	SugarLogger *zap.SugaredLogger

	AliKey    string
	AliSecret string

	EnableStoreWrite     bool
	EnableJdStoreWrite   bool
	EnableEbaiStoreWrite bool
	EnableElmStoreWrite  bool
	EnableMtwmStoreWrite bool
	EnableWscStoreWrite  bool
	EnablePendingChange  bool
	EnableYbStoreWrite   bool
	EnableJdShopWrite    bool //京东商城
	EnableDdStoreWrite   bool

	QiniuBucket       string
	WeimobCallbackURL string
	WeimobStateSecret string

	BackstageHost   string
	WxBackstageHost string

	GetWeixinTokenURL  string
	GetWeixin2TokenURL string
	GetWeixinTokenKey  string
	GetYLYTokenURL     string
	GetPushTokenURL    string
	GetWeimobTokenURL  string

	StoreName       string
	StoreNameMtwm   string
	StoreNameTiktok string
	StoreNameEbai   string
	StoreNameEbai2  string
	StoreNameTao    string

	SMSSignName                  string
	SMSMobileVerifyTemplate      string
	SMSNewOrderTemplate          string
	SMSNewOrderTemplateQ         string
	SMSOrderCanceledTemplate     string
	SMSNewUserOrderTemplate      string
	SMSJxOrderDelivering         string
	SMSPickOrderTemplate         string
	SMSBrandBalanceTemplate      string
	SMSVoicePickOrderTemplate    string
	SMSVoiceBrandBalanceTemplate string

	EnableWXAuth2  bool
	DisableWXAuth1 bool

	WxpayNotifyURL     string
	TLPayNotifyURL     string
	FnNotifyURL        string
	TictokpayNotifyURL string

	JdOrgCode   string
	Jd2OrgCode  string
	Jd3OrgCode  string
	JdcsOrgCode string
	JdydOrgCode string
	JdLoginName string

	MtwmCode  string
	Mtwm2Code string

	IsStoreSkuAct bool
	IsAddEvent    bool

	OutputDebugMsgLevel int
	TiktokShopAppSecret string
)

func init() {
	logs.SetLogFuncCallDepth(3)
	web.BConfig.Log.AccessLogs = true

	logger, _ := zap.NewDevelopment()
	SugarLogger = logger.Sugar()
	baseapi.Init(SugarLogger)
	Init() // 这里必须要自动调用，不然直接引用全局变量的模块得不到正确的配置值
}

func Init() {
	if false { //!IsProductEnv() {
		OutputDebugMsgLevel = 1
	}

	SugarLogger.Infof("globals RunMode =%s", web.BConfig.RunMode)
	ReallyCallPlatformAPI = (web.BConfig.RunMode != "dev" && web.BConfig.RunMode != "test")
	//ReallyCallPlatformAPI = true //本地测试用-ysq
	ReallySendWeixinMsg = ReallyCallPlatformAPI && IsProductEnv()

	AliKey = web.AppConfig.DefaultString("aliKey", "")
	AliSecret = web.AppConfig.DefaultString("aliSecret", "")

	EnableStoreWrite = web.AppConfig.DefaultBool("enableStoreWrite", false)
	EnableJdStoreWrite = EnableStoreWrite && web.AppConfig.DefaultBool("enableJdStoreWrite", false)
	EnableEbaiStoreWrite = EnableStoreWrite && web.AppConfig.DefaultBool("enableEbaiStoreWrite", false)
	EnableElmStoreWrite = EnableStoreWrite && web.AppConfig.DefaultBool("enableElmStoreWrite", false)
	EnableDdStoreWrite = EnableStoreWrite && web.AppConfig.DefaultBool("enableDdStoreWrite", false)
	EnableMtwmStoreWrite = EnableStoreWrite && web.AppConfig.DefaultBool("enableMtwmStoreWrite", false)
	EnableWscStoreWrite = EnableStoreWrite && web.AppConfig.DefaultBool("enableWscStoreWrite", false)
	// EnablePendingChange = web.AppConfig.DefaultBool("enablePendingChange", false)
	EnableYbStoreWrite = EnableStoreWrite && web.AppConfig.DefaultBool("enableYbStoreWrite", false)
	EnableJdShopWrite = EnableStoreWrite && web.AppConfig.DefaultBool("enableJdShopWrite", false)

	QiniuBucket = web.AppConfig.DefaultString("qiniuBucket", "")

	WeimobCallbackURL = web.AppConfig.DefaultString("weimobCallbackURL", "")
	WeimobStateSecret = web.AppConfig.DefaultString("weimobStateSecret", "")

	BackstageHost = web.AppConfig.DefaultString("backstageHost", "")
	WxBackstageHost = web.AppConfig.DefaultString("wxBackstageHost", "")

	GetWeixinTokenURL = web.AppConfig.DefaultString("getWeixinTokenURL", "")
	GetWeixin2TokenURL = web.AppConfig.DefaultString("getWeixin2TokenURL", "")
	GetYLYTokenURL = web.AppConfig.DefaultString("getYLYTokenURL", "")
	GetWeimobTokenURL = web.AppConfig.DefaultString("getWeimobTokenURL", "")
	GetWeixinTokenKey = web.AppConfig.DefaultString("getWeixinTokenKey", "")
	GetPushTokenURL = web.AppConfig.DefaultString("getPushTokenURL", "")

	StoreName = web.AppConfig.DefaultString("storeName", "")
	StoreNameMtwm = web.AppConfig.DefaultString("storeNameMtwm", "")
	StoreNameTiktok = web.AppConfig.DefaultString("storeNameTiktok", "")
	StoreNameTao = web.AppConfig.DefaultString("storeNameTao", "")
	StoreNameEbai = web.AppConfig.DefaultString("storeNameEbai", "")
	StoreNameEbai2 = web.AppConfig.DefaultString("storeNameEbai2", "")

	SMSSignName = web.AppConfig.DefaultString("smsSignName", "")
	SMSMobileVerifyTemplate = web.AppConfig.DefaultString("smsMobileVerifyTemplate", "")
	SMSNewOrderTemplate = web.AppConfig.DefaultString("smsNewOrderTemplate", "")
	SMSNewOrderTemplateQ = web.AppConfig.DefaultString("smsNewOrderTemplateQ", "")
	SMSPickOrderTemplate = web.AppConfig.DefaultString("smsPickOrderTemplate", "")
	SMSOrderCanceledTemplate = web.AppConfig.DefaultString("smsOrderCanceledTemplate", "")
	SMSNewUserOrderTemplate = web.AppConfig.DefaultString("smsNewUserOrderTemplate", "")
	SMSJxOrderDelivering = web.AppConfig.DefaultString("smsJxOrderDelivering", "")
	SMSBrandBalanceTemplate = web.AppConfig.DefaultString("smsBrandBalanceTemplate", "")
	SMSVoicePickOrderTemplate = web.AppConfig.DefaultString("smsVoicePickOrderTemplate", "")
	SMSVoiceBrandBalanceTemplate = web.AppConfig.DefaultString("smsVoiceBrandBalanceTemplate", "")

	EnableWXAuth2 = true // web.BConfig.RunMode == "beta"
	DisableWXAuth1 = true

	WxpayNotifyURL = web.AppConfig.DefaultString("wxpayNotifyURL", "")
	TLPayNotifyURL = web.AppConfig.DefaultString("tonglianPayNotifyURL", "")
	FnNotifyURL = web.AppConfig.DefaultString("fnCallbackURL", "")
	JdOrgCode = web.AppConfig.DefaultString("jdOrgCode", "")
	Jd2OrgCode = web.AppConfig.DefaultString("jd2OrgCode", "")
	Jd3OrgCode = web.AppConfig.DefaultString("jd3OrgCode", "")
	JdcsOrgCode = web.AppConfig.DefaultString("jdcsOrgCode", "")
	JdydOrgCode = web.AppConfig.DefaultString("jdydOrgCode", "")
	JdLoginName = web.AppConfig.DefaultString("jdLoginName", "")
	IsAddEvent = web.AppConfig.DefaultBool("addEvent", false)
	TictokpayNotifyURL = web.AppConfig.DefaultString("tiktokNotifyUrl", "")
	TiktokShopAppSecret = web.AppConfig.DefaultString("tiktokShopAppSecret", "")

	IsStoreSkuAct = !IsProductEnv()

	MtwmCode = web.AppConfig.DefaultString("mtwmAppID", "")
	Mtwm2Code = web.AppConfig.DefaultString("mtwmAppID2", "")
}

func IsCallbackAlwaysReturnSuccess() bool {
	return web.BConfig.RunMode == "beta"
}

func IsMainProductEnv() bool {
	return web.BConfig.RunMode == "prod" // model.ServerTypeVegetable
}

func IsProductEnv() bool {
	return IsMainProductEnv() || web.BConfig.RunMode == "jxgy" || web.BConfig.RunMode == "jxpet"
}
