package dbc_test

import (
	"encoding/base64"
	"fmt"
	"testing"

	fm29_frs90 "github.com/fiskerinc/cloud-services/pkg/dbc/fm29_frs90"
	fm29_frsd0 "github.com/fiskerinc/cloud-services/pkg/dbc/fm29_frsd0"
	fm29_frsd21 "github.com/fiskerinc/cloud-services/pkg/dbc/fm29_frsd21"
	fm29_frsd390 "github.com/fiskerinc/cloud-services/pkg/dbc/fm29_frsd390"
	n60 "github.com/fiskerinc/cloud-services/pkg/dbc/n60"
	"github.com/fiskerinc/cloud-services/pkg/testhelper"

	can "github.com/fiskerinc/cloud-services/pkg/can-go"
	"github.com/fiskerinc/cloud-services/pkg/can-go/pkg/descriptor"
)

// For generating test data

type CANMsg interface {
	Descriptor() *descriptor.Message
	Frame() can.Frame
}

func displayData(msg CANMsg) {
	fmt.Println(msg.Descriptor().ID, encodeData(msg.Frame().Data))
}

func encodeData(data can.Data) string {
	return base64.StdEncoding.EncodeToString([]byte(data[:]))
}

func TestDBCHash(t *testing.T) {
	const fm29_frs90_hash = "0e1f0e4eb01cfdd795f7f539d6baccc85770846050a83f5ac2f3ce75ea6453a0"
	const fm29_frsd0_hash = "386c18977a1be3cda60c953e5902c680dbe82b89523f2527e80cd9db863db991"
	const fm29_frsd21_hash = "d439abd3662dd20099f49dd8f43f7b145202e961caa2b5aba2c6154c8096348b"
	const fm29_frsd390_hash = "1c9d097ab570b1f28d447f947ed9f87aeaba3e4386db02ffdfb698bb1c08808b"
	const n60_hash = "556fe1385c2bfcbdc89bfa1fda7b580e594aa2ae3267cbe6ec9668a593755e59"

	if fm29_frsd21.Hash != fm29_frsd21_hash {
		t.Errorf(testhelper.TestErrorTemplate, "fm29_frsd21 hash", fm29_frsd21_hash, fm29_frsd21.Hash)
	}

	if fm29_frs90.Hash != fm29_frs90_hash {
		t.Errorf(testhelper.TestErrorTemplate, "fm29_frs90 hash", fm29_frs90_hash, fm29_frs90.Hash)
	}

	if fm29_frsd0.Hash != fm29_frsd0_hash {
		t.Errorf(testhelper.TestErrorTemplate, "fm29_frsd0 hash", fm29_frsd0_hash, fm29_frsd0.Hash)
	}

	if fm29_frsd390.Hash != fm29_frsd390_hash {
		t.Errorf(testhelper.TestErrorTemplate, "fm29_frsd390 hash", fm29_frsd390_hash, fm29_frsd390.Hash)
	}

	if n60.Hash != n60_hash {
		t.Errorf(testhelper.TestErrorTemplate, "n60 hash", n60_hash, n60.Hash)
	}
}
func TestGenerateSignalsFM29_FRSD0(t *testing.T) {
	t.Skip()
	var msg CANMsg

	msg = func() CANMsg {
		x := fm29_frsd0.NewVCU_0x260()
		x.SetVCU_VehChrgDchgMod(1)
		x.SetVCU_260_SSecOC_Fresh_Byte0(1)
		x.SetVCU_260_SSecOC_MAC_Byte0(1)
		x.SetVCU_260_SSecOC_MAC_Byte1(1)
		x.SetVCU_260_SSecOC_MAC_Byte2(1)
		x.SetVCU_ChrgSts(10)
		x.SetVCU_ChrgSysOperCmd(10)
		x.SetVCU_DCCrtCmd_OBC(10)
		x.SetVCU_DCVoltCmd_OBC(5)
		return x
	}()
	displayData(msg)

	msg = func() CANMsg {
		x := fm29_frsd0.NewBMS_SOC()
		x.SetBMS_Bat_SoC_usable(10)
		x.SetBMS_Bat_SOH(20)
		return x
	}()
	displayData(msg)

	msg = func() CANMsg {
		x := fm29_frsd0.NewBCM_0x321()
		x.SetBCM_AP_FL_LeFrntWinPosnInfo(10)
		x.SetBCM_AP_FL_RiFrntWinPosnInfo(20)
		x.SetBCM_AP_FL_LeReWinPosnInfo(30)
		x.SetBCM_AP_FL_RiReWinPosnInfo(40)
		return x
	}()
	displayData(msg)

	msg = func() CANMsg {
		x := fm29_frsd0.NewBMS_HVBatt_Status_2_TBOX()
		x.SetBMS_PwrBattRmngCpSOC(50)
		return x
	}()
	displayData(msg)

	msg = func() CANMsg {
		x := fm29_frsd0.NewBCM_0x335()
		x.SetBCM_ReDefrstHeatgCmd(true)
		return x
	}()
	displayData(msg)

	msg = func() CANMsg {
		x := fm29_frsd0.NewBCM_0x343()
		x.SetBCM_FrntHoodLidSts(true)
		x.SetBCM_FrntDrDoorLockSts(true)
		x.SetBCM_FrntDrDoorLockSts(true)
		x.SetBCM_CenLockSwtSts(2)
		x.SetBCM_RiReDoorSts(true)
		x.SetBCM_LeReDoorSts(true)
		x.SetBCM_SunroofPosnInfo(50)
		return x
	}()
	displayData(msg)

	msg = func() CANMsg {
		x := fm29_frsd0.NewBCM_0x369()
		x.SetBCM_AP_TL_LeReWinPosnInfo(60)
		x.SetBCM_AP_TL_RiReWinPosnInfo(70)
		x.SetBCM_AP_RW_WinPosnInfo(80)
		return x
	}()
	displayData(msg)

	msg = func() CANMsg {
		x := fm29_frsd0.NewBMS_CellTemp_CoolingHeating()
		x.SetBMS_BattAvrgT(90)
		return x
	}()
	displayData(msg)

	msg = func() CANMsg {
		x := fm29_frsd0.NewECC_0x373()
		x.SetECC_OutdT(30)
		return x
	}()
	displayData(msg)

	msg = func() CANMsg {
		x := fm29_frsd0.NewBCM_0x386()
		x.SetBCM_HeatedSteerWhlSt(1)
		return x
	}()
	displayData(msg)

	msg = func() CANMsg {
		x := fm29_frsd0.NewVCU_0x504()
		x.SetVCU_DrvgMilg(1234)
		return x
	}()
	displayData(msg)

	msg = func() CANMsg {
		x := fm29_frsd0.NewPSM_0x512()
		x.SetPSM_PassSeatHeatgSts(4)
		return x
	}()
	displayData(msg)

	msg = func() CANMsg {
		x := fm29_frsd0.NewDSMC_0x518()
		x.SetDSMC_DrvrSeatHeatgSts(2)
		return x
	}()
	displayData(msg)

	msg = func() CANMsg {
		x := fm29_frsd0.NewICC_0x531()
		x.SetICC_TotMilg_ODO(2345)
		return x
	}()
	displayData(msg)

	msg = func() CANMsg {
		x := fm29_frsd0.NewECC_0x582()
		x.SetECC_InsdT(30)
		x.SetECC_RemTSetSts(120)
		return x
	}()
	displayData(msg)

	msg = func() CANMsg {
		x := fm29_frsd0.NewVCU_0x580()
		x.SetVCU_DCChrgRmngTi(5000)
		x.SetVCU_ActChrgTotAh(2)
		x.SetVCU_ActDchgTotAh(3)
		x.SetVCU_PwrBattAvlEgy(4)
		return x
	}()
	displayData(msg)

	msg = func() CANMsg {
		x := fm29_frsd0.NewTBOX_0x525()
		x.SetTBOX_GPSHei(16)
		x.SetTBOX_525_AliveCounter(5)
		x.SetTBOX_525_CheckSum(100)
		return x
	}()
	displayData(msg)

	msg = func() CANMsg {
		x := fm29_frsd0.NewTBOX_0x526()
		x.SetTBOX_GPSLongi(-120.397)
		x.SetTBOX_GPSLati(35.832)
		return x
	}()
	displayData(msg)

	t.Error("done")
}

func TestGenerateSignalsFM29_FRS90(t *testing.T) {
	t.Skip()
	var msg CANMsg

	msg = func() CANMsg {
		x := fm29_frs90.NewVCU_0x260()
		x.SetVCU_VehChrgDchgMod(1)
		x.SetVCU_260_SSecOC_Fresh_Byte0(1)
		x.SetVCU_260_SSecOC_MAC_Byte0(1)
		x.SetVCU_260_SSecOC_MAC_Byte1(1)
		x.SetVCU_260_SSecOC_MAC_Byte2(1)
		x.SetVCU_ChrgSts(10)
		x.SetVCU_ChrgSysOperCmd(10)
		x.SetVCU_DCCrtCmd_OBC(10)
		x.SetVCU_DCDCPwrLimCmd(10)
		x.SetVCU_DCPosRlyCtrlCmd(true)
		x.SetVCU_DCVoltCmd_OBC(5)
		return x
	}()
	displayData(msg)

	msg = func() CANMsg {
		x := fm29_frs90.NewBMS_SOC()
		x.SetBMS_Bat_SoC_usable(10)
		x.SetBMS_Bat_SOH(20)
		return x
	}()
	displayData(msg)

	msg = func() CANMsg {
		x := fm29_frs90.NewBCM_0x321()
		x.SetBCM_AP_FL_LeFrntWinPosnInfo(10)
		x.SetBCM_AP_FL_RiFrntWinPosnInfo(20)
		x.SetBCM_AP_FL_LeReWinPosnInfo(30)
		x.SetBCM_AP_FL_RiReWinPosnInfo(40)
		return x
	}()
	displayData(msg)

	msg = func() CANMsg {
		x := fm29_frs90.NewBMS_HVBatt_Status_2_TBOX()
		x.SetBMS_PwrBattRmngCpSOC(50)
		return x
	}()
	displayData(msg)

	msg = func() CANMsg {
		x := fm29_frs90.NewBCM_0x335()
		x.SetBCM_ReDefrstHeatgCmd(true)
		return x
	}()
	displayData(msg)

	msg = func() CANMsg {
		x := fm29_frs90.NewBCM_0x343()
		x.SetBCM_FrntHoodLidSts(true)
		x.SetBCM_FrntDrDoorLockSts(true)
		x.SetBCM_FrntDrDoorLockSts(true)
		x.SetBCM_CenLockSwtSts(2)
		x.SetBCM_RiReDoorSts(true)
		x.SetBCM_LeReDoorSts(true)
		x.SetBCM_SunroofPosnInfo(50)
		return x
	}()
	displayData(msg)

	msg = func() CANMsg {
		x := fm29_frs90.NewBCM_0x369()
		x.SetBCM_AP_TL_LeReWinPosnInfo(60)
		x.SetBCM_AP_TL_RiReWinPosnInfo(70)
		x.SetBCM_AP_RW_WinPosnInfo(80)
		return x
	}()
	displayData(msg)

	msg = func() CANMsg {
		x := fm29_frs90.NewBMS_CellTemp_CoolingHeating()
		x.SetBMS_BattAvrgT(90)
		return x
	}()
	displayData(msg)

	msg = func() CANMsg {
		x := fm29_frs90.NewECC_0x373()
		x.SetECC_OutdT(30)
		return x
	}()
	displayData(msg)

	msg = func() CANMsg {
		x := fm29_frs90.NewBCM_0x386()
		x.SetBCM_HeatedSteerWhlSt(1)
		return x
	}()
	displayData(msg)

	msg = func() CANMsg {
		x := fm29_frs90.NewVCU_0x504()
		x.SetVCU_DrvgMilg(1234)
		return x
	}()
	displayData(msg)

	msg = func() CANMsg {
		x := fm29_frs90.NewPSM_0x512()
		x.SetPSM_PassSeatHeatgSts(4)
		return x
	}()
	displayData(msg)

	msg = func() CANMsg {
		x := fm29_frs90.NewDSMC_0x518()
		x.SetDSMC_DrvrSeatHeatgSts(2)
		return x
	}()
	displayData(msg)

	msg = func() CANMsg {
		x := fm29_frs90.NewICC_0x531()
		x.SetICC_TotMilg_ODO(2345)
		return x
	}()
	displayData(msg)

	msg = func() CANMsg {
		x := fm29_frs90.NewECC_0x582()
		x.SetECC_InsdT(30)
		x.SetECC_RemTSetSts(120)
		return x
	}()
	displayData(msg)

	msg = func() CANMsg {
		x := fm29_frs90.NewVCU_0x580()
		x.SetVCU_DCChrgRmngTi(5000)
		return x
	}()
	displayData(msg)

	msg = func() CANMsg {
		x := fm29_frs90.NewTBOX_0x525()
		x.SetTBOX_GPSHei(16)
		x.SetTBOX_GPSLongi(-120.397)
		x.SetTBOX_GPSLati(35.832)
		return x
	}()
	displayData(msg)

	t.Error("done")
}
