<?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\Tariff as TariffUrgent;
use Eadesigndev\FanCourier\Model\Sources\Tariff as TariffFanCourier;
use Eadesigndev\DPD\Model\Sources\DeliveryPayment as DPD;
use Eadesigndev\GLS\Model\Sources\Tariff as TariffGLS;
use Eadesigndev\DragonStar\Model\Sources\Tariff as TariffDragonStar;
use Eadesigndev\xCurier\Model\Sources\Tariff as TariffXCurier;
use Eadesigndev\NemoExpress\Model\Sources\DeliveryPayment as NemoExpress;
use Eadesigndev\Awb\Model\Sources\CarrierType;
use Eadesigndev\Awb\Api\AwbRepositoryInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;
/**
 * Class DeliveryPayment
 * @package Eadesigndev\Awb\ModelSource
 */
#[\AllowDynamicProperties]
class DeliveryPayment extends AbstractSource
{
    /**
     * DeliveryPayment
     */
    const EXPEDITOR = 0;
    const DESTINATAR = 1;

    public static $settingsFieldKey = "delivery_payment";

    public $carierId;
    public $tarrif;
    public ScopeConfigInterface $scopeConfig;

    public function __construct(
        Http $request,
        TariffFanCourier $pickupIdFanCourier,
        TariffUrgent $pickupIdUrgent,
        DPD $pickupIdDPD,
        TariffGLS $pickupIdGLS,
        TariffDragonStar $tariffDragonStar,
        TariffXCurier $tariffXCurier,
        NemoExpress $tariffNemoExpress,
        AwbRepositoryInterface $awbRepository,
        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');
        }

        switch ($this->carierId) {
            case CarrierType::URGENT_CURRIER:
                $this->tarrif = $pickupIdUrgent;
                break;
            case CarrierType::FAN_CURRIER:
                $this->tarrif = $pickupIdFanCourier;
                break;
            case CarrierType::DPD:
                $this->tarrif = $pickupIdDPD;
                break;
            case CarrierType::GLS:
                $this->tarrif = $pickupIdGLS;
                break;
            case CarrierType::DRAGON_STAR:
                $this->tarrif = $tariffDragonStar;
                break;
            case CarrierType::X_CURIER:
                $this->tarrif = $tariffXCurier;
                break;
            case CarrierType::NEMO_EXPRESS:
                $this->tarrif = $tariffNemoExpress;
                break;
        }

        $this->scopeConfig = $scopeConfig;
    }

    /**
     * @return array
     */
    public function getAvailable()
    {
        if ($this->carierId == CarrierType::NEMO_EXPRESS) {
            return $this->tarrif->getAvailable();
        } else if($this->carierId == CarrierType::DPD) {
            return $this->tarrif->getAvailable();
        }

        $deliveryPayment = [
            self::EXPEDITOR => __('Expeditor'),
            self::DESTINATAR => __('Destinatar'),
        ];

        return $deliveryPayment;
    }
}
