package common_test

import (
	"testing"
	"time"

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

func TestHMIRXMessages(t *testing.T) {
	schema := "file://" + testhelper.GetSchemaDirPath() + "/hmi/RXMessage.json"
	now := time.Now()
	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:               "ICC",
				Version:           "1.2.3",
				CurrentVersion:    "1.0.0",
				HWVersions:        []string{"10000"},
				ConfigurationMask: "04d923318b8e1517a9cc8b52af11b051",
				Files: []*common.UpdateManifestFile{
					{
						FileID:        "XXXXXXXXXX",
						URL:           "https://fiskerinc.com/ICC.bin",
						Checksum:      "CHECKSUM",
						FileType:      common.Software,
						FileSize:      90,
						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",
						Files: []*common.UpdateManifestFile{
							{
								FileID:        "XXXXXXXXXX",
								URL:           "https://fiskerinc.com/settings.config",
								FileType:      common.Calibration,
								Checksum:      "CHECKSUM",
								FileSize:      100,
								WriteRegionID: 100,
								WriteRegion: common.MemoryRegion{
									Offset: 101,
									Length: 102,
								},
								EraseRegionID: 200,
								EraseRegion: &common.MemoryRegion{
									Offset: 201,
									Length: 202,
								},
							},
						},
					},
				},
			},
		},
		CarUpdateID: 1,
	}
	manifest.Scrub(common.HMI)

	tests := []testhelper.SchemaTest{
		{
			Name: "TestHMIUpdateManifest",
			Object: common.Message{
				Handler: "update_manifest",
				Data:    manifest,
			},
		},
		{
			Name: "TestMobileMapDestinationSource",
			Object: common.Message{
				Handler: "map_destination",
				Data: common.MapDestinationSource{
					DriverID: "valid-cognito-id-1",
					Name:     "Fisker Inc",
					Address: &common.TomTomAddress{
						StreetNumber:           "3030",
						StreetName:             "17th St",
						LocalName:              "San Francisco",
						PostalCode:             "94110",
						CountrySubdivisionName: "California",
						CountryCodeIso3:        "USA",
					},
					Coordinates: common.MapCoordinates{
						Latitude:  37.0,
						Longitude: -122.0,
					},
				},
			},
		},
		{
			Name: "TestMobileMapRouteSource",
			Object: common.Message{
				Handler: "map_route",
				Data: common.MapRouteSource{
					DriverID: "valid-cognito-id-1",
					Waypoints: []common.MapWaypoint{
						{
							Type:  "Restaurant",
							Title: "Test Restaurant",
							MapCoordinates: common.MapCoordinates{
								Latitude:  111,
								Longitude: 222,
							},
						},
						{
							Type:  "Park",
							Title: "Test Park",
							MapCoordinates: common.MapCoordinates{
								Latitude:  333.333,
								Longitude: 444.444,
							},
						},
					},
					Route: []common.MapCoordinates{
						{
							Latitude:  123,
							Longitude: 456,
						},
						{
							Latitude:  999,
							Longitude: 888,
						},
					},
				},
			},
		},
		{
			Name: "TestHMIProfiles",
			Object: common.Message{
				Handler: "profiles",
				Data: []common.CarToDriverModel{{
					User: common.UserProfile{
						FirstName: "First",
						LastName:  "Last",
						Email:     "test@test.com",
						Phone:     "1234567890",
					},
					DriverID: "valid-cognito-id-1",
					Role:     "DRIVER",
					Settings: []common.CarSetting{{
						Name:  "TESTSETTING",
						Value: "TESTVALUE",
						Type:  "string",
						DBModelBase: dbbasemodel.DBModelBase{
							UpdatedAt: &now,
						},
					}},
					Subscriptions: []common.Subscription{{
						Name:        "test",
						Destination: "ICC",
						Expires:     time.Date(2021, 10, 30, 8, 35, 0, 0, time.UTC),
					}},
				}},
			},
		},
		{
			Name: "TestHMISettingsUpdate",
			Object: common.Message{
				Handler: "settings_update",
				Data: common.HMISettingsUpdate{
					DriverID: "valid-cognito-id-1",
					Settings: []common.CarSetting{{
						Name:  "TESTSETTING",
						Value: "TESTVALUE",
						Type:  "string",
						DBModelBase: dbbasemodel.DBModelBase{
							UpdatedAt: &now,
						},
					}},
				},
			},
		},
		{
			Name: "TestHMISubscriptionsUpdate",
			Object: common.Message{
				Handler: "subscriptions_update",
				Data: common.HMISubscriptionUpdate{
					DriverID: "valid-cognito-id-1",
					Subscriptions: []common.Subscription{{
						Name:        "test",
						Destination: "ICC",
						Expires:     time.Date(2021, 10, 30, 8, 35, 0, 0, time.UTC),
					}},
				},
			},
		},
		{
			Name: "TestHMIPOIs",
			Object: common.Message{
				Handler: "user_pois_get",
				Data: common.HMIPOIRequestMessage{
					DriverID: "valid-cognito-id-1",
				},
			},
		},
		{
			Name: "TestHMIPOICreate",
			Object: common.Message{
				Handler: "user_poi_create",
				Data: common.HMIPOIMessage{
					DriverID: "valid-cognito-id-1",
					UserPOI: common.PointOfInterest{
						Name: "TestPOI",
						Location: common.POILocation{
							Latitude:  1.23,
							Longitude: 4.56,
						},
					},
				},
			},
		},
		{
			Name: "TestHMIPOIEdit",
			Object: common.Message{
				Handler: "user_poi_edit",
				Data: common.HMIPOIMessage{
					DriverID: "valid-cognito-id-1",
					UserPOI: common.PointOfInterest{
						Name: "TestPOI",
						Location: common.POILocation{
							Latitude:  8.88,
							Longitude: 9.99,
						},
					},
				},
			},
		},
		{
			Name: "TestHMIPOIDelete",
			Object: common.Message{
				Handler: "user_poi_delete",
				Data: common.HMIPOIDeleteMessage{
					DriverID: "valid-cognito-id-1",
					Name:     "TestPOI",
				},
			},
		},
		{
			Name: "TestHMIError",
			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)
}
