package utils

import (
	"code.fnuoos.com/go_rely_warehouse/zyos_go_coupon.git/utils"
	"fmt"
	"github.com/gin-gonic/gin"
	"math"
	"strings"
)

func CouponFormat(c *gin.Context, data string) string {
	switch data {
	case "0.00", "0", "":
		return ""
	default:
		return GetPrec(c, data, "2")
	}
}
func GetPrec(c *gin.Context, sum, commPrec string) string {
	if sum == "" {
		sum = "0"
	}
	sum = StrToFormat(c, sum, utils.StrToInt(commPrec))
	ex := strings.Split(sum, ".")
	if len(ex) == 2 && c.GetString("is_show_point") != "1" {
		if utils.StrToFloat64(ex[1]) == 0 {
			sum = ex[0]
		} else {
			val := utils.Float64ToStrByPrec(utils.StrToFloat64(ex[1]), 0)
			keyMax := 0
			for i := 0; i < len(val); i++ {
				ch := string(val[i])
				fmt.Println(utils.StrToInt(ch))
				if utils.StrToInt(ch) > 0 {
					keyMax = i
				}
			}
			valNew := val[0 : keyMax+1]
			sum = ex[0] + "." + strings.ReplaceAll(ex[1], val, valNew)
		}
	}
	return sum
}

func CommissionFormat(data string) string {
	if StrToFloat64(data) > 0 {
		return data
	}

	return ""
}

func HideString(src string, hLen int) string {
	str := []rune(src)
	if hLen == 0 {
		hLen = 4
	}
	hideStr := ""
	for i := 0; i < hLen; i++ {
		hideStr += "*"
	}
	hideLen := len(str) / 2
	showLen := len(str) - hideLen
	if hideLen == 0 || showLen == 0 {
		return hideStr
	}
	subLen := showLen / 2
	if subLen == 0 {
		return string(str[:showLen]) + hideStr
	}
	s := string(str[:subLen])
	s += hideStr
	s += string(str[len(str)-subLen:])
	return s
}

// SaleCountFormat is 格式化销量
func SaleCountFormat(s string) string {
	s = strings.ReplaceAll(s, "+", "")
	if strings.Contains(s, "万") {
		s = strings.ReplaceAll(s, "万", "")
		s = Float64ToStrByPrec(StrToFloat64(s)*10000, 0)
	}
	ex := strings.Split(s, ".")
	if len(ex) == 2 && StrToInt(ex[1]) == 0 {
		s = ex[0]
	}
	if utils.StrToInt(s) > 0 {
		s = Comm(s)
		return "已售" + s
	}
	return "已售0"
}
func SaleCountFormat2(s string) string {
	s = strings.ReplaceAll(s, "+", "")
	if strings.Contains(s, "万") {
		s = strings.ReplaceAll(s, "万", "")
		s = Float64ToStrByPrec(StrToFloat64(s)*10000, 0)
	}
	ex := strings.Split(s, ".")
	if len(ex) == 2 && StrToInt(ex[1]) == 0 {
		s = ex[0]
	}
	if utils.StrToInt(s) > 0 {
		s = Comm(s)

		return s
	}
	return "0"
}
func SaleCountFormat1(s string) string {
	s = strings.ReplaceAll(s, "+", "")
	if strings.Contains(s, "万") {
		s = strings.ReplaceAll(s, "万", "")
		s = Float64ToStrByPrec(StrToFloat64(s)*10000, 0)
	}
	ex := strings.Split(s, ".")
	if len(ex) == 2 && StrToInt(ex[1]) == 0 {
		s = ex[0]
	}
	if utils.StrToInt(s) > 0 {
		s = Comm(s)
		return s
	}
	return "0"
}
func Comm(s string) string {
	ex := strings.Split(s, ".")
	if len(ex) == 2 && StrToInt(ex[1]) == 0 {
		s = ex[0]
	}
	if utils.StrToInt(s) >= 10000 {
		num := FloatFormat(StrToFloat64(s)/10000, 2)
		numStr := Float64ToStr(num)
		ex := strings.Split(numStr, ".")
		if len(ex) == 2 {
			if utils.StrToFloat64(ex[1]) == 0 {
				numStr = ex[0]
			} else {
				val := utils.Float64ToStrByPrec(utils.StrToFloat64(ex[1]), 0)
				keyMax := 0
				for i := 0; i < len(val); i++ {
					ch := string(val[i])
					fmt.Println(utils.StrToInt(ch))
					if utils.StrToInt(ch) > 0 {
						keyMax = i
					}
				}
				valNew := val[0 : keyMax+1]
				numStr = ex[0] + "." + strings.ReplaceAll(ex[1], val, valNew)
			}
		}
		s = numStr + "万"
	}
	//if StrToInt(s) >= 10000000 {
	//	num := FloatFormat(StrToFloat64(s)/10000000, 2)
	//	numStr := Float64ToStr(num)
	//	ex := strings.Split(numStr, ".")
	//	if len(ex) == 2 {
	//		if utils.StrToFloat64(ex[1]) == 0 {
	//			numStr = ex[0]
	//		} else {
	//			val := utils.Float64ToStrByPrec(utils.StrToFloat64(ex[1]), 0)
	//			keyMax := 0
	//			for i := 0; i < len(val); i++ {
	//				ch := string(val[i])
	//				fmt.Println(utils.StrToInt(ch))
	//				if utils.StrToInt(ch) > 0 {
	//					keyMax = i
	//				}
	//			}
	//			valNew := val[0 : keyMax+1]
	//			numStr = ex[0] + "." + strings.ReplaceAll(ex[1], val, valNew)
	//		}
	//	}
	//	s = numStr + "千万"
	//}
	return s
}
func CommissionFormat1(numStr string) string {
	split := strings.Split(numStr, ".")
	if len(split) == 2 {
		if split[1] == "00" {
			numStr = strings.ReplaceAll(numStr, ".00", "")
		}
	}
	return numStr
}

// 小数格式化
func FloatFormat(f float64, i int) float64 {
	if i > 14 {
		return f
	}
	p := math.Pow10(i)
	return float64(int64((f+0.000000000000009)*p)) / p
}
