package utils

import (
	"errors"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/apimachinery/pkg/util/duration"
	"regexp"
	"strconv"
	"strings"
	"time"
)

//转换时间 2019-01-28T19:48:19.298572068Z 格式化 2019-01-28 19:48:19
func ParseDate(t string, isRemoveSecond bool) string {
	if !strings.Contains(t, "T") {
		return t
	}
	all := strings.Split(t, "T")

	start := strings.Split(all[0], "-")
	year, _ := strconv.Atoi(start[0])
	month, _ := strconv.Atoi(start[1])
	day, _ := strconv.Atoi(start[2])

	end := strings.Split(all[1], ":")
	hour, _ := strconv.Atoi(end[0])
	minute, _ := strconv.Atoi(end[1])
	secondStr := end[2]
	if strings.Contains(secondStr, "Z") {
		secondStr = secondStr[:strings.Index(secondStr, "Z")]
	}
	if strings.Contains(secondStr, ".") {
		secondStr = secondStr[:strings.Index(secondStr, ".")]
	}
	second, _ := strconv.Atoi(secondStr)

	t1 := time.Date(year, time.Month(month), day, hour, minute, second, 0, time.Local)
	//加上8小时
	t2 := t1.Add(8 * time.Hour).String()
	t2 = t2[:strings.Index(t2, " +")]

	//去掉秒
	if isRemoveSecond {
		t2 = t2[:strings.LastIndex(t2, ":")]
	}
	return t2
}

// 转换时间 指定时间与当前时间差 返回具体秒、分钟、小时、天、年
func ParseDateToString(lang string, t time.Time) (string, error) {
	nowUnix := time.Now().Unix()
	//计算时间之差
	differenceStr := strconv.FormatInt(nowUnix-t.Unix(), 10)
	differenceInt, _ := strconv.Atoi(differenceStr)
	//InfoLogger.Println(differenceInt)
	//InfoLogger.Println("differenceInt = ", differenceInt)
	switch {
	case differenceInt >= 1 && differenceInt < 60:
		return differenceStr + GetI18nLang(lang, "seconds"), nil
	case differenceInt >= 60 && differenceInt < 60*60:
		return strconv.Itoa(differenceInt/60) + GetI18nLang(lang, "minutes"), nil
	case differenceInt >= 60*60 && differenceInt < 60*60*24:
		return strconv.Itoa(differenceInt/60/60) + GetI18nLang(lang, "hours"), nil
	case differenceInt >= 60*60*24 && differenceInt < 60*60*24*365:
		return strconv.Itoa(differenceInt/60/60/24) + GetI18nLang(lang, "day"), nil
	case differenceInt >= 60*60*24*365:
		return strconv.Itoa(differenceInt/60/60/24/365) + GetI18nLang(lang, "year"), nil
	default:
		return "", errors.New("时间转换失败")
	}

}

// 返回持续时间 如13d1h  如isUseI18n为false时不需要做国际化转换
func GetDurationToString(lang string, t time.Time, isUseI18n bool) string {
	if t.IsZero() {
		return "<unknown>"
	}
	d := duration.HumanDuration(time.Since(t))

	if isUseI18n && lang == "zh" {
		if m, _ := regexp.MatchString(".*([0-9][smhdy]).*", d); m {
			d = strings.Replace(d, "s", "秒", -1)
			d = strings.Replace(d, "m", "分钟", -1)
			d = strings.Replace(d, "h", "小时", -1)
			d = strings.Replace(d, "d", "天", -1)
			d = strings.Replace(d, "y", "年", -1)
		}
	}
	return d
}

//转换k8s时间 2019-07-19 14:39:06 +0800 CST 格式化 2019-07-19 14:39:06
func ParseK8sDateTime(t metav1.Time, isRemoveSecond bool) string {
	if t.IsZero() {
		return "<unknown>"
	}
	timeStr := t.String()
	all := strings.Split(timeStr, " ")

	start := strings.Split(all[0], "-")
	year, _ := strconv.Atoi(start[0])
	month, _ := strconv.Atoi(start[1])
	day, _ := strconv.Atoi(start[2])

	end := strings.Split(all[1], ":")
	hour, _ := strconv.Atoi(end[0])
	minute, _ := strconv.Atoi(end[1])
	secondStr := end[2]
	if strings.Contains(secondStr, "Z") {
		secondStr = secondStr[:strings.Index(secondStr, "Z")]
	}
	if strings.Contains(secondStr, ".") {
		secondStr = secondStr[:strings.Index(secondStr, ".")]
	}
	second, _ := strconv.Atoi(secondStr)

	t1 := time.Date(year, time.Month(month), day, hour, minute, second, 0, time.Local)
	//加上8小时
	t2 := t1.Add(8 * time.Hour).String()
	t2 = t2[:strings.Index(t2, " +")]

	//去掉秒
	if isRemoveSecond {
		t2 = t2[:strings.LastIndex(t2, ":")]
	}
	return t2
}
