/* Find out which vehicle produced the data and list its journey history */
func lookupVehicleHistory(line, bound, origin string, departureTime time.Time) (string, []VehicleRecord) {
	vehicleId, arrivalTime, err := selectdb.SelectVehicleAndArrivalTimeFromPerformanceMeasure(line, bound, origin, departureTime)
	if err != nil {
		logger.GetLogger().Panic(err)
	}
	startTime := departureTime
	endTime := arrivalTime

	vehicleHistory := selectdb.SelectVehicleHistory(vehicleId, startTime, endTime, timeFormatString)

	result := make([]VehicleRecord, len(vehicleHistory))
	for i, v := range vehicleHistory {
		result[i] = VehicleRecord{
			Line:        v["line"],
			Bound:       v["bound"],
			StopSeq:     v["stopSeq"],
			StopName:    v["stopName"],
			ArrivalTime: v["time"],
			NaptanId:    v["naptanId"],
		}
	}
	return vehicleId, result
}
/* Find out which vehicle produced the data and list its journey history, together with
   the total actual journey time, total predictions and total standard deviations */
func lookupVehicleHistory2(line, bound, origin, destination string, departureTime time.Time, errorMessage *string) (string, []VehicleRecord2, time.Duration, map[performanceeval.Method]time.Duration, map[performanceeval.Method]time.Duration) {
	vehicleId, arrivalTime, err := selectdb.SelectVehicleAndArrivalTimeFromJourneyHistory(line, bound, origin, destination, departureTime)
	if err != nil {
		logger.GetLogger().Panic(err)
	}
	startTime := departureTime
	endTime := arrivalTime

	vehicleHistory := selectdb.SelectVehicleHistory(vehicleId, startTime, endTime, timeFormatString)

	rawArrivalTimes := selectdb.SelectRawArrivalTimes(vehicleId, startTime, endTime, timeFormatString)

	intermediateStopsOrdered := selectdb.SelectIntermediateStops(line, bound, origin, destination)
	intermediateStops := make(map[string]map[string]bool)
	for i := 0; i < len(intermediateStopsOrdered)-1; i++ {
		intermediateStops[intermediateStopsOrdered[i]] = map[string]bool{
			intermediateStopsOrdered[i+1]: true,
		}
	}

	/* Temporarily discarded */
	// predictionsUsedRealTime := make(map[string]map[string]map[time.Time]map[string]int64)

	totalPredictions := make(map[performanceeval.Method]time.Duration)
	totalStddevs := make(map[performanceeval.Method]time.Duration)
	idvPredictions := make(map[performanceeval.Method]map[string]map[string]time.Duration)
	idvStddevs := make(map[performanceeval.Method]map[string]map[string]time.Duration)
	idvExpectedArrivalTimesShifting := make(map[performanceeval.Method]map[string]map[string]time.Time)
	idvMixedTypes := make(map[performanceeval.Method]map[string]map[string]string)

	for _, method := range performanceeval.Methods() {
		totalPrediction, totalStddev, idvPredictionsMap, idvStddevsMap, edtShifting, mixedTypes := lookupPredictionsUsed(line, bound, intermediateStops, intermediateStopsOrdered, departureTime,
			method, nil, errorMessage)

		totalPredictions[method] = totalPrediction
		totalStddevs[method] = totalStddev
		idvPredictions[method] = idvPredictionsMap
		idvStddevs[method] = idvStddevsMap
		if method.IsShiftingBased() {
			idvExpectedArrivalTimesShifting[method] = edtShifting
		}
		if method.IsMixedBased() {
			idvMixedTypes[method] = mixedTypes
		}

	}

	actualJourneyTime := performanceeval.FetchActualJourneyTime(line, bound, origin, destination, departureTime)

	/* Flag for use later, in case bus switches direction invalidate and prevent travel time details from further generated */
	firstTimeFlag := true

	result := make([]VehicleRecord2, len(vehicleHistory))
	for i, v := range vehicleHistory {
		result[i] = VehicleRecord2{
			Line:           v["line"],
			Bound:          v["bound"],
			StopSeq:        v["stopSeq"],
			StopName:       v["stopName"],
			ArrivalTime:    v["time"],
			RawArrivalTime: rawArrivalTimes[v["naptanId"]],
			NaptanId:       v["naptanId"],
			Details: generateTravelTimeDetails(line, bound, intermediateStops, departureTime, vehicleHistory, i,
				idvPredictions, idvStddevs, idvExpectedArrivalTimesShifting, idvMixedTypes, &firstTimeFlag),
		}
	}

	if !firstTimeFlag {
		*errorMessage += "Bus terminated early\n"
	}

	/*predictions := []time.Duration{
		actualJourneyTime,
		predictionThreeDays,
		predictionWeekly,
		predictionWeeklyDiscardInterpolation,
		predictionWeeklySameRouteOnly,
		predictionWeeklyShifting,
		predictionWeeklyShiftingDiscardInterpolation,
		predictionWeeklyShiftingSameRouteOnly,
		predictionRealTimeMedian,
		predictionRealTimeLastTwo,

		totalStddevThreeDays,
		totalStddevWeekly,
		totalStddevWeeklyDiscardInterpolation,
		totalStddevWeeklySameRouteOnly,
		totalStddevWeeklyShifting,
		totalStddevWeeklyShiftingDiscardInterpolation,
		totalStddevWeeklyShiftingSameRouteOnly,
		totalStddevRealTimeMedian,
		totalStddevRealTimeLastTwo,
	}*/

	return vehicleId, result, actualJourneyTime, totalPredictions, totalStddevs
}