package common

import (
	"strings"

	"github.com/fiskerinc/cloud-services/pkg/utils/envtool"
	"github.com/fiskerinc/cloud-services/pkg/utils/whereami"
)

var EcuMap = map[string]string{
	"AGS":       "Active Grille Shutter",
	"ADB":       "Adaptive Driving Beam",
	"ADAS":      "Advanced Driver Assist System",
	"ACU":       "Airbag Control Unit",
	"ACP":       "Airconditioning Control Panel",
	"AMP":       "Amplifier",
	"AP_FL":     "Anti-Pinch Front Left",
	"AP_FR":     "Anti-Pinch Front Right",
	"AP_RL":     "Anti-Pinch Rear Left",
	"AP_RR":     "Anti-Pinch Rear Right",
	"AL":        "Atmosphere Lamp",
	"BCS":       "Battery HV Current Sensor",
	"BMS":       "Battery Management System",
	"BMU":       "Battery Management Unit",
	"BCM":       "Body Control Module",
	"CDS":       "Center Display Screen",
	"CCU":       "Charging Control Unit",
	"CIM":       "Column Integrated Module",
	"CVM":       "Coolant Valve Module",
	"CFM":       "Cooling Fan Module",
	"CMRR_FL":   "Corner Mid Range Radar Front Left",
	"CMRR_FR":   "Corner Mid Range Radar Front Right",
	"CMRR_RL":   "Corner Mid Range Radar Rear Left",
	"CMRR_RR":   "Corner Mid Range Radar Rear Right",
	"DVRC":      "Digital Video Recorder Camera",
	"DC-CHM":    "Direct Current Charge Machine",
	"DMC":       "Driver Monitor Camera",
	"DSMC":      "Driver Seat Memory Controller",
	"DWSG":      "Driver Window Switch Group",
	"EPS":       "Electric Power Steering",
	"EPS1":      "Electric Power Steering Sub 1",
	"EPS2":      "Electric Power Steering Sub 2",
	"EAS":       "Electrical Air Compressor System",
	"ECC":       "Electrical Climate Controller",
	"EWP_B":     "Electrical Water Pump Battery",
	"EWP_FD":    "Electrical Water Pump Front Drive",
	"EWP_H":     "Electrical Water Pump Heat",
	"EWP_RD":    "Electrical Water Pump Rear Drive",
	"EWM":       "Electrical Wiper Motor",
	"EXV_B":     "Electronic Expansion Value Battery",
	"EXV_HP":    "Electronic Expansion Valve HPC",
	"ESP":       "Electronic Stability Program",
	"FDHA_FL":   "Flush Door Handle Actuator Front Left",
	"FDHA_FR":   "Flush Door Handle Actuator Front Right",
	"FDHA_RL":   "Flush Door Handle Actuator Rear Left",
	"FDHA_RR":   "Flush Door Handle Actuator Rear Right",
	"Lumber":    "Four-Way Lumber",
	"FBM_L":     "Front Beam Module Left",
	"FBM_R":     "Front Beam Module Right",
	"FCM":       "Front Camera Module",
	"GW":        "Gateway",
	"HUD":       "Head-Up Display",
	"IDS":       "Instrument Display Screen",
	"ICC":       "Integrated Cockpit Controller",
	"IBS":       "Intelligent Battery Sensor",
	"iBooster":  "Intelligent Booster",
	"KS":        "Kick Sensor",
	"LSC":       "Left Side Camera",
	"MRR":       "Mid Range Radar",
	"MCU":       "TBox Microcontroller",
	"MCU_F":     "Motor Control Unit Front",
	"MCU_R":     "Motor Control Unit Rear",
	"MDV":       "Motorized Deco-Vent",
	"MFS":       "Multifunction Steering",
	"MIS":       "Multimedia Interactive Switch",
	"MPC":       "Multipurpose Camera",
	"OBC":       "Onboard Charger",
	"OMC":       "Occupant Monitor Camera",
	"OHC":       "Overhead Console",
	"PAS":       "Parking Assistant System",
	"PCU":       "Parking Control Unit",
	"PMS":       "Particulate Matter Sensor",
	"PSM":       "Passenger Seat Module",
	"PEPS":      "Passive Entry And Passive Start",
	"PKC":       "Phone Key Controller",
	"PKC_ANT_L": "Phone Key Controller Antenna Left",
	"PKC_ANT_R": "Phone Key Controller Antenna Right",
	"PWC_L":     "Phone Wireless Charging Left",
	"PWC_R":     "Phone Wireless Charging Right",
	"PVIU":      "Photovoltaic Integration Unit",
	"PASC":      "Power Adjust Steering Column",
	"PDU":       "Power Distribution Unit",
	"PLGM":      "Power Lift Gate Module",
	"RLS":       "Rain Light Sensor",
	"RAC":       "Rear Airconditioning Control",
	"RVC":       "Rear View Camera",
	"RSC":       "Right Side Camera",
	"RCM":       "Roof Control Module",
	"RSM":       "Roof Shade Module",
	"SCM":       "Suspension Control Module",
	"TBOX":      "Telematics Box",
	"TPMS":      "Tire Pressure Monitoring System",
	"TDS":       "Touch Display Screen",
	"TRM":       "Trailer Module",
	"USB Box":   "USB Box",
	"VCU":       "Vehicle Control Unit",
	"VSP":       "Vehicle Sound For Pedestrians",
	"WTC_B":     "Water Thermal Controller Battery",
	"WTC_H":     "Water Thermal Controller Heat",
}

// Currently used by car configuration sender only
var CurrentECUMap = map[string]struct{}{
	"ACU": {}, "ADAS": {}, "AMP": {}, "BCM": {}, "BMS": {},
	"CIM": {}, "CMRR_FL": {}, "CMRR_FR": {}, "CMRR_RL": {}, "CMRR_RR": {},
	"DSMC": {}, "EAS": {}, "ECC": {}, "EPS1": {}, "EPS2": {},
	"ESP": {}, "FCM": {}, "GW": {}, "ICC": {}, "MCU_F": {},
	"MCU_R": {}, "MFS": {}, "MRR": {}, "OBC": {}, "OHC": {},
	"PKC": {}, "PLGM": {}, "PSM": {}, "PVIU": {}, "PWC_R": {},
	"PWC": {}, "RAC": {}, "SCM": {}, "TRM": {}, "MCU": {},
	"VCU": {}, "VSP": {}, "WTC_B": {}, "WTC_H": {}, "iBooster": {},
	"VOD": {}, // Although not officially a ECU, we don't want to delete it from the list of data we receive
}

// List of ECU's we want to filter out from our configuration
var FilterECUConfigurationMap = map[string]struct{}{}

func init() {
	BuildFilterECUConfigurationMap()
}

func BuildFilterECUConfigurationMap() {
	ecusToFilter := strings.Split(envtool.GetEnv("NO_CDS_ECUS", ""), ",")
	for _, str := range ecusToFilter {
		str = strings.TrimSpace(str)
		FilterECUConfigurationMap[str] = struct{}{}
	}
}

var ECUReplacement = func() map[string]string {
	switch whereami.Service {
	case whereami.OTA:
		return OTAUpdateECUReplacement
	case whereami.AFTERSALES:
		return AfterSalesECUReplacement
	default:
		return OTAUpdateECUReplacement
	}
}

var AfterSalesECUReplacement = map[string]string{}

var OTAUpdateECUReplacement = map[string]string{
	"EPS":   "EPS1",
	"PDU":   "OBC",
	"PWC_L": "PWC",
}

// An inverse of the otaUpdateECUReplacement. To map the cars definition to the pdx/odx definitions
// keep this updated
var CarDTCLookupInverse = map[string]string{
	"EPS1": "EPS",
	"OBC":  "PDU",
	"PWC":  "PWC_L",
}

var FlashpackCalculationECUReplacement = map[string]string{
	"MCU": "TBOX",
	"OBC": "PDU",
}
