package tool

//数字相关的

import (
	"fmt"
	"github.com/shopspring/decimal"
	"math"
	"math/rand"
	"strconv"
	"time"
)

// float64保留两个小数点 向下取，就是去掉后面的值
func DecimalDown(num float64) float64 {
	numD := int64(math.Floor(num * 100))
	return float64(numD) / 100
}

// 去掉模糊的99999999等精度
func Decimal(num float64) float64 {
	num, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", num), 64)
	return num
}

// 四舍五入
func Rounding(num float64) float64 {
	res, _ := decimal.NewFromFloat(num).Round(2).Float64()
	return res
}

// 去掉模糊的99999999等精度,去掉小数点
func Decimal0(num float64) float64 {
	num, _ = strconv.ParseFloat(fmt.Sprintf("%.0f", num), 64)
	return num

}

func round(x float64) int {
	return int(math.Floor(x + 0.5))
}

// 向下取整数
func RemoveDecimal(num float64) int64 {
	return int64(math.Floor(num))
}

// 随机获取一个字符串
func RandomStr() string {
	return fmt.Sprintf("%d", time.Now().UnixNano())
}

// 随机获取一个 数字，长度为len
func RandomNum(size int, kind int) []byte {
	ikind, kinds, result := kind, [][]int{[]int{10, 48}, []int{26, 97}, []int{26, 65}}, make([]byte, size)
	is_all := kind > 2 || kind < 0
	rand.Seed(time.Now().UnixNano())
	for i := 0; i < size; i++ {
		if is_all { // random ikind
			ikind = rand.Intn(3)
		}
		scope, base := kinds[ikind][0], kinds[ikind][1]
		result[i] = uint8(base + rand.Intn(scope))
	}
	return result
}

// Ordered 比较类型约束
type Ordered interface {
	Integer | Float | ~string
}

type Integer interface {
	Signed | Unsigned
}

type Signed interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64
}

type Unsigned interface {
	~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
}

type Float interface {
	~float32 | ~float64
}

// Min 比较数值大小，返回最小值
func Min[K Ordered](x K, y ...K) K {
	if len(y) == 0 {
		return x
	}
	min := x
	for _, v := range y {
		if v < min {
			min = v
		}
	}
	return min
}
