package common

import "time"

const (
	PositionLeftFront        string = "left_front"
	PositionRightFront       string = "right_front"
	PositionLeftRear         string = "left_rear"
	PositionRightRear        string = "right_rear"
	PositionLeftRearQuarter  string = "left_rear_quarter"
	PositionRightRearQuarter string = "right_rear_quarter"
	PositionRearWindshield   string = "rear_windshield"
	PositionTrunk            string = "trunk"
)

type RemoteCommandSource struct {
	Command string     `json:"command" validate:"required,max=100"`
	Data    *string    `json:"data,omitempty"`
	Start   *time.Time `json:"start,omitempty"`
	End     *time.Time `json:"end,omitempty"`
}

type RemoteReadVersionsCommandArgs struct {
	ECUName string `json:"ecu_name" validate:"required,max=100"`
}

type RemoteResetDiagnosticCommandArgs struct {
	ECUName string   `json:"ecu_name" validate:"required,max=100"`
	UDSKeys *ECCKeys `json:"uds_keys,omitempty"`
}

type RemoteCANNetworkCommandArgs struct {
	Action  string `json:"action" validate:"required,oneof=on off"`
	Timeout int32  `json:"timeout"`
}

type RemoteIgnitionCommandArgs struct {
	Action  string `json:"action" validate:"required,oneof=on off"`
	Timeout int32  `json:"timeout"`
}

type RemoteUpdateSecOCCommandArgs struct {
	ECUs      []string  `json:"ecus" validate:"required"`
	UDSKeys   []ECCKeys `json:"uds_keys" validate:"required"`
	KeyBase64 string    `json:"key_base64" validate:"required"`
}

type RemoteDiagnosticCommandRequest struct {
	VINs           []string `json:"vins,omitempty" validate:"required,max=1000,dive,vin"`
	Command        string   `json:"command" validate:"required,oneof=remote_reset can_network remote_ignition read_ecu_versions write_secoc_key"`
	ECU            string   `json:"ecu_name,omitempty"`
	CANNetAction   string   `json:"can_net_action,omitempty"`
	IgnitionAction string   `json:"ignition_action,omitempty"`
	Timeout        int32    `json:"timeout"`
}
type RemoteCommandRequest struct {
	VIN          string     `json:"vin"`
	Source       string     `json:"source"`
	SentAt       *time.Time `json:"time,omitempty"`
	WaitDuration int        `json:"wait_dur"`
	RemoteCommandSource
}

func (r *RemoteCommandRequest) IsExpired(commandExpiration time.Duration) bool {
	if r.SentAt == nil {
		return false
	}

	expiresAt := r.SentAt.Add(commandExpiration * time.Second)
	return expiresAt.Before(time.Now())
}

func (r *RemoteCommandRequest) GetSentAt() *time.Time {
	return r.SentAt
}

func (r *RemoteCommandRequest) SetSentAt(sentAt time.Time) {
	r.SentAt = &sentAt
}
