package tool

import (
	"fmt"
	"time"
)

const (
	YmdHis = `2006-01-02 15:04:05`
	Ymd    = `2006-01-02`
	Ym     = `2006-01`
)

// 写之前 检查carbon库是否有相关的方法 https://github.com/golang-module/carbon

// 获取月第一天0点时间
func GetMonthFirstDay(t time.Time) string {
	monthDay := t.AddDate(0, 0, -t.Day()+1)
	day1 := time.Date(monthDay.Year(), monthDay.Month(), monthDay.Day(), 0, 0, 0, 0, monthDay.Location())
	return day1.Format(YmdHis)
}

// string转时间
func SToTime(timeS string) time.Time {
	local, _ := time.LoadLocation("Asia/Shanghai")
	timeT, _ := time.ParseInLocation(YmdHis, timeS, local)
	return timeT
}

// TimeToS 时间转string
func TimeToS(t time.Time) string {
	local, _ := time.LoadLocation("Asia/Shanghai")
	return t.In(local).Format(YmdHis)
}

// string time 转成时间戳 int32
func STimeToI64(timeS string) int64 {
	t := SToTime(timeS)
	timeStamp := t.Unix()
	return timeStamp
}

func CompareTime(t1 string, t2 string, t3 string) bool {
	time1, err := time.Parse("15:04", t1)
	if err != nil {
		return false
	}
	time2, err := time.Parse("15:04", t2)
	if err != nil {
		return false
	}
	time3, err := time.Parse("15:04", t3)
	if err != nil {
		return false
	}
	if time1.After(time2) && time1.Before(time3) {
		return true
	}
	return false
}

// GetFewDaysTime 获取具体几天后/前的时间
func GetFewDaysTime(days int8) time.Time {
	return time.Now().AddDate(0, 0, int(days))
}

// IsToday 判断是否为当天时间
func IsToday(t time.Time) bool {
	now := time.Now()
	return t.Year() == now.Year() && t.Month() == now.Month() && t.Day() == now.Day()
}

// GetDiffDays 获取两个时间相差的天数
func GetDiffDays(startTime, endTime time.Time) (diffDays int) {
	startTimestamp := startTime.Unix()
	endTimestamp := endTime.Unix()

	if startTimestamp > endTimestamp {
		startTimestamp, endTimestamp = endTimestamp, startTimestamp
	}

	diffTimestamp := endTimestamp - startTimestamp

	diffDays = int(diffTimestamp / (24 * 60 * 60))

	return
}

func CalculateAge(birthday time.Time) string {
	now := time.Now()
	diff := now.Sub(birthday)

	years := int(diff.Hours() / (24 * 365))
	months := int(diff.Hours()/(24*30)) % 12
	days := int(diff.Hours()/24) % 30

	result := ""
	if years > 0 {
		result += fmt.Sprintf("%d年", years)
	}
	if months > 0 {
		result += fmt.Sprintf("%d月", months)
	}
	if days > 0 {
		result += fmt.Sprintf("%d日", days)
	}

	return result
}

func CalculateDuration(start time.Time, end time.Time) string {
	diff := end.Sub(start)
	totalDays := int(diff.Hours() / 24)
	years := int(totalDays / 365)
	days := totalDays % 365

	result := ""
	if years > 0 {
		result += fmt.Sprintf("%d年", years)
	}
	if days > 0 {
		result += fmt.Sprintf("%d天", days)
	}

	return result
}

func AgeCalculate(birthday string) string {
	ageShow := ""
	if birthday != "" {
		layout := "2006-01-02" // Go中的时间格式化模板
		birthTime, err := time.Parse(layout, birthday)
		if err != nil {
			return ageShow // 如果日期格式不正确，返回空字符串
		}
		now := time.Now()
		years := now.Year() - birthTime.Year()
		months := int(now.Month()) - int(birthTime.Month())
		days := now.Day() - birthTime.Day()

		if days < 0 {
			// 如果天数为负，减去一个月，并重新计算天数
			months--
			days += time.Date(birthTime.Year(), birthTime.Month()+1, 0, 0, 0, 0, 0, time.UTC).Day()
		}
		if months < 0 {
			// 如果月份为负，减去一年，并重新计算月份
			years--
			months += 12
		}

		if years > 0 {
			ageShow += fmt.Sprintf("%d岁", years)
		}
		if months > 0 {
			ageShow += fmt.Sprintf("%d月", months)
		}
		if days > 0 {
			ageShow += fmt.Sprintf("%d天", days)
		}
	}
	return ageShow
}
