<?php

namespace App\Classes\Modules\Exports\ControllersLogic;


use App\Classes\General\Abstracts\Abstract2ControllerLogic;
use App\Classes\Modules\Exports\Standards\Rules\CanExportPackingList;
use App\Classes\Modules\Exports\Services\ExportsOnHoldPackingList;
use App\Classes\Modules\Exports\Services\ExportsPendingArrangementDeliveryList;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Maatwebsite\Excel\Excel;

class ExportPackingListLogic extends Abstract2ControllerLogic
{
    /**
     * @return array
     */
    protected function notification():array {
        return [
            'title' => 'Retrieved PackingList',
            'message' => 'You have successfully exported PackingList'
        ];
    }

    /** @var ExportsOnHoldPackingList */
    private $exportsOnHoldPackingList;

    /** @var ExportsPendingArrangementDeliveryList */
    private $exportsPendingArrangementDeliveryList;

    /** @var CanExportPackingList */
    private $canExportPackingList;

    /**
     * ExportPackingListLogic constructor.
     * @param ExportsPendingArrangementDeliveryList $exportsPendingArrangementDeliveryList
     * @param ExportsOnHoldPackingList $exportsOnHoldPackingList
     * @param CanExportPackingList $canExportPackingList
     */
    public function __construct(ExportsPendingArrangementDeliveryList $exportsPendingArrangementDeliveryList, ExportsOnHoldPackingList $exportsOnHoldPackingList, CanExportPackingList $canExportPackingList)
    {
        $this->exportsPendingArrangementDeliveryList = $exportsPendingArrangementDeliveryList;
        $this->exportsOnHoldPackingList = $exportsOnHoldPackingList;
        $this->canExportPackingList = $canExportPackingList;
    }


    /**
     * @param Request $request
     * @return Response
     */
    public function logic(Request $request) : BinaryFileResponse
    {
        $this->canExportPackingList->passes();
        $isSetPendingArrangement = $request->header('PendingArrangement');
        if($isSetPendingArrangement){
            $response = $this->exportsPendingArrangementDeliveryList->download('packing-list-delivery.xls', Excel::XLS, ['Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']);
        }
        else {
            $response = $this->exportsOnHoldPackingList->download('packing-list-on-hold.xls', Excel::XLS, ['Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']);
        }

        ob_end_clean();
        return $response;
    }

}
