package utils

import (
	crand "crypto/rand"
	"fmt"
	"math"
	"math/big"
	"math/rand"
	"time"
)

func RandString(l int, c ...string) string {
	var (
		chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
		str   string
		num   *big.Int
	)
	if len(c) > 0 {
		chars = c[0]
	}
	chrLen := int64(len(chars))
	for len(str) < l {
		num, _ = crand.Int(crand.Reader, big.NewInt(chrLen))
		str += string(chars[num.Int64()])
	}
	return str
}

func RandNum() string {
	seed := time.Now().UnixNano() + rand.Int63()
	return fmt.Sprintf("%05v", rand.New(rand.NewSource(seed)).Int31n(1000000))
}

// x的y次方
func RandPow(l int) string {
	var i = "1"
	for j := 0; j < l; j++ {
		i += "0"
	}
	k := StrToInt64(i)
	n := rand.New(rand.NewSource(time.Now().UnixNano())).Int63n(k)
	ls := "%0" + IntToStr(l) + "v"
	str := fmt.Sprintf(ls, n)
	//min := int(math.Pow10(l - 1))
	//max := int(math.Pow10(l) - 1)
	return str
}
func RandInt(min, max int) int {
	if min >= max || min == 0 || max == 0 {
		if max == 0 {
			max = min
		}
		return max
	}
	return rand.Intn(max-min) + min
}
func RandInt1(min, max int) int {
	rand.Seed(time.Now().UnixNano())
	return rand.Intn(max-min+1) + min
}

func CalculateDistance(lat1, lng1, lat2, lng2 float64) float64 {
	radius := 6371.0 // 地球半径（单位：公里）
	dLat := DegToRad(lat2 - lat1)
	dLng := DegToRad(lng2 - lng1)
	a := math.Sin(dLat/2)*math.Sin(dLat/2) + math.Cos(DegToRad(lat1))*math.Cos(DegToRad(lat2))*math.Sin(dLng/2)*math.Sin(dLng/2)
	c := 2 * math.Atan2(math.Sqrt(a), math.Sqrt(1-a))
	distance := radius * c

	return distance
}

// 将角度转换为弧度
func DegToRad(deg float64) float64 {
	return deg * (math.Pi / 180)

}
