package handlers

import (
	"net/http"
	"otaupdate/services"

	"github.com/fiskerinc/cloud-services/pkg/common"
	"github.com/fiskerinc/cloud-services/pkg/loggerdataresp"
	"github.com/fiskerinc/cloud-services/pkg/utils"
)

// HandleCarSoftwareInformationV2 godoc
// @Summary Get overall software version from a car and its ecu version information
// @Description Get overall software version from a car and its ecu version information
// @Accept  json
// @Produce  json
// @Param Authorization header string false "Bearer <ID token>"
// @Param Api-Key header string false "<API token>"
// @Param vin query string true "VIN"
// @Success 200 {object} CarSoftwareInformationResponseV2
// @Failure 400 {object} common.JSONError "Bad request"
// @Failure 401 {object} common.JSONError "Unauthorized"
// @Failure 503 {object} common.JSONError "Service unavailable"
// @Router /car/software_information/v2 [get]
func HandleCarSoftwareInformationV2(w http.ResponseWriter, r *http.Request) {
	qs := r.URL.Query()
	vin := qs.Get("vin")
	information, err := carSoftwareInformationV2(vin)
	if loggerdataresp.BadDataError(err) {
		return
	}
	utils.RespJSON(w, http.StatusOK, information)
}  

func carSoftwareInformationV2(vin string) (info CarSoftwareInformationResponseV2, err error) {
	info.VIN = vin
	var ecus []common.CarECU
	ecus, err = services.GetDB().GetCars().GetCarECUs(common.CarECUFilter{VIN: vin, Unique: true, ECUs: OVLoopECUList}, nil)
	if err != nil {
		return
	}
	info.ECUVersionInformation = convertCommonCarECUToECUVersionInformationArrayV2(ecus)
	var carVersion common.CarPKCOSVersion
	carVersion, err = services.GetDB().GetCars().GetSoftwareVersion(vin)
	info.OSVersion = carVersion.OSVersion
	info.SUMsVersion = carVersion.SumsVersion
	return
}

type CarSoftwareInformationResponseV2 struct {
	VIN                   string                  `json:"vin"`
	SUMsVersion           string                  `json:"sum_version"`
	OSVersion             string                  `json:"os_version"`
	ECUVersionInformation []ECUVersionInformationV2 `json:"ecu_version_information"`
}

type ECUVersionInformationV2 struct {
	ECU               string `json:"ecu"`
	SupplierSWVersion string `json:"supplier_sw_version,omitempty" validate:"max=1024"`
	BootLoaderVersion string `json:"boot_loader_version,omitempty" validate:"max=1024"`
}

func convertCommonCarECUToECUVersionInformationArrayV2(input []common.CarECU) (output []ECUVersionInformationV2) {
	output = make([]ECUVersionInformationV2, len(input))
	for index, ecuInfo := range input {
		output[index] = convertCommonCarECUToECUVersionInformationV2(ecuInfo)
	}
	// Probably want to sort the ECU's alphabetically
	return output
}

func convertCommonCarECUToECUVersionInformationV2(input common.CarECU) (output ECUVersionInformationV2) {
	output.ECU = input.ECU
	output.SupplierSWVersion = input.SupplierSWVersion
	output.BootLoaderVersion = input.BootLoaderVersion
	return output
}

var OVLoopECUList = []string{"ACU","ADAS","AMP","BCM","BMS","CIM","CMRR_FL","CMRR_FR","CMRR_RL","CMRR_RR","DSMC","ECC","EPS1","EPS2","ESP","FCM","GW","iBooster","ICC","MCU","MCU_F","MCU_R","MRR","OBC","OHC","PKC","PLGM","PSM","PVIU","PWC","PWC_R","TRM","VCU","VSP"}