package common

import (
	"encoding/xml"
	"time"
)

type FlashpackDataRequest struct {
	XMLName                  xml.Name                 `xml:"SerializationData"`
	Header                   FlashPackHeaderArea      `xml:"HeaderArea" validate:"required"`
	VehicleData              VehicleData              `xml:"DataArea>VehicleData" validate:"required"`
	SerializationDataIndices SerializationDataIndices `xml:"DataArea>SerializationDataIndices" validate:"required"`
}

type FlashPackHeaderArea struct {
	CreationDateTime  CreationDateTimeWithSeconds `xml:"CreationDateTime" json:"creation_date_time"`
	MessageIdentifier string                      `xml:"MessageIdentifier" json:"message_identifier"`
	InterfaceID       int                         `xml:"InterfaceId" json:"interface_id"`
	SourceSystem      string                      `xml:"Sender>SourceSystem" json:"source_system"`
	TargetSystem      string                      `xml:"Sender>TargetSystem" json:"target_system"`
}

type CreationDateTimeWithSeconds struct {
	time.Time
}

func (t *CreationDateTimeWithSeconds) UnmarshalText(data []byte) error {
	// Fractional seconds are handled implicitly by Parse.
	var err error
	t.Time, err = time.Parse("2006-01-02T15:04:05", string(data))

	return err
}

func (t CreationDateTimeWithSeconds) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
	s := t.Format("2006-01-02T15:04:05")
	return e.EncodeElement(s, start)
}

type VehicleData struct {
	VIN             string `xml:"VIN"`
	FlashPackNumber string `xml:"FlashPackNumber"`
	PDXData         string `xml:"PDXData"`
}

type SerializationDataIndices struct {
	SerializationDataIndices []SerializationDataIndex `xml:"SerializationDataIndex"`
}

type SerializationDataIndex struct {
	IndexNr                                 string `xml:"IndexNr"`
	IndexName                               string `xml:"IndexName"`
	ECUName                                 string `xml:"ECUName"`
	DataType                                string `xml:"DataType"`
	SerialNumber                            string `xml:"SerialNumber"`
	F108_CodingData                         string `xml:"F108_CodingData"`
	F111_VehicleOrderData                   string `xml:"F111_VehicleOrderData"`
	F183_BootloaderVersion                  string `xml:"F183_BootloaderVersion"`
	F187_VehicleManufacturerSparePartNumber string `xml:"F187_VehicleManufacturerSparePartNumber"`
	F188_ECUSoftwareNumber                  string `xml:"F188_ECUSoftwareNumber"`
	F191_ECUHardwareNumber                  string `xml:"F191_ECUHardwareNumber"`
	F195_ECUSoftwareVersionNumber           string `xml:"F195_ECUSoftwareVersionNumber"`
	F18C_ECUSerialNumber                    string `xml:"F18C_ECUSerialNumber"`
	MaterialNr                              string `xml:"MaterialNr"`
	SoftwareBranch                          string `xml:"SoftwareBranch"`
	SourcingType                            string `xml:"SourcingType"`
	SourcingTypeName                        string `xml:"SourcingTypeName"`
}

// FlashpackDataRequestSwag is used ONLY to represent FlashpackDataRequest in swagger.
type FlashpackDataRequestSwag struct {
	Header struct {
		CreationDateTime  CreationDateTime `json:"CreationDateTime" swaggertype:"string"`
		MessageIdentifier string           `json:"MessageIdentifier"`
		InterfaceID       int              `json:"InterfaceId"`
		Sender            struct {
			SourceSystem string `json:"SourceSystem"`
			TargetSystem string `json:"TargetSystem"`
		} `json:"Sender"`
	} `json:"HeaderArea" validate:"required"`
	DataArea struct {
		VehicleData struct {
			VIN             string `json:"vin"`
			FlashpackNumber string `json:"flashpack_number"`
			PDXNumber       string `json:"pdx_number"`
		} `json:"VehicleData" validate:"required"`
		SerializationDataIndices struct {
			SerializationDataIndices []struct {
				IndexNr                                 string `json:"index_nr"`
				IndexName                               string `json:"index_name"`
				DataType                                string `json:"data_type"`
				SerialNumber                            string `json:"serial_number"`
				F108_CodingData                         string `json:"f108_coding_data"`
				F111_VehicleOrderData                   string `json:"f111_vehicle_order_data"`
				F183_BootloaderVersion                  string `json:"f183_boot_loader_version"`
				F187_VehicleManufacturerSparePartNumber string `json:"f187_vehicle_manufacturer_spare_part_number"`
				F188_ECUSoftwareNumber                  string `json:"f188_ecu_software_number"`
				F191_ECUHardwareNumber                  string `json:"f191_ecu_hardware_number"`
				F195_ECUSoftwareVersionNumber           string `json:"f195_ecu_software_version_number"`
				F18C_ECUSerialNumber                    string `json:"f18c_ecu_serial_number"`
				MaterialNr                              string `json:"material_nr"`
				SoftwareBranch                          string `json:"software_branch"`
				SourcingType                            string `json:"sourcing_type"`
				SourcingTypeName                        string `json:"sourcing_type_name"`
			}
		} `json:"SerializationDataIndices" validate:"required"`
	} `json:"DataArea"`
} // @name SerializationData
