<?php

namespace Alsendo\AlsendoWrapper\Api\Zaslat\Wrapper;

use Alsendo\AlsendoWrapper\Api\Ecolet\Model\AdditionalService;
use Alsendo\AlsendoWrapper\Api\Zaslat\Model\Order\ZaslatOrderRequest;
use Alsendo\AlsendoWrapper\Api\Zaslat\Model\Order\ZaslatPackage;
use Alsendo\AlsendoWrapper\Api\Zaslat\Model\Order\ZaslatService;
use Alsendo\AlsendoWrapper\Api\Zaslat\Model\Order\ZaslatServiceData;
use Alsendo\AlsendoWrapper\Api\Zaslat\Model\Order\ZaslatShipment;
use Alsendo\AlsendoWrapper\Model\Order\OrderRequest;
use Alsendo\AlsendoWrapper\Model\Shipment;

class OrderRequestWrapper
{
    /**
     * @param OrderRequest $orderRequest
     * @return ZaslatOrderRequest
     *
     * Wraps OrderRequest into ZaslatOrderRequest
     * @throws \DateMalformedStringException|\Exception
     */
    public static function wrap(OrderRequest $orderRequest): ZaslatOrderRequest
    {
        $zaslatOrderRequest = new ZaslatOrderRequest();
        //$zaslatOrderRequest->source = $orderRequest->source; Response API: 500, Internal server error.
        $zaslatOrderRequest->currency = $orderRequest->currency;
        $zaslatOrderRequest->paymentType = $orderRequest->paymentType;
        $zaslatOrderRequest->voucher = $orderRequest->promoCode;


        $zaslatShipment = new ZaslatShipment();

        if(  ! empty( $orderRequest->pickup->type ) ) {
            $zaslatShipment->type = $orderRequest->pickup->type; // fix Zaslat
        }

        $zaslatShipment->carrier = $orderRequest->carrier;

        if ( ! is_null( $orderRequest->pickupRequest ) ) {
            if( 'BALIKOVNA' !== strtoupper( $orderRequest->carrier) && 'ZASILKOVNA' !== strtoupper( $orderRequest->carrier)  && 'PACKETA' !== strtoupper( $orderRequest->carrier) ) {
                $zaslatShipment->pickupRequest = 1;
            }
        }

        if ( ! empty($orderRequest->pickup->date ) ) {
            if( 'BALIKOVNA' !== strtoupper( $orderRequest->carrier) && 'ZASILKOVNA' !== strtoupper( $orderRequest->carrier) && 'PACKETA' !== strtoupper( $orderRequest->carrier) ) {
                $zaslatShipment->pickupDate = (new \DateTimeImmutable($orderRequest->pickup->date))->format('Y-m-d');
            }
        }

        $zaslatShipment->from = ContactWrapper::wrap($orderRequest->address->sender);
        $zaslatShipment->to = ContactWrapper::wrap($orderRequest->address->receiver);

        $zaslatShipment->note = $orderRequest->comment;

        if ( $orderRequest->address->sender->hasMapPoint ) {
            $zaslatShipment->pickupBranch = 1;
        }


        $zaslatShipment->deliveryBranch = $orderRequest->address->receiver->mapPointId;

        foreach ($orderRequest->shipment as $shipment) {
            /**
             * @var $shipment Shipment
             */
            $zaslatPackage = new ZaslatPackage();
            $zaslatPackage->weight  = $shipment->weight;
            $zaslatPackage->width = $shipment->dimension1;
            $zaslatPackage->length = $shipment->dimension2;
            $zaslatPackage->height = $shipment->dimension3;

            $zaslatShipment->packages[] = $zaslatPackage;

        }

        if ($orderRequest->additionalServices) {
            foreach ($orderRequest->additionalServices as $key => $additionalService) {
                /** @var  AdditionalService $additionalService */
                $zaslatSerivce = new ZaslatService();
                $zaslatSerivce->code = $key;
                $zaslatSerivce->data = new ZaslatServiceData();
                $zaslatSerivce->data->currency = $orderRequest->currency;
                $zaslatSerivce->data->value = $additionalService->amount;

                //$zaslatShipment->services[] = $zaslatSerivce;
            }
        }


        $zaslatShipment->reference = $orderRequest->reference;

        $zaslatOrderRequest->shipments[] = $zaslatShipment;

        return $zaslatOrderRequest;
    }
}
