package templates

import (
	"fmt"

	. "primesys_email/src/models/report"
	. "primesys_email/src/templates/utils"
)

func CreateSwmReportExcel(reports []DivisionTripReportModel, divisionName string, reportDate string) ([]byte, error) {
	t := tableReport{
		sheet:   "SWM Report",
		title:   "SWM REPORT",
		widths:  []float64{10, 28, 22, 22, 22, 16, 16, 16, 26},
		headers: []string{"Sr.No", "Name", "Expected Beat", "Expected Time", "OnTrack Time", "Actual KM", "Actual Trip", "Max Speed", "Start% - End% Battery"},
	}

	return writeTableReport(t, reports, divisionName, reportDate, func(r DivisionTripReportModel, s tableStyles) []tableCell {
		return []tableCell{
			{DefaultString(r.DeviceName), s.name},
			{kmRange(r.TripStartKm, r.TripEndKm, 3), s.cell},
			{timeRange(r.TripStartTime, r.TripEndTime), s.cell},
			{timeRange(r.ActualStartTime, r.ActualEndTime), s.cell},
			{DefaultFloat(r.DistanceCoverTrip), s.cell},
			{fmt.Sprint(len(r.TripList)), s.cell},
			{intText(r.TripMaxSpeed, "-"), s.cell},
			{batteryText(r.TripList, "- - -"), s.cell},
		}
	})
}

// CreateSwmLJNReportExcel is the SWM report of the LJN division, with working and idle hours
func CreateSwmLJNReportExcel(reports []DivisionTripReportModel, divisionName string, reportDate string) ([]byte, error) {
	t := tableReport{
		sheet:   "SWM Report",
		title:   "SWM REPORT",
		widths:  []float64{10, 28, 22, 16, 18, 16, 22, 16, 26},
		headers: []string{"Sr.No", "Name", "OnTrack Time", "Actual KM", "Total Working", "Total Idle", "Walking Distance", "Max Speed", "Start% - End% Battery"},
	}

	return writeTableReport(t, reports, divisionName, reportDate, workingIdleRow("- - -"))
}

// workingIdleRow is the row of the SWM LJN and Mopper reports, they differ only in the empty battery text
func workingIdleRow(emptyBattery string) func(r DivisionTripReportModel, s tableStyles) []tableCell {
	return func(r DivisionTripReportModel, s tableStyles) []tableCell {
		return []tableCell{
			{DefaultString(r.DeviceName), s.name},
			{timeRange(r.ActualStartTime, r.ActualEndTime), s.cell},
			{DefaultFloat(r.DistanceCoverTrip), s.cell},
			{totalWorking(r.TripList), s.cell},
			{totalIdle(r.TripList), s.cell},
			{DefaultFloat(r.WalkingDistance), s.cell},
			{intText(r.TripMaxSpeed, "-"), s.cell},
			{batteryText(r.TripList, emptyBattery), s.cell},
		}
	}
}
