package schedule

import (
	model "LAPP_LF_MOM_BACKEND/models/base"
	"time"
)

type DayModelSrv struct {
	DayModelNr  int
	Duration    time.Duration
	DayModelTab model.DayModel
	LineArray   []LineSegementSrv
	StartTime   time.Time
	EndTime     time.Time
}

// 该日模型的有效时长
func (dms *DayModelSrv) GetDuration() {
	var (
		lssrv LineSegementSrv
	)

	dms.Duration = 0
	for _, lssrv = range dms.LineArray {
		dms.Duration = dms.Duration + lssrv.GetDuration()
	}
}

// 日模型对象初始化,将日模型的班次时间转化成线段数组，并计算日模型的Duration
func (dms *DayModelSrv) Init(dmtab model.DayModel) {
	var (
		lssrv          LineSegementSrv
		nextStartPoint int
	)

	// 初始化
	dms.DayModelTab = dmtab
	dms.DayModelNr = dms.DayModelTab.DayModelNr
	dms.LineArray = []LineSegementSrv{}
	nextStartPoint = dms.DayModelTab.S1Beg
	// 班次1
	if dms.DayModelTab.S1Toggle {
		if dms.DayModelTab.S1B1End > 0 && dms.DayModelTab.S1B1End > dms.DayModelTab.S1B1Beg {
			lssrv = LineSegementSrv{}
			lssrv.StartSecond = nextStartPoint
			lssrv.EndSecond = dms.DayModelTab.S1B1Beg
			lssrv.GetDuration()
			nextStartPoint = dms.DayModelTab.S1B1End
			dms.LineArray = append(dms.LineArray, lssrv)
		}
		if dms.DayModelTab.S1B2End > 0 && dms.DayModelTab.S1B2End > dms.DayModelTab.S1B2Beg {
			lssrv = LineSegementSrv{}
			lssrv.StartSecond = nextStartPoint
			lssrv.EndSecond = dms.DayModelTab.S1B2Beg
			lssrv.GetDuration()
			nextStartPoint = dms.DayModelTab.S1B2End
			dms.LineArray = append(dms.LineArray, lssrv)
		}
		if dms.DayModelTab.S1B3End > 0 && dms.DayModelTab.S1B3End > dms.DayModelTab.S1B3Beg {
			lssrv = LineSegementSrv{}
			lssrv.StartSecond = nextStartPoint
			lssrv.EndSecond = dms.DayModelTab.S1B3Beg
			lssrv.GetDuration()
			nextStartPoint = dms.DayModelTab.S1B3End
			dms.LineArray = append(dms.LineArray, lssrv)
		}
		if dms.DayModelTab.S1B4End > 0 && dms.DayModelTab.S1B4End > dms.DayModelTab.S1B4Beg {
			lssrv = LineSegementSrv{}
			lssrv.StartSecond = nextStartPoint
			lssrv.EndSecond = dms.DayModelTab.S1B4Beg
			lssrv.GetDuration()
			nextStartPoint = dms.DayModelTab.S1B4End
			dms.LineArray = append(dms.LineArray, lssrv)
		}
		if dms.DayModelTab.S1B5End > 0 && dms.DayModelTab.S1B5End > dms.DayModelTab.S1B5Beg {
			lssrv = LineSegementSrv{}
			lssrv.StartSecond = nextStartPoint
			lssrv.EndSecond = dms.DayModelTab.S1B5Beg
			lssrv.GetDuration()
			nextStartPoint = dms.DayModelTab.S1B5End
			dms.LineArray = append(dms.LineArray, lssrv)
		}
		lssrv = LineSegementSrv{}
		lssrv.StartSecond = nextStartPoint
		lssrv.EndSecond = dms.DayModelTab.S1End
		lssrv.GetDuration()
		dms.LineArray = append(dms.LineArray, lssrv)
	}

	nextStartPoint = dms.DayModelTab.S2Beg
	// 班次2
	if dms.DayModelTab.S2Toggle {
		if dms.DayModelTab.S2B1End > 0 && dms.DayModelTab.S2B1End > dms.DayModelTab.S2B1Beg {
			lssrv = LineSegementSrv{}
			lssrv.StartSecond = nextStartPoint
			lssrv.EndSecond = dms.DayModelTab.S2B1Beg
			lssrv.GetDuration()
			nextStartPoint = dms.DayModelTab.S2B1End
			dms.LineArray = append(dms.LineArray, lssrv)
		}
		if dms.DayModelTab.S2B2End > 0 && dms.DayModelTab.S2B2End > dms.DayModelTab.S2B2Beg {
			lssrv = LineSegementSrv{}
			lssrv.StartSecond = nextStartPoint
			lssrv.EndSecond = dms.DayModelTab.S2B2Beg
			lssrv.GetDuration()
			nextStartPoint = dms.DayModelTab.S2B2End
			dms.LineArray = append(dms.LineArray, lssrv)
		}
		if dms.DayModelTab.S2B3End > 0 && dms.DayModelTab.S2B3End > dms.DayModelTab.S2B3Beg {
			lssrv = LineSegementSrv{}
			lssrv.StartSecond = nextStartPoint
			lssrv.EndSecond = dms.DayModelTab.S2B3Beg
			lssrv.GetDuration()
			nextStartPoint = dms.DayModelTab.S2B3End
			dms.LineArray = append(dms.LineArray, lssrv)
		}
		if dms.DayModelTab.S2B4End > 0 && dms.DayModelTab.S2B4End > dms.DayModelTab.S2B4Beg {
			lssrv = LineSegementSrv{}
			lssrv.StartSecond = nextStartPoint
			lssrv.EndSecond = dms.DayModelTab.S2B4Beg
			lssrv.GetDuration()
			nextStartPoint = dms.DayModelTab.S2B4End
			dms.LineArray = append(dms.LineArray, lssrv)
		}
		if dms.DayModelTab.S2B5End > 0 && dms.DayModelTab.S2B5End > dms.DayModelTab.S2B5Beg {
			lssrv = LineSegementSrv{}
			lssrv.StartSecond = nextStartPoint
			lssrv.EndSecond = dms.DayModelTab.S2B5Beg
			lssrv.GetDuration()
			nextStartPoint = dms.DayModelTab.S2B5End
			dms.LineArray = append(dms.LineArray, lssrv)
		}
		lssrv = LineSegementSrv{}
		lssrv.StartSecond = nextStartPoint
		lssrv.EndSecond = dms.DayModelTab.S2End
		lssrv.GetDuration()
		dms.LineArray = append(dms.LineArray, lssrv)
	}

	nextStartPoint = dms.DayModelTab.S3Beg
	// 班次2
	if dms.DayModelTab.S3Toggle {
		if dms.DayModelTab.S3B1End > 0 && dms.DayModelTab.S3B1End > dms.DayModelTab.S3B1Beg {
			lssrv = LineSegementSrv{}
			lssrv.StartSecond = nextStartPoint
			lssrv.EndSecond = dms.DayModelTab.S3B1Beg
			lssrv.GetDuration()
			nextStartPoint = dms.DayModelTab.S3B1End
			dms.LineArray = append(dms.LineArray, lssrv)
		}
		if dms.DayModelTab.S3B2End > 0 && dms.DayModelTab.S3B2End > dms.DayModelTab.S3B2Beg {
			lssrv = LineSegementSrv{}
			lssrv.StartSecond = nextStartPoint
			lssrv.EndSecond = dms.DayModelTab.S3B2Beg
			lssrv.GetDuration()
			nextStartPoint = dms.DayModelTab.S3B2End
			dms.LineArray = append(dms.LineArray, lssrv)
		}
		if dms.DayModelTab.S3B3End > 0 && dms.DayModelTab.S3B3End > dms.DayModelTab.S3B3Beg {
			lssrv = LineSegementSrv{}
			lssrv.StartSecond = nextStartPoint
			lssrv.EndSecond = dms.DayModelTab.S3B3Beg
			lssrv.GetDuration()
			nextStartPoint = dms.DayModelTab.S3B3End
			dms.LineArray = append(dms.LineArray, lssrv)
		}
		if dms.DayModelTab.S3B4End > 0 && dms.DayModelTab.S3B4End > dms.DayModelTab.S3B4Beg {
			lssrv = LineSegementSrv{}
			lssrv.StartSecond = nextStartPoint
			lssrv.EndSecond = dms.DayModelTab.S3B4Beg
			lssrv.GetDuration()
			nextStartPoint = dms.DayModelTab.S3B4End
			dms.LineArray = append(dms.LineArray, lssrv)
		}
		if dms.DayModelTab.S3B5End > 0 && dms.DayModelTab.S3B5End > dms.DayModelTab.S3B5Beg {
			lssrv = LineSegementSrv{}
			lssrv.StartSecond = nextStartPoint
			lssrv.EndSecond = dms.DayModelTab.S3B5Beg
			lssrv.GetDuration()
			nextStartPoint = dms.DayModelTab.S3B5End
			dms.LineArray = append(dms.LineArray, lssrv)
		}
		lssrv = LineSegementSrv{}
		lssrv.StartSecond = nextStartPoint
		lssrv.EndSecond = dms.DayModelTab.S3End
		lssrv.GetDuration()
		dms.LineArray = append(dms.LineArray, lssrv)
	}
}
