package cachev2_test

import (
	"fmt"
	"testing"
	"time"

	cache "github.com/fiskerinc/cloud-services/pkg/cachev2"
	"github.com/fiskerinc/cloud-services/pkg/common"
	redis "github.com/fiskerinc/cloud-services/pkg/redisv2"
	"github.com/go-redis/redismock/v9"
	"github.com/stretchr/testify/assert"
)

//HERE

func TestConnGetVehicleState(t *testing.T) {
	var updateTime = time.Date(2020, time.October, 3, 12, 10, 0, 0, time.UTC)
	vin := "TESTVIN123"
	// redisMock := tester.NewRedisMock()
	// redisPool := tester.NewMockClientPool(redisMock)
	redisClientFakeConnection, clientMock := redismock.NewClientMock()
	_ = clientMock
	redisClient := redis.NewClient(redisClientFakeConnection)

	testCases := map[string]struct {
		sismemberResults map[string]map[string]interface{}
		hgetallResults   map[string][]interface{}
		expResp          common.CarState
		expErr           error
	}{
		"correct": {
			sismemberResults: map[string]map[string]interface{}{
				redis.CarSessionsKey(): {
					vin: int64(1),
				},
				redis.HMISessionsKey(): {
					vin: int64(1),
				},
			},
			hgetallResults: map[string][]interface{}{
				fmt.Sprintf("car:%s:state", vin): {
					[]byte("DSMC_DrvrSeatHeatgSts"), []byte("2"),
					[]byte("ESP_VehSpd"), []byte("123.4"),
					[]byte("BMS_RmChrgTi_TrgtSoC"), []byte("5000"),
					[]byte("BMS_RmChrgTi_FullChrg"), []byte("6000"),
					[]byte("VCU_VehChrgDchgMod"), []byte("DC_charging"),
					[]byte("BCM_AP_FL_LeReWinPosnInfo"), []byte("30"),
					[]byte("BCM_ReDefrstHeatgCmd"), []byte("1"),
					[]byte("BCM_FrntHoodLidSts"), []byte("1"),
					[]byte("BMS_Bat_SOH"), []byte("20"),
					[]byte("ICC_TotMilg_ODO"), []byte("2345"),
					[]byte("IBS_BatteryVoltage"), []byte("12.3"),
					[]byte("TBOX_GPSHei"), []byte("16"),
					[]byte("ECC_OutdT"), []byte("30"),
					[]byte("PSM_PassSeatHeatgSts"), []byte("4"),
					[]byte("TBOX_GPSLati"), []byte("35.831"),
					[]byte("BCM_PasFrntDoorSts"), []byte("0"),
					[]byte("BCM_CenLockSwtSts"), []byte("3"),
					[]byte("BCM_RiReDoorSts"), []byte("1"),
					[]byte("BCM_LeReDoorSts"), []byte("1"),
					[]byte("VCU_DrvgMilg"), []byte("1234"),
					[]byte("TBOX_GPSLongi"), []byte("-120.398"),
					[]byte("BCM_AP_FL_RiReWinPosnInfo"), []byte("40"),
					[]byte("BCM_FrntDrDoorLockSts"), []byte("1"),
					[]byte("BCM_DrFrntDoorSts"), []byte("0"),
					[]byte("BCM_AP_TL_LeReWinPosnInfo"), []byte("60"),
					[]byte("ECC_RemTSetSts"), []byte("120"),
					[]byte("BCM_AP_FL_RiFrntWinPosnInfo"), []byte("20"),
					[]byte("BMS_PwrBattRmngCpSOC"), []byte("50"),
					[]byte("BCM_AP_TL_RiReWinPosnInfo"), []byte("70"),
					[]byte("BCM_HeatedSteerWhlSt"), []byte("1"),
					[]byte("BCM_AP_RW_WinPosnInfo"), []byte("80"),
					[]byte("ECC_InsdT"), []byte("30"),
					[]byte("updated"), []byte(`"2020-10-03T12:10:00Z"`),
					[]byte("BMS_Bat_SoC_usable"), []byte("10"),
					[]byte("BCM_AP_FL_LeFrntWinPosnInfo"), []byte("10"),
					[]byte("BCM_SunroofPosnInfo"), []byte("50"),
					[]byte("BMS_BattAvrgT"), []byte("90"),
					[]byte("dbc_version"), []byte("hash"),
					[]byte("VCU_VehSt"), []byte("12"),
					[]byte("VCU_VcuState"), []byte("18"),
					[]byte("MCU_F_ActSafeSt"), []byte("4"),
					[]byte("MCU_R_ActSafeSt"), []byte("2"),
					[]byte("MCU_R_Decoup_State"), []byte("3"),
					[]byte("MCU_F_CrtMod"), []byte("7"),
					[]byte("MCU_R_CrtMod"), []byte("8"),
					[]byte("VCU_RdyLamp"), []byte("1"),
				},
			},
			expResp: common.CarState{
				Online:    true,
				OnlineHMI: true,
				VehicleSpeed: &common.VehicleSpeed{
					Speed: 123.4,
				},
				Battery: &common.Battery{
					Percent:              50,
					TotalMileageOdometer: 2345,
					BatteryVoltage:       12.3,
				},
				MaxRange: &common.MaxRange{
					MaxMiles: 1234,
				},
				Doors: &common.Doors{
					Hood:       true,
					LeftFront:  false,
					LeftRear:   true,
					RightFront: false,
					RightRear:  true,
				},
				Location: &common.Location{
					Altitude:  16,
					Longitude: -120.398,
					Latitude:  35.831,
				},
				Locks: &common.Locks{
					Driver: false,
					All:    false,
				},
				Windows: &common.Windows{
					LeftFront:  10,
					LeftRear:   30,
					RightFront: 20,
					RightRear:  40,
				},
				MiscWindows: &common.MiscWindows{
					LeftRearQuarter:  60,
					RightRearQuarter: 70,
					RearWindshield:   80,
				},
				Sunroof: &common.Sunroof{
					Sunroof: 50,
				},
				CabinClimate: &common.CabinClimate{
					CabinTemperature:    120,
					InternalTemperature: 30,
				},
				RearDefrost: &common.RearDefrost{
					On: true,
				},
				DriverSeatHeat: &common.DriverSeatHeat{
					Level: 2,
				},
				PassengerSeatHeat: &common.PassengerSeatHeat{
					Level: 4,
				},
				CellTemperature: &common.CellTemperature{
					AvgBatteryTemp: 90,
				},
				ChargingMetrics: &common.VCUChargingMetrics{
					RemainingChargingTime:     5000,
					RemainingChargingTimeFull: 6000,
				},
				SteeringWheelHeat: &common.SteeringWheelHeat{
					On: true,
				},
				AmbientTemperature: &common.AmbientTemperature{
					Temperature: 30,
				},
				VCU0x260: &common.VCU0x260Descriptor{
					ChargeType: "DC_charging",
				},
				StateOfCharge: &common.StateOfCharge{
					Usable: 10,
					Health: 20,
				},
				DBCVersion: "hash",
				UpdatedAt:  &updateTime,
				SafeState: &common.SafeState{
					VehicleSafeState:      false,
					VCUSafeState:          true,
					MCUFrontSafeState:     false,
					MCURearSafeState:      true,
					MCURearDecoupState:    false,
					MCUFrontInverterError: true,
					MCURearInverterError:  false,
				},
				VehicleReadyState: &common.VehicleReadyState{
					IsVehicleReady: true,
				},
			},
			expErr: nil,
		},
	}

	parser := cache.NewVehicleState(redisClient)

	for tName, tt := range testCases {
		t.Run(tName, func(t *testing.T) {
			// clientMock.ExpectSIsMember()
			// redisMock.SISMEMBEResults = tt.sismemberResults
			// redisMock.HGETALLResults = tt.hgetallResults
			state, err := parser.Get(vin)
			assert.Equal(t, tt.expErr, err)
			assert.Equal(t, tt.expResp, state)
		})
	}
}
