<?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 NemoExpressPickup;
use Eadesigndev\Sameday\Model\Sources\PickupId as SamedayPickup;
use Eadesigndev\Awb\Model\Sources\CarrierType;
use Eadesigndev\Awb\Api\AwbRepositoryInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;

/**
 * Class PickupId
 * @package Eadesigndev\Awb\ModelSource
 */
#[\AllowDynamicProperties]
class PickupId extends AbstractSource
{
    const AWB_PICKUP_ID = 1;

    public $carierId;
    public $pickupId;

    public static $settingsFieldKey = "pickup_id";

    public static $defaultPickup = [
        2 => [
            ['value' => '22420',   'label' => 'Magazia Lu Costica - Online'],
            ['value' => '1001085',   'label' => 'Depozit AGT'],
            ['value' => '201239786', 'label' => 'Depozit Dakard'],
            ['value' => '201195321', 'label' => 'Depozit EMT'],
            ['value' => '201156779', 'label' => 'Depozit Felco'],
            ['value' => '201214339', 'label' => 'Depozit Fiskars'],
            ['value' => '201173065', 'label' => 'Depozit Honda'],
            ['value' => '201194417', 'label' => 'Depozit Marolex'],
            ['value' => '201251525', 'label' => 'Depozit Proenerg'],
            ['value' => '201252060', 'label' => 'Depozit Proenerg Cernica'],
            ['value' => '201240279', 'label' => 'Depozit Scule'],
            ['value' => '201198698', 'label' => 'Depozit SOLO'],
            ['value' => '201162606', 'label' => 'Depozit Husqvarna Iasi'],
        ]
    ];

    public function __construct(
        Http $request,
        private PickupIdFanCourier $pickupIdFanCourier,
        private PickupIdUrgent $pickupIdUrgent,
        private PickupIdDPD $pickupIdDPD,
        private PickupIdGLS $pickupIdGLS,
        private PickupDragonStar $pickupDragonStar,
        private PickupXCurier $pickupXCurier,
        private NemoExpressPickup $pickupNemoExpress,
        private SamedayPickup $pickupSameday,
        public AwbRepositoryInterface $awbRepository,
        public ScopeConfigInterface $scopeConfig
    )
    {
        $this->carierId = $request->getParam('carrier_id') ?? CarrierType::defaultCarrierId();
        $awbId = $request->getParam('entity_id');
        if (!empty($awbId)) {
            $model = $awbRepository->getById($awbId);
            $this->carierId = $model->getData('carrier_id');
        }

        $this->setCarrier();
    }

    public function setCarrierById($carrierId)
    {
        $this->carierId = $carrierId;
        $this->setCarrier();
    }

    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;
            case CarrierType::SAMEDAY:
                $this->pickupId = $this->pickupSameday;
                break;
        }
    }

    /**
     * @return array
     * @deprecated
     */
    public function getAvailable()
    {
        $pickupLocation = $this->pickupId->getAvailable();

        return $pickupLocation;
    }

}
