<?php

namespace Alsendo\AlsendoWrapper\Api\Ecolet\Wrapper;

use Alsendo\AlsendoWrapper\Api\Ecolet\Model\AdditionalService;
use Alsendo\AlsendoWrapper\Api\Ecolet\Model\Order\Courier;
use Alsendo\AlsendoWrapper\Api\Ecolet\Model\Order\CourierPickup;
use Alsendo\AlsendoWrapper\Api\Ecolet\Model\Dimension;
use Alsendo\AlsendoWrapper\Api\Ecolet\Model\Order\EcoletOrderRequest;
use Alsendo\AlsendoWrapper\Api\Ecolet\Model\Parcel;
use Alsendo\AlsendoWrapper\Model\Order\OrderRequest;
use Alsendo\AlsendoWrapper\Model\Shipment;

class OrderRequestWrapper
{
    /**
     * @param OrderRequest $orderRequest
     * @return EcoletOrderRequest
     *
     * Wraps OrderRequest into EcoletOrderRequest
     */
    public static function wrap(OrderRequest $orderRequest): EcoletOrderRequest
    {
        $ecoletOrderRequest = new EcoletOrderRequest();
        $ecoletOrderRequest->sender = $orderRequest->address->sender;
        $ecoletOrderRequest->sender->country = strtolower($ecoletOrderRequest->sender->countryCode);
        $ecoletOrderRequest->sender->county = $orderRequest->address->sender->stateCode;
        $ecoletOrderRequest->sender->locality = $orderRequest->address->sender->city;
        unset(
            $ecoletOrderRequest->sender->countryCode,
            $ecoletOrderRequest->sender->line1,
            $ecoletOrderRequest->sender->line2,
            $ecoletOrderRequest->sender->stateCode,
            $ecoletOrderRequest->sender->isResidential,
            $ecoletOrderRequest->sender->foreignAddressId,
            $ecoletOrderRequest->sender->city,
            $ecoletOrderRequest->sender->id,
            $ecoletOrderRequest->sender->mapPointName,
        );
        $ecoletOrderRequest->receiver = $orderRequest->address->receiver;
        $ecoletOrderRequest->receiver->country = strtolower($ecoletOrderRequest->receiver->countryCode);
        $ecoletOrderRequest->receiver->county = $orderRequest->address->receiver->stateCode;
        $ecoletOrderRequest->receiver->locality = $orderRequest->address->receiver->city;
        unset(
            $ecoletOrderRequest->receiver->countryCode,
            $ecoletOrderRequest->receiver->line1,
            $ecoletOrderRequest->receiver->line2,
            $ecoletOrderRequest->receiver->stateCode,
            $ecoletOrderRequest->receiver->isResidential,
            $ecoletOrderRequest->receiver->foreignAddressId,
            $ecoletOrderRequest->receiver->city,
            $ecoletOrderRequest->receiver->id,
            $ecoletOrderRequest->receiver->mapPointName,
        );
        $parcel = new Parcel();
        /** @var Shipment $shipment */
        $shipment = $orderRequest->shipment[0];

        $parcel->type = $shipment->shipmentTypeCode;
        $parcel->observations = $shipment->observations;
        $parcel->shape = $shipment->shape;

        unset(
            $parcel->weight,
            $parcel->dimensions,
            $parcel->declaredValue,
            $parcel->content,
        );

        $ecoletOrderRequest->parcel = $parcel;

        foreach ($orderRequest->shipment as $shipment) {
            $parcel = new Parcel();
            $parcel->weight = $shipment->weight;
            $parcel->amount = $shipment->amount;
            $parcel->type = $shipment->shipmentTypeCode;
            $parcel->content = $shipment->content;
            $parcel->declaredValue = $shipment->declaredValue;
            $parcel->dimensions = new Dimension(
                $shipment->dimension1,
                $shipment->dimension2,
                $shipment->dimension3
            );
            $parcel->observations = $shipment->observations;
            $parcel->shape = $shipment->shape;
            unset(
                $parcel->type,
                $parcel->shape,
                $parcel->observations,
                $parcel->amount,
            );
            $ecoletOrderRequest->parcels[] = $parcel;
        }

        $ecoletOrderRequest->additionalServices = $orderRequest->additionalServices;

        if ($orderRequest->cod) {
            $ecoletOrderRequest->additionalServices['cod'] = new AdditionalService(true, $orderRequest->cod->amount);
        } else {
            // fix COD ECOLET.
            $ecoletOrderRequest->additionalServices['cod'] = new AdditionalService(false, 0 );
        }

        $courier = new Courier();
        $courier->service = $orderRequest->serviceId;
        $courier->pickup = new CourierPickup();
        $courier->pickup->type = $orderRequest->pickup->type;
        $courier->pickup->date = $orderRequest->pickup->date;
        $courier->pickup->time = $orderRequest->pickup->hoursFrom;

        $ecoletOrderRequest->courier = $courier;

        $ecoletOrderRequest->source = $orderRequest->source;

        return $ecoletOrderRequest;
    }
}