<?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\Tariff as TariffDPD;
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\Tariff as NemoExpress;
use Eadesigndev\Sameday\Model\Sources\Tariff as TariffSameday;
use Eadesigndev\Awb\Model\Sources\CarrierType;
use Eadesigndev\Awb\Api\AwbRepositoryInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;


/**
 * Class Tariff
 * @package Eadesigndev\Awb\ModelSource
 */
#[\AllowDynamicProperties]
class Tariff extends AbstractSource
{
    public $carierId;
    public $tarrif;

    public static $settingsFieldKey = "tariff_plan";

    /**
     * @var ConnectUrgent
     */
    private $connectUrgent;

    /**
     * @var ScopeConfigInterface
     */
    public ScopeConfigInterface $scopeConfig;

    /**
     * Tariff constructor.
     * @param ConnectUrgent $connectUrgent
     */

    public function __construct(
        Http $request,
        TariffFanCourier $pickupIdFanCourier,
        TariffUrgent $pickupIdUrgent,
        TariffDPD $pickupIdDPD,
        TariffGLS $pickupIdGLS,
        TariffDragonStar $tariffDragonStar,
        TariffXCurier $tariffXCurier,
        NemoExpress $tariffNemoExpress,
        TariffSameday $tariffSameday,
        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;
            case CarrierType::SAMEDAY:
                $this->tarrif = $tariffSameday;
                break;
        }

        $this->scopeConfig = $scopeConfig;
    }

    /**
     * @param $array
     * @param $key
     * @param $value
     * @return array
     */
    public function toOptionArrayReplaceList($array, $key, $value): ?array
    {
        if (!empty($array)) {
            $return = [];
            foreach ($array as $element) {
                $return[$element[$key]] = $element[$value];
            }
            return $return;
        }
    }

    /**
     * @return array
     * @deprecated
     */
    public function getAvailable()
    {
        $pickupLocation = $this->tarrif->getAvailable();

        return $pickupLocation;
    }
}
