package common_test

import (
	"testing"

	"github.com/fiskerinc/cloud-services/pkg/common"
	s "github.com/fiskerinc/cloud-services/pkg/common/carupdatestatus"
	"github.com/fiskerinc/cloud-services/pkg/testhelper"
)

func TestTRexTXMessages(t *testing.T) {
	schema := "file://" + testhelper.GetSchemaDirPath() + "/trex/TXMessage.json"

	tests := []testhelper.SchemaTest{
		{
			Name: "TestTRexCANBus",
			Object: common.Message{
				Handler: "canbus",
				Data: common.CANBusMessage{
					EpochUsec: 1653255445,
					Dropped:   10,
					Filtered:  20,
					Frames: []common.CANFrame{
						{
							TimestampUSec: 1234567890,
							ID:            1,
							Data:          "dGVzdA==",
						},
						{
							TimestampUSec: 9876543210,
							ID:            2,
							Data:          "abc",
						},
					},
				},
			},
		},
		{
			Name: "TestTRexCarUpdateDownload",
			Object: common.Message{
				Handler: "car_update_status",
				Data: common.CarUpdateProgress{
					CarUpdateID:    0,
					ECU:            "trex",
					FileCurrent:    1,
					FileTotal:      100,
					PackageCurrent: 2,
					PackageTotal:   3,
					Status:         s.Downloading,
					ErrorCode:      0,
				},
			},
		},
		{
			Name: "TestTRexCarUpdateInstall",
			Object: common.Message{
				Handler: "car_update_status",
				Data: common.CarUpdateProgress{
					CarUpdateID:    0,
					ECU:            "trex",
					InstalledFiles: 3,
					TotalFiles:     3,
					Status:         s.InstallSucceeded,
					ErrorCode:      0,
				},
			},
		},
		{
			Name: "TestTRexGetFileKey",
			Object: common.Message{
				Handler: "get_filekeys",
				Data: common.FileKeysRequest{
					FileIDs: []string{"testfileid123", "testfileid234"},
				},
			},
		},
		{
			Name: "TestTRexCarState",
			Object: common.Message{
				Handler: "car_state",
				Data: common.CarStateUpdate{
					ECUs: map[string]common.CarECU{
						"TEST123": {
							ECU:               "trex",
							Version:           "1.2.3",
							SerialNumber:      "REALSERIAL123",
							HWVersion:         "HWVERSION456",
							BootLoaderVersion: "BOOTLOADER789",
							Fingerprint:       "FINGERPRINT13579",
							Config:            "CONFIG02468",
							Vendor:            "FISKER",
						},
					},
				},
			},
		},
		{
			Name: "TestTRexLog",
			Object: common.Message{
				Handler: "trex_log",
				Data: []common.TRexLog{
					{
						Level:      common.Info,
						Timestamp:  "2022-04-29 17:38:32.230052",
						LineNumber: 17,
						FileName:   "example.cpp",
						Message:    "this is a test log",
					},
					{
						Level:      common.Error,
						Timestamp:  "2022-04-29 17:38:32.230052",
						LineNumber: 30,
						FileName:   "example.cpp",
						Message:    "this is a test error log",
					},
				},
			},
		},
		{
			Name: "TestTRexError",
			Object: common.Message{
				Handler: "error",
				Data: common.TRexError{
					Code: 0,
					Message: []common.TRexErrorMessage{
						{
							Description: "test",
							Context:     []string{},
						},
					},
				},
			},
		},
	}

	testhelper.NewSchemaTestHelper(t, schema).
		RunSchemaTests(tests)
}
