package schedule

import (
	"time"
)

// 绝对时间线段
type TimeLineSrv struct {
	StartTime 	time.Time // 时间曲线的起始时间点
	EndTime   	time.Time // 时间曲线的结束时间点
	Duration 	time.Duration
	EffFactor 	float64   	 // 效率因子
	WorkShiftNr	int 		// 人员班组编号
}

// 初始化，获取时间差值 = (结束时间 - 开始时间) * 效率因子
func (tlsrv *TimeLineSrv)Init(){
	var seconds		float64

	if tlsrv.EffFactor <= 0 {
		tlsrv.EffFactor = 1.0
	}
	if tlsrv.StartTime.Unix() > tlsrv.EndTime.Unix() {
		tlsrv.Duration = 0
	}else{
		seconds 		= tlsrv.EndTime.Sub(tlsrv.StartTime).Seconds() * tlsrv.EffFactor
		tlsrv.Duration	= time.Duration(seconds) * time.Second
	}
}

// 获取时间线的Duration，不考虑效率因子
func (tlsrv *TimeLineSrv)GetDuration()(duration time.Duration){
	if tlsrv.StartTime.Unix() > tlsrv.EndTime.Unix() {
		duration 	= 0
		return
	}else{
		duration	= tlsrv.EndTime.Sub(tlsrv.StartTime)
		return
	}
}

// 获取时间线的Duration，考虑效率因子
func (tlsrv *TimeLineSrv)GetEffDuration()(effDuration time.Duration){
	var seconds		float64
	if tlsrv.StartTime.Unix() > tlsrv.EndTime.Unix() {
		effDuration = 0
		return
	}else{
		if tlsrv.EffFactor <= 0 {
			tlsrv.EffFactor = 1.0
		}
		seconds 		= tlsrv.EndTime.Sub(tlsrv.StartTime).Seconds() * tlsrv.EffFactor
		effDuration	= time.Duration(seconds) * time.Second
		return
	}
}

// 基于线段的起始时间和持续时间计算在该线段上的结束时间
func (tlsrv *TimeLineSrv)GetEndTimeByDuration(duration time.Duration)(endTime time.Time){
	var seconds		float64

	if tlsrv.Duration <= duration{
		endTime = tlsrv.EndTime
		return
	}else{
		if tlsrv.EffFactor <= 0 {
			tlsrv.EffFactor = 1.0
		}
		seconds = duration.Seconds() / tlsrv.EffFactor
		endTime = tlsrv.EndTime.Add(-time.Duration(seconds) * time.Second)
		return
	}
	return
}

// 基于落在线段内的起始时间计算到线段结束的Duration
func (tlsrv *TimeLineSrv)GetDurationByStartTime(startTime time.Time)(duration time.Duration){
	var seconds		float64
	if startTime.Unix() >= tlsrv.EndTime.Unix(){
		duration = 0
		return
	}
	if startTime.Unix() < tlsrv.StartTime.Unix(){
		duration = tlsrv.Duration
		return
	}
	seconds = tlsrv.EndTime.Sub(startTime).Seconds() * tlsrv.EffFactor
	duration = time.Duration(seconds) * time.Second
	return
}

// 基于落在线段的起始时间计算到指定结束时间的Duration
func (tlsrv *TimeLineSrv)GetDurationByEndTime(endTime time.Time)(duration time.Duration){
	var seconds		float64
	if endTime.Unix() <= tlsrv.StartTime.Unix(){
		duration = 0
		return
	}
	if endTime.Unix() >= tlsrv.EndTime.Unix(){
		duration = tlsrv.Duration
		return
	}
	seconds = endTime.Sub(tlsrv.StartTime).Seconds() * tlsrv.EffFactor
	duration = time.Duration(seconds) * time.Second
	return
}

// 获取线段上两个时间点之间的Duration
func (tlsrv *TimeLineSrv)GetDurationByStartEndTime(startTime, endTime time.Time)(duration time.Duration){
	var seconds		float64
	if startTime.Unix() < tlsrv.StartTime.Unix(){
		startTime = tlsrv.StartTime
	}
	if endTime.Unix() > tlsrv.EndTime.Unix(){
		endTime = tlsrv.EndTime
	}
	if endTime.Unix() <= startTime.Unix(){
		duration = 0
		return
	}

	seconds = endTime.Sub(startTime).Seconds() * tlsrv.EffFactor
	duration = time.Duration(seconds) * time.Second
	return
}