package entitymapping

import (
	"bitbucket.org/nemt/nemt-portal-api/application/viewmodel"
	"bitbucket.org/nemt/nemt-portal-api/domain/entity"
)

// rideMapping has method to map ride entities to view models
type visitMapping struct {
	mapper *Mapper
}

// ToVisitEntity maps a User entity to User view model
func (mapping *visitMapping) ToVisitEntity(item viewmodel.Visit) entity.Visit {
	return entity.Visit{
		UUID:                 item.UUID,
		Status:               mapping.ToVisitStatusEntity(item.Status),
		User:                 mapping.mapper.User.ToUserEntity(item.User),
		VisitDuration:        item.VisitDuration,
		VisitDatetime:        item.VisitDatetime,
		PickupDatetime:       item.PickupDatetime,
		Notes:                item.Notes,
		PickupAddressID:      item.PickupAddressID,
		DestinationAddressID: item.DestinationAddressID,
		Pickup:               mapping.mapper.Ride.ToLocationEntity(item.Pickup),
		Provider:             mapping.mapper.Provider.ToProviderRespEntity(item.Provider),
		CreatedUser:          mapping.mapper.User.ToUserEntity(item.CreatedUser),
		Created:              item.CreatedDate,
		Updated:              item.UpdatedDate,
		ReturnDate:           item.ReturnDate,
		ExternalID:           item.ExternalID,
		TripType:             mapping.ToTripTypeEntity(item.TripType),
		Rides:                mapping.mapper.Ride.ToRideInternalEntitySlice(item.Rides),
	}
}

func (mapping *visitMapping) ToVisitStatusEntity(item viewmodel.VisitStatus) entity.VisitStatus {
	return entity.VisitStatus{
		Value: item.Value,
		Key:   item.Key,
	}
}

// ToVisitEntity maps a User entity to User view model
func (mapping *visitMapping) ToVisitModel(item entity.Visit) viewmodel.Visit {
	return viewmodel.Visit{
		UUID:                 item.UUID,
		Status:               mapping.ToVisitStatusModel(item.Status),
		User:                 mapping.mapper.User.ToUserModel(item.User),
		VisitDuration:        item.VisitDuration,
		VisitDatetime:        item.VisitDatetime,
		PickupDatetime:       item.PickupDatetime,
		Notes:                item.Notes,
		PickupAddressID:      item.PickupAddressID,
		DestinationAddressID: item.DestinationAddressID,
		Pickup:               mapping.mapper.Ride.ToLocationModel(item.Pickup),
		Provider:             mapping.mapper.Provider.ToProviderRespModel(item.Provider),
		CreatedUser:          mapping.mapper.User.ToUserModel(item.CreatedUser),
		CreatedDate:          item.Created,
		UpdatedDate:          item.Updated,
		ReturnDate:           item.ReturnDate,
		ExternalID:           item.ExternalID,
		TripType:             mapping.ToTripTypeModel(item.TripType),
		Rides:                mapping.mapper.Ride.ToRideModelSlice(item.Rides),
	}
}

func (mapping *visitMapping) ToVisitStatusModel(item entity.VisitStatus) viewmodel.VisitStatus {
	return viewmodel.VisitStatus{
		Value: item.Value,
		Key:   item.Key,
	}
}

// ToUserEntitySlice maps a User entity slice to User view model slice
func (mapping *visitMapping) ToVisitEntitySlice(list []viewmodel.Visit) (retVal []entity.Visit) {
	retVal = make([]entity.Visit, 0)

	for _, item := range list {
		retVal = append(retVal, mapping.ToVisitEntity(item))
	}

	return retVal
}

// ToRideModelSlice maps a Ride entity slice to Ride view model slice
func (mapping *visitMapping) ToVisitModelSlice(list []entity.Visit) (retVal []viewmodel.Visit) {
	retVal = make([]viewmodel.Visit, 0)

	for _, item := range list {
		retVal = append(retVal, mapping.ToVisitModel(item))
	}

	return retVal
}

func (mapping *visitMapping) ToTripTypeModel(item entity.TripType) viewmodel.TripType {
	return viewmodel.TripType{
		Value: item.Value,
		Key:   item.Key,
	}
}

func (mapping *visitMapping) ToTripTypeEntity(item viewmodel.TripType) entity.TripType {
	return entity.TripType{
		Value: item.Value,
		Key:   item.Key,
	}
}

// ToUserEntitySlice maps a User entity slice to User view model slice
func (mapping *visitMapping) ToTripTypeEntitySlice(list []viewmodel.TripType) (retVal []entity.TripType) {
	retVal = make([]entity.TripType, 0)

	for _, item := range list {
		retVal = append(retVal, mapping.ToTripTypeEntity(item))
	}

	return retVal
}

// ToRideModelSlice maps a Ride entity slice to Ride view model slice
func (mapping *visitMapping) ToTripTypeModelSlice(list []entity.TripType) (retVal []viewmodel.TripType) {
	retVal = make([]viewmodel.TripType, 0)

	for _, item := range list {
		retVal = append(retVal, mapping.ToTripTypeModel(item))
	}

	return retVal
}
