package util

import (
	"fmt"
	"strconv"
	"strings"
	"time"
)

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

func BagTimeInInterval(bagTime string, begin string, end string) bool {
	compare1 := TimeCustom1GreaterEqualThanTimeCustom2(bagTime, begin)
	compare2 := TimeCustom1LessEqualThanTimeCustom2(bagTime, end)
	return compare1 && compare2
}

func TimeCustom1GreaterEqualThanTimeCustom2(timeCustom1 string, timeCustom2 string) bool {
	timeInt1, _ := strconv.Atoi(strings.Replace(timeCustom1, "-", "", -1))
	timeInt2, _ := strconv.Atoi(strings.Replace(timeCustom2, "-", "", -1))
	return timeInt1 >= timeInt2
}
func GetBagTime(bagName string) string {
	s1 := strings.Split(bagName, "_")[0]
	s1Split := strings.Split(s1, "/")
	s2 := s1Split[len(s1Split)-1]
	return s2
}
func TimeCustomChange(originalTimeStr string, number int) string {
	var newTimeStr string
	layout := "2006-01-02-15-04-05"
	originalTime, err := time.Parse(layout, originalTimeStr)
	if err != nil {
		return newTimeStr
	}
	newTime := originalTime.Add(time.Duration(number) * time.Second)
	return newTime.Format(layout)
}
func CalculateDifferenceOfTimeCustom(timeCustom1 string, timeCustom2 string) int {
	timeInt1, _ := strconv.Atoi(strings.Replace(timeCustom1, "-", "", -1))
	timeInt2, _ := strconv.Atoi(strings.Replace(timeCustom2, "-", "", -1))
	return timeInt2 - timeInt1 + 1

}
func TimeCustom1GreaterTimeCustom2(timeCustom1 string, timeCustom2 string) bool {
	timeInt1, _ := strconv.Atoi(strings.Replace(timeCustom1, "-", "", -1))
	timeInt2, _ := strconv.Atoi(strings.Replace(timeCustom2, "-", "", -1))
	return timeInt1 > timeInt2
}

func TimeCustom1LessEqualThanTimeCustom2(timeCustom1 string, timeCustom2 string) bool {
	timeInt1, _ := strconv.Atoi(strings.Replace(timeCustom1, "-", "", -1))
	timeInt2, _ := strconv.Atoi(strings.Replace(timeCustom2, "-", "", -1))
	return timeInt1 <= timeInt2
}

func GetTimeString(sourceTime time.Time) string {
	var defaultTime = time.Date(2006, time.January, 2, 15, 4, 5, 0, time.Local)
	if sourceTime.Equal(defaultTime) {
		return ""
	}
	return sourceTime.Format("2006-01-02 15:04:05")
}

func AnyToTime(value any) (time.Time, error) {
	switch v := value.(type) {
	case time.Time:
		return v, nil
	default:
		return time.Time{}, fmt.Errorf("unsupported type: %T", value)
	}
}
