<?php

namespace App\Classes\Modules\PackingLists\Processors;

use App\Classes\Modules\Orders\Services\FetchesDataFromVTPortal;
use App\Classes\Modules\PackingLists\DataTransferObjects\PackageObject;
use App\Classes\Modules\PackingLists\Services\ListsPackingLists;
use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Classes\ValueObjects\Constants\PackageType;
use App\Classes\ValueObjects\Constants\PackingListType;
use App\Models\PackingList;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class FetchPackingListFromVTPortalProcessor
{

    /** @var FetchesDataFromVTPortal */
    private $fetchesDataFRomVTPortal;

    /** @var ListsPackingLists */
    private $listsPackingLists;

    /** @var CreatePackageProcessor */
    private $createPackageProcessor;

    /**
     * FetchPackingListFromVTPortalProcessor constructor.
     * @param FetchesDataFromVTPortal $fetchesDataFRomVTPortal
     * @param ListsPackingLists $listsPackingLists
     * @param CreatePackageProcessor $createPackageProcessor
     */
    public function __construct(FetchesDataFromVTPortal $fetchesDataFRomVTPortal, ListsPackingLists $listsPackingLists, CreatePackageProcessor $createPackageProcessor)
    {
        $this->fetchesDataFRomVTPortal = $fetchesDataFRomVTPortal;
        $this->listsPackingLists = $listsPackingLists;
        $this->createPackageProcessor = $createPackageProcessor;
    }


    /**
     * @return array
     * @throws \App\Classes\Exceptions\AccessForbiddenException
     * @throws \App\Classes\Exceptions\RequestValidationException
     */
    public function execute(){

        $packingLists = PackingList::where('type', '=', PackingListType::SHIPPING_PACKING_LIST)->whereIn('status', [ApprovalStatus::PENDING_VERIFICATION, ApprovalStatus::SUSPENDED])->whereDoesntHave('containers', function($query){
            $query->where('status', '=', ApprovalStatus::COMPLETED);
        })->get();

        foreach($packingLists as $packingList){

            try {
                DB::beginTransaction();

                $filter = '["PoNumber:=%js%\"'.$packingList->reference.'\"\u0000"]';

                $shippingPackingListsRequest = $this->fetchesDataFRomVTPortal->clientRequest('http://portal.vtnation.com.my/Services/DataControllerService.asmx/GetPage', 'POST', json_decode('{"controller":"VPodetailparcel","view":"grid1","request":{"PageIndex":-1,"PageSize":10000,"SortExpression":"CreatedOn asc","Filter":'.$filter.'}}'), '');

                $shippingPackingLists = $this->fetchesDataFRomVTPortal->getResponseBody($shippingPackingListsRequest);

                $packingList->packages()->delete();

                foreach($shippingPackingLists->Rows as $shippingPackingList){
                    if(!$shippingPackingList[22]) {
                        continue;
                    }

                    $packageObject = new PackageObject(PackageType::CARTON, $shippingPackingList[19], $shippingPackingList[31], $shippingPackingList[30], $shippingPackingList[29], 0, $shippingPackingList[22], ApprovalStatus::APPROVED);
                    $this->createPackageProcessor->execute($packageObject, $packingList);
                }

                DB::commit();
            } catch (GuzzleException $exception) {
                continue;
            }

        }

        return [];

    }


}
