package main

import (
	"log"
	"time"
)

var WeekDayMap = map[string]int64{
	"Monday":    1,
	"Tuesday":   2,
	"Wednesday": 3,
	"Thursday":  4,
	"Friday":    5,
	"Saturday":  6,
	"Sunday":    7,
}

// 获取输入日期分别是星期几
func GetOneWeekDay(startime string) (int64) {
	startday, _ := time.Parse("2006-01-02", startime)
	staweek_int := startday.Weekday().String()
	return WeekDayMap[staweek_int]
}


// 获取输入的两个日期分别是星期几
func GetWeekDay(startime, endtim string) (int64, int64) {
	startday, _ := time.Parse("2006-01-02", startime)
	endday, _ := time.Parse("2006-01-02", endtim)
	staweek_int := startday.Weekday().String()
	endweek_int := endday.Weekday().String()
	return WeekDayMap[staweek_int], WeekDayMap[endweek_int]
}


// 字符串转时间戳
func StringToTimeStamp(strTime string) int64 {
	timeLayout := "2006-01-02"
	//timeLayout := "2006-01-02 15:04:05"
	loc, _ := time.LoadLocation("Local")
	the_time, err := time.ParseInLocation(timeLayout, strTime, loc)
	if err != nil {
		log.Println("StringToTimeStamp出现异常：", err)
	}
	unix_time := the_time.Unix()
	return unix_time
}

// 时间戳转 字符串
func TimeStampToString(timeStp int64) string {
	//转化所需模板
	//timeLayout := "2006-01-02 15:04:05"
	timeLayout := "2006-01-02"
	//进行格式化
	datetime := time.Unix(timeStp, 0).Format(timeLayout)
	return datetime
}


// 时间转化为周日期列表
func ChangeToWeek(startime, endtim string) []map[string]string {

	staweek_int, endweek_int := GetWeekDay(startime, endtim)
	// 获取时间戳
	start_stamp := StringToTimeStamp(startime)
	end_stamp := StringToTimeStamp(endtim)
	log.Println("start_stamp==",start_stamp,"end_stamp==", end_stamp)

	var week_list = make([]map[string]string, 0)
	if (end_stamp-start_stamp)/604800 <= 1 && endweek_int-staweek_int >= 0 {
		if end_stamp-start_stamp < 604800 && endweek_int-staweek_int > 0 {
			one_map := map[string]string{}
			mon_one := TimeStampToString(start_stamp - (staweek_int-1)*86400)
			sun_one := TimeStampToString(start_stamp + (7-staweek_int)*86400)
			one_map["mon"] = mon_one
			one_map["sun"] = sun_one
			week_list = append(week_list, one_map)
			return week_list
		}
		one_map := map[string]string{}
		mon_one := TimeStampToString(start_stamp - (staweek_int-1)*86400)
		sun_one := TimeStampToString(start_stamp + (7-staweek_int)*86400)
		one_map["mon"] = mon_one
		one_map["sun"] = sun_one
		week_list = append(week_list, one_map)
		tow_map := map[string]string{}
		mon_tow := TimeStampToString(end_stamp - (endweek_int-1)*86400)
		sun_tow := TimeStampToString(end_stamp + (7-endweek_int)*86400)
		tow_map["mon"] = mon_tow
		tow_map["sun"] = sun_tow
		week_list = append(week_list, tow_map)
		return week_list
	}
	week_n := (end_stamp - start_stamp) / 604800
	one_map := map[string]string{}
	mon_one := TimeStampToString(start_stamp - (staweek_int-1)*86400)
	sun_one := TimeStampToString(start_stamp + (7-staweek_int)*86400)
	one_map["mon"] = mon_one
	one_map["sun"] = sun_one
	week_list = append(week_list, one_map)
	for i := 1; i <= int(week_n); i++ {
		week_map := map[string]string{}
		mon_day := TimeStampToString(start_stamp - (staweek_int-1)*86400 + int64(i)*604800)
		sun_day := TimeStampToString(start_stamp + (7-staweek_int)*86400 + int64(i)*604800)
		week_map["mon"] = mon_day
		week_map["sun"] = sun_day
		week_list = append(week_list, week_map)
	}
	if endweek_int-staweek_int >= 0 {
		return week_list
	}
	tow_map := map[string]string{}
	mon_tow := TimeStampToString(end_stamp - (endweek_int-1)*86400)
	sun_tow := TimeStampToString(end_stamp + (7-endweek_int)*86400)
	tow_map["mon"] = mon_tow
	tow_map["sun"] = sun_tow
	week_list = append(week_list, tow_map)
	return week_list

}