<?php
/**
 * Copyright © 2018 EaDesign by Eco Active S.R.L. All rights reserved.
 * See LICENSE for license details.
 */

namespace Eadesigndev\Awb\Model\Sources;

use Magento\Framework\App\Request\Http;
use Eadesigndev\Urgent\Model\Sources\PickupId as PickupIdUrgent;
use Eadesigndev\FanCourier\Model\Sources\PickupId as PickupIdFanCourier;
use Eadesigndev\DPD\Model\Sources\PickupId as PickupIdDPD;
use Eadesigndev\GLS\Model\Sources\PickupId as PickupIdGLS;
use Eadesigndev\DragonStar\Model\Sources\PickupId as PickupDragonStar;
use Eadesigndev\xCurier\Model\Sources\PickupId as PickupXCurier;
use Eadesigndev\NemoExpress\Model\Sources\PickupId as PickupNemoExpress;
use Eadesigndev\Awb\Model\Sources\CarrierType;
use Eadesigndev\Awb\Api\AwbRepositoryInterface;

/**
 * Class PickupId
 * @package Eadesigndev\Awb\ModelSource
 */
#[\AllowDynamicProperties]
class PickupIdAll extends AbstractSource
{
    private $carierId;
    private $pickupId;

    public function __construct(
        Http $request,
        private PickupIdFanCourier $pickupIdFanCourier,
        private PickupIdUrgent $pickupIdUrgent,
        private PickupIdDPD $pickupIdDPD,
        private PickupIdGLS $pickupIdGLS,
        private PickupDragonStar $pickupDragonStar,
        private PickupXCurier $pickupXCurier,
        private PickupNemoExpress $pickupNemoExpress,
        private AwbRepositoryInterface $awbRepository
    ) {
    }

    public function setCarrier()
    {
        switch ($this->carierId) {
            case CarrierType::URGENT_CURRIER:
                $this->pickupId = $this->pickupIdUrgent;
                break;
            case CarrierType::FAN_CURRIER:
                $this->pickupId = $this->pickupIdFanCourier;
                break;
            case CarrierType::DPD:
                $this->pickupId = $this->pickupIdDPD;
                break;
            case CarrierType::GLS:
                $this->pickupId = $this->pickupIdGLS;
                break;
            case CarrierType::DRAGON_STAR:
                $this->pickupId = $this->pickupDragonStar;
                break;
            case CarrierType::X_CURIER:
                $this->pickupId = $this->pickupXCurier;
                break;
            case CarrierType::NEMO_EXPRESS:
                $this->pickupId = $this->pickupNemoExpress;
                break;
        }
    }

    public function getAvailableFromCurrent()
    {
        $pickupLocation = $this->pickupId->getAvailable();

        return $pickupLocation;
    }

    public function getAvailable()
    {
        $availablePickupIds = [];
        foreach (
            [
                CarrierType::URGENT_CURRIER,
                CarrierType::FAN_CURRIER,
                CarrierType::DPD,
                CarrierType::GLS,
                CarrierType::DRAGON_STAR,
                CarrierType::X_CURIER,
                CarrierType::NEMO_EXPRESS
            ] as $carrierId
        ){
            $this->carierId = $carrierId;
            $this->setCarrier();
            $pickupId = $this->getAvailableFromCurrent();

            if (!empty($pickupId)) {
                $availablePickupIds = $availablePickupIds + $pickupId;
            }
        }

        return $availablePickupIds;
    }
}
