package mw

import (
	"applet/app/svc"
	"applet/app/utils"
	"code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/rule/mw"
	"errors"
	"fmt"
	"github.com/gin-gonic/gin"
	"strings"

	"applet/app/db"
	"applet/app/e"
	"applet/app/md"
)

// DB is 中间件 用来检查master_id是否有对应的数据库engine
func DB(c *gin.Context) {
	fmt.Println(c.Request.Header)
	masterID := mw.GetMasterId(db.Db, c)
	_, ok := db.DBs[masterID]
	if !ok {
		e.OutErr(c, e.ERR_MASTER_ID, errors.New("not found master_id in DBs"))
		return
	}

	fmt.Println("master_id", masterID)
	c.Set("mid", masterID)
	//判断应用是不是过期了
	isOverTime := svc.SysCfgGet(c, "is_over_time")
	if isOverTime == "1" {
		str := "应用已过期"
		overTimeStr := svc.SysCfgGet(c, "over_time_str")
		if overTimeStr != "" {
			str = overTimeStr
		}
		e.OutErr(c, 400, e.NewErr(400, str))
		return
	}
	closeStation := svc.SysCfgGet(c, "close_station")
	closeAppVersion := svc.SysCfgGet(c, "close_app_version")
	platform := c.GetHeader("Platform")
	if strings.Contains(closeStation, platform) && closeStation != "" && utils.StrToInt64(c.GetHeader("app_version")) <= utils.StrToInt64(closeAppVersion) {
		str := "应用关闭"
		overTimeStr := svc.SysCfgGet(c, "over_time_str")
		if overTimeStr != "" {
			str = overTimeStr
		}
		e.OutErr(c, 400, e.NewErr(400, str))
		return
	}
	//判断是否有独立域名
	domainWapBase := svc.GetWebSiteDomainInfo(c, "wap")

	httpStr := "http://"
	if c.GetHeader("Platform") == md.PLATFORM_WX_APPLET || c.GetHeader("Platform") == md.PLATFORM_ALIPAY_APPLET || c.GetHeader("Platform") == md.PLATFORM_BAIDU_APPLET || c.GetHeader("Platform") == md.PLATFORM_TOUTIAO_APPLET || c.GetHeader("Platform") == md.PLATFORM_TIKTOK_APPLET {
		httpStr = "https://"
		domainWapBase = strings.Replace(domainWapBase, "http://", httpStr, 1)
	}
	c.Set("domain_wap_base", domainWapBase)
	c.Set("domain_wap_base_new", svc.GetWebSiteLiveBroadcastDomainInfo(c, "wap", masterID))
	c.Set("domain_wap_base_second", svc.GetWebSiteDomainInfoSecond(c, "wap"))

	c.Set("http_host", httpStr)

	c.Set("h5_api_secret_key", svc.SysCfgGet(c, "h5_api_secret_key"))
	c.Set("app_api_secret_key", svc.SysCfgGet(c, "app_api_secret_key"))
	c.Set("applet_api_secret_key", svc.SysCfgGet(c, "applet_api_secret_key"))
	c.Set("integral_prec", svc.SysCfgGet(c, "integral_prec"))
	fanOrderCommissionPrec := svc.SysCfgGet(c, "fan_order_commission_prec")
	if fanOrderCommissionPrec == "" {
		fanOrderCommissionPrec = "2"
	}
	c.Set("fan_order_commission_prec", fanOrderCommissionPrec)
	areaOrderCommissionPrec := svc.SysCfgGet(c, "area_order_commission_prec")
	if areaOrderCommissionPrec == "" {
		areaOrderCommissionPrec = "2"
	}
	c.Set("area_order_commission_prec", areaOrderCommissionPrec)

	commissionPrec := svc.SysCfgGet(c, "commission_prec")
	c.Set("commission_prec", commissionPrec)
	pricePrec := svc.SysCfgGet(c, "price_prec")
	if pricePrec == "" {
		pricePrec = commissionPrec
	}
	dsChcek := svc.SysCfgGet(c, "ds_check")
	if dsChcek == "1" {
		pricePrec = commissionPrec
	}
	c.Set("price_prec", pricePrec)
	c.Set("is_show_point", svc.SysCfgGet(c, "is_show_point"))
	c.Set("appUserDefaultAvatar", svc.SysCfgGet(c, "app_user_default_avatar"))

	translateOpen := ""
	if strings.Contains(c.GetHeader("locale"), "zh_Hant_") {
		translateOpen = "zh_Hant_"
	}
	if strings.Contains(c.GetHeader("locale"), "ug_CN") {
		translateOpen = "ug_CN"
	}
	c.Set("translate_open", translateOpen)
	orderVirtualCoinType := db.SysCfgGet(c, "order_virtual_coin_type")
	c.Set("order_virtual_coin_type", orderVirtualCoinType)
	orderVirtualCoinName := db.SysCfgGet(c, "order_virtual_coin_name")
	if orderVirtualCoinName == "" {
		orderVirtualCoinName = "收益:¥"
	}
	c.Set("orderVirtualCoinName", orderVirtualCoinName)
	h5AppletMustSign := svc.SysCfgGet(c, "h5_applet_must_sign")
	c.Set("h5_applet_must_sign", h5AppletMustSign)
	androidMustSign := svc.SysCfgGet(c, "android_must_sign")
	c.Set("android_must_sign", androidMustSign)
	iosMustSign := svc.SysCfgGet(c, "ios_must_sign")
	c.Set("ios_must_sign", iosMustSign)
	c.Set("is_not_change_url", "0")
	smsType := svc.SysCfgGet(c, "sms_type")
	c.Set("sms_type", smsType)
	if utils.StrToInt64(c.GetHeader("app_version")) > 1678445020 {
		// || utils.StrToInt64(c.GetHeader("BuildVersion")) > 1678676004
		c.Set("is_not_change_url", "1")
	}
	if utils.InArr(c.GetHeader("platform"), []string{md.PLATFORM_ANDROID, md.PLATFORM_IOS}) == false {
		c.Set("is_not_change_url", "1")
	}
	GetHeaderParam(c)
	c.Next()
}

func GetHeaderParam(c *gin.Context) {
	var appTypeList = []string{"app_type", "App_type", "AppType"}
	appType := ""
	for _, v := range appTypeList {
		val := c.GetHeader(v)
		if val != "" {
			appType = val
		}
	}
	c.Set("app_type", appType)
	var storeIdList = []string{"store_id", "Store_id", "StoreId"}
	storeId := ""
	for _, v := range storeIdList {
		val := c.GetHeader(v)
		if val != "" {
			storeId = val
		}
	}
	c.Set("store_id", storeId)
}