package utils

import (
	"encoding/hex"
	"fmt"
	"gin-vue-admin/model"
	"math/rand"
	"sort"
	"strconv"
	"strings"
	"time"
)

func JavaTimeToDate(timeI int) string {
	dateS := strconv.Itoa(timeI)
	dateI, _ := strconv.ParseInt(dateS[0:10], 10, 64)
	return time.Unix(dateI, 0).Format("2006-01-02 15:04:05")
}

func StringToInt(data string) int {
	reply, _ := strconv.Atoi(data)
	return reply
}

func StringToInt64(data string) int64 {
	reply, _ := strconv.ParseInt(data, 10, 64)
	return reply
}

func String2Float(d string) float64 {
	r, _ := strconv.ParseFloat(d, 64)
	return r
}

func Float2String(d float64) string {
	return strconv.FormatFloat(d, 'f', 2, 64)
}

func Float2Int(d float64) int {
	r, _ := strconv.Atoi(fmt.Sprintf("%1.0f", d))
	return r
}

func FeeToString(fee int) string {
	return fmt.Sprintf("%.2f", float64(fee)/100)
}

func StringToFee(fee string) int {
	float, _ := strconv.ParseFloat(fee, 64)
	return int(float * 100)
}

// 生成随机6位数字
func CreateCaptcha() string {
	return fmt.Sprintf("%06v", rand.New(rand.NewSource(time.Now().UnixNano())).Int31n(1000000))
}

/*
*
递归获取树形菜单
*/
func GetMenu(list []*model.Region, pid int64) []*model.RegionList {
	var treeList []*model.RegionList
	for _, v := range list {
		if pid == v.ParentId {
			child := GetMenu(list, v.RegionId)
			node := model.RegionList{
				RegionId:   v.RegionId,
				RegionName: v.RegionName,
				ParentId:   v.ParentId,
			}
			node.Children = child
			treeList = append(treeList, &node)
		}
	}

	return treeList
}

func Bt2sting(bt []byte) string {
	str := string(bt)
	str = strings.Replace(str, " ", "", -1)
	str = strings.Replace(str, "\n", "", -1)
	return str
}

func Time2Str(timeVal time.Time) string {
	strTime := timeVal.Format("2006-01-02 15:04:05")
	if strTime == "0001-01-01 00:00:00" {
		strTime = ""
	}
	return strTime
}

func StringToTime(data string) string {
	to, _ := time.Parse("2006-01-02T15:04:05+08:00", data)

	strTime := to.Format("2006-01-02 15:04:05")

	if strTime == "0001-01-01 00:00:00" {
		strTime = ""
	}

	return strTime
}

func Time2StrHMS(timeVal int) string {
	s := timeVal % 60
	if timeVal/60 == 0 {
		return strconv.Itoa(s) + "秒"
	}
	m := (timeVal / 60) % 60
	if (timeVal/60)/60 == 0 {
		return strconv.Itoa(m) + "分" + strconv.Itoa(s) + "秒"
	}
	h := (timeVal / 60) / 60
	return strconv.Itoa(h) + "时" + strconv.Itoa(m) + "分" + strconv.Itoa(s) + "秒"
}

func GetFirstDay() string {
	now := time.Now()
	currentYear, currentMonth, _ := now.Date()
	currentLocation := now.Location()

	firstOfMonth := time.Date(currentYear, currentMonth, 1, 0, 0, 0, 0, currentLocation)
	return firstOfMonth.Format("2006-01-02")
}

func GetLastDay() string {
	now := time.Now()
	currentYear, currentMonth, _ := now.Date()
	currentLocation := now.Location()

	lastOfMonth := time.Date(currentYear, currentMonth, 1, 0, 0, 0, 0, currentLocation).AddDate(0, 1, -1)
	return lastOfMonth.Format("2006-01-02")
}

func GetOrderNo(start string, num int) string {
	timeStr := time.Now().Format("20060102150405")
	orderNo := strconv.Itoa(num) + strconv.FormatInt(time.Now().UnixNano(), 10)[12:16]
	return start + timeStr + sup(orderNo, 9)
}

func GetPointsOrderNo(num int) string {
	timeStr := time.Now().Format("20060102150405")
	orderNo := strconv.Itoa(num) + strconv.FormatInt(time.Now().UnixNano(), 10)[12:16]
	return "P" + timeStr + sup(orderNo, 9)
}

func GetRandStr2() string {
	charset := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
	seededRand := rand.New(rand.NewSource(time.Now().UnixNano()))

	b := make([]byte, 6)
	for i := range b {
		b[i] = charset[seededRand.Intn(len(charset))]
	}
	return string(b)
}

func GetRandStr() string {
	result := make([]byte, 3)
	rand.Read(result)
	return hex.EncodeToString(result)
}

func GetRandNum(num int) string {
	code := ""
	rand.Seed(time.Now().UnixNano())
	min := 0
	max := 9
	for i := 0; i < num; i++ {
		code += strconv.Itoa(rand.Intn(max-min+1) + min)
	}
	return code
}

// 对长度不足n的数字前面补0
func sup(i string, n int) string {
	m := fmt.Sprintf("%s", i)
	for len(m) < n {
		m = fmt.Sprintf("0%s", m)
	}
	return m
}

// 获取指定时间所在月的开始 结束时间
func GetMonthStartEnd(t time.Time) (time.Time, time.Time) {
	monthStartDay := t.AddDate(0, 0, -t.Day()+1)
	monthStartTime := time.Date(monthStartDay.Year(), monthStartDay.Month(), monthStartDay.Day(), 0, 0, 0, 0, t.Location())
	monthEndDay := monthStartTime.AddDate(0, 1, -1)
	monthEndTime := time.Date(monthEndDay.Year(), monthEndDay.Month(), monthEndDay.Day(), 23, 59, 59, 0, t.Location())
	return monthStartTime, monthEndTime
}

func String2Time(data string) string {
	to, _ := time.Parse("2006-01-02T15:04:05+08:00", data)

	strTime := to.Format("2006-01-02 15:04:05")

	if strTime == "0001-01-01 00:00:00" {
		strTime = ""
	}

	if strTime == "" {
		return data
	}

	return strTime
}

func TimeSince(timeStart, timeEnd string) float64 {
	toStart, _ := time.Parse("2006-01-02T15:04:05+08:00", timeStart)
	toEnd, _ := time.Parse("2006-01-02T15:04:05+08:00", timeEnd)
	return toEnd.Sub(toStart).Seconds()
}

/*
*
递归获取书目录
*/
func GetBookCatalogs(list []*model.ReadCatalog, pid int) []*model.ReadCatalogList {
	var treeList []*model.ReadCatalogList
	for _, v := range list {
		if pid == v.ParentId {
			child := GetBookCatalogs(list, v.Id)
			node := model.ReadCatalogList{
				Id:          v.Id,
				Name:        v.Name,
				ReadBooksId: v.ReadBooksId,
				ParentId:    v.ParentId,
				SortOrder:   v.SortOrder,
				Content:     v.Content,
			}
			node.Children = child
			treeList = append(treeList, &node)
		}
	}

	return treeList
}

// 检查是否为英文
func ContainsEnglish(str string) bool {
	dictionary := "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"

	isEn := 0
	for _, v := range str {
		if strings.Contains(dictionary, string(v)) {
			isEn += 1

		}
	}
	if isEn > 3 {
		return true
	}
	return false
}

func InArray(target string, strArray []string) bool {
	sort.Strings(strArray)
	index := sort.SearchStrings(strArray, target)
	if index < len(strArray) && strArray[index] == target {
		return true
	}
	return false
}

func REndDate(date string) string {
	if date != "" {
		dateArr := strings.Split(date, " ")
		return dateArr[0] + " 23:59:59"
	}
	return ""
}

func NowTime() string {
	return time.Now().Format("2006-01-02 15:04:05")
}
