package common_test

import (
	"testing"

	"github.com/fiskerinc/cloud-services/pkg/common"
	"github.com/fiskerinc/cloud-services/pkg/common/handlers"
	"github.com/fiskerinc/cloud-services/pkg/testhelper"
	"github.com/fiskerinc/cloud-services/pkg/utils/elptr"
)

func TestTRexRXMessages(t *testing.T) {
	schema := "file://" + testhelper.GetSchemaDirPath() + "/trex/RXMessage.json"
	var bhex common.BinaryHex
	bhex = []byte("test")
	manifest := common.UpdateManifest{
		ID:           1234567890,
		Name:         "Test Update Manifest",
		Version:      "1.2.3",
		Description:  "This is a test manifest",
		ReleaseNotes: "https://fiskerinc.com",
		Fingerprint:  "8fe27f7fb079ebe06db5f88586b98797",
		Type:         "standard",
		Country:      "US",
		PowerTrain:   "MD23",
		Restraint:    "None",
		Model:        "Ocean",
		Trim:         "Sport",
		Year:         2022,
		BodyType:     "truck",
		ECUs: []*common.UpdateManifestECU{
			{
				ECU:               "adas",
				Version:           "1.2.3",
				CurrentVersion:    "1.0.0",
				HWVersions:        []string{"10000"},
				ConfigurationMask: "04d923318b8e1517a9cc8b52af11b051",
				Configuration:     "04d923318b8e1517a9cc8b52af11b051",
				Files: []*common.UpdateManifestFile{
					{
						FileID:        "XXXXXXXXXX",
						URL:           "https://fiskerinc.com/adas_test.txt",
						Checksum:      "CHECKSUM",
						FileType:      common.Calibration,
						FileSize:      90,
						Signature:     "53e976bd61161e4f425186e560f0797767d10fd6f0116e224efc444fa014754cb299e51827468c03c9b06508740ed738b1ec14b9cc753a1875971e335408660e",
						WriteRegionID: 100,
						WriteRegion: common.MemoryRegion{
							Offset: 101,
							Length: 102,
						},
						EraseRegionID: 200,
						EraseRegion: &common.MemoryRegion{
							Offset: 201,
							Length: 202,
						},
						FileKey: &common.FileKeyResponse{
							FileID: "fileid",
							Key:    "key",
							Auth:   "auth",
							Nonce:  "nonce",
							Error:  "NoError",
						},
					},
				},
				Rollback: []*common.UpdateManifestECU{
					{
						Version:           "1.2.3",
						ConfigurationMask: "04d923318b8e1517a9cc8b52af11b051",
						Configuration:     "04d923318b8e1517a9cc8b52af11b051",
						Files: []*common.UpdateManifestFile{
							{
								FileID:        "XXXXXXXXXX",
								URL:           "https://fiskerinc.com/adas_test.txt",
								FileType:      common.Calibration,
								Checksum:      "CHECKSUM",
								FileSize:      100,
								WriteRegionID: 100,
								WriteRegion: common.MemoryRegion{
									Offset: 101,
									Length: 102,
								},
							},
						},
					},
				},
				ECCKeys: &common.ECCKeys{
					PrivKey1: &bhex,
					PrivKey2: &bhex,
					PrivKey3: &bhex,
				},
			},
		},
		CarUpdateID: 1,
	}
	manifest.Scrub(common.TRex)

	tests := []testhelper.SchemaTest{
		{
			Name: "TestTRexCarFileKeys",
			Object: common.Message{
				Handler: "filekeys",
				Data: []common.FileKeyResponse{
					{
						FileID: "abc123",
						Key:    "testKey",
						Auth:   "testAuth",
						Nonce:  "testNonce",
						Error:  "noError",
					},
				},
			},
		},
		{
			Name: "TestTRexCarUpdateInstall",
			Object: common.Message{
				Handler: "update_manifest",
				Data:    manifest,
			},
		},
		{
			Name: "TestTRexConfigResponse",
			Object: common.Message{
				Handler: "config",
				Data: common.TRexConfigResponse{
					CANBus: common.CANBus{
						Enabled: true,
						Filters: []common.CANFilter{
							{
								CANID:    "1234",
								Interval: elptr.ElPtr(1000),
							},
							{
								CANID:    "5128",
								EdgeMask: elptr.ElPtr(common.BinaryHex("")), // it won't serialize if it's empty
							},
							{
								CANID:    "5678",
								Interval: elptr.ElPtr(2000),
							},
							{
								CANID:    "5678",
								EdgeMask: elptr.ElPtr(bhex),
							},
						},
						DataLogger:   true,
						DTCEnabled:   elptr.ElPtr(true),
						MemBuffSize:  1 * 1024 * 1024,
						DiskBuffSize: 10 * 1024 * 1024,
					},
					LogLevel: common.Debug,
				},
			},
		},
		{
			Name: "TestTRexConfigEmptyResponse",
			Object: common.Message{
				Handler: "config",
				Data: common.TRexConfigResponse{
					CANBus: common.CANBus{
						Enabled:      false,
						Filters:      []common.CANFilter{},
						DataLogger:   false,
						DTCEnabled:   elptr.ElPtr(false),
						MemBuffSize:  0,
						DiskBuffSize: 0,
					},
					LogLevel: common.Info,
				},
			},
		},
		{
			Name: "TestTRexError",
			Object: common.Message{
				Handler: "error",
				Data: common.MessageString{
					Message: "disconnected by duplicate ID",
				},
			},
		},
		{
			Name: "TestCancelUpdateManifest",
			Object: common.Message{
				Handler: handlers.UpdateManifestCancel,
				Data: common.CarUpdateRequest{
					CarUpdateID: 1000,
				},
			},
		},
	}

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