<?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 Magento\Framework\App\Config\ScopeConfigInterface;
use Eadesigndev\Awb\Helper\Fields;
/**
 * Class CarrierType
 * @package Eadesigndev\Awb\ModelSource
 */
#[\AllowDynamicProperties]
class CarrierType extends AbstractSource
{
    public $carierId;
    private $_scopeConfig;
    private $_excludeCarrierFields = [];
    private $_fieldsHelper;
    /**
     * CarrierType
     */
    const URGENT_CURRIER = 1;
    const FAN_CURRIER = 2;
    const DPD = 3;
    const GLS = 4;
    const DRAGON_STAR = 5;
    const X_CURIER = 6;
    const NEMO_EXPRESS = 7;
    const SAMEDAY = 8;
    const PACKETA_FAN_COURIER = 9;
    const PACKETA_SAMEDAY = 10;
    
    public static $carriersSettingsPath = [
        self::URGENT_CURRIER => "eadesigndevurgent",
        self::FAN_CURRIER => "fancourier_customshipping",
        self::DPD => "dpd",
        self::GLS => "gls",
        self::DRAGON_STAR => "dragon",
        self::NEMO_EXPRESS => "nemoexpress",
        self::SAMEDAY => "sameday"
    ];

    public static $carriersSettingsPathPattern = "carriers/CARRIERCODE/active";

    public static $carriersRequirePostcode = [
        self::DPD,
        self::GLS
    ];

    public static $carriersCitySelector = [
        self::X_CURIER
    ];

    public function __construct(
        Http $request,
        ScopeConfigInterface $scopeConfig,
        Fields $fieldsHelper
    )
    {
        $this->carierId = $request->getParam('carrier_id') ?? self::defaultCarrierId();
        $this->_excludeCarrierFields = $fieldsHelper->getExcludeFieldsByCarrier($this->carierId);
        $this->_scopeConfig = $scopeConfig;
        $this->_fieldsHelper = $fieldsHelper;
    }

    /**
     * @return array
     */
    public function getAvailable($onlyActive = false)
    {
        $carrierType = self::getAvailableStatic();

        if ($onlyActive) {
            foreach ($carrierType as $carrierId => $carrierName) {
                if (empty(self::$carriersSettingsPath[$carrierId]) || !$this->_scopeConfig->getValue(str_replace("CARRIERCODE", self::$carriersSettingsPath[$carrierId], self::$carriersSettingsPathPattern))) {
                    unset($carrierType[$carrierId]);
                }
            }
        }

        return $carrierType;
    }

    public static function defaultCarrierId()
    {
        return self::URGENT_CURRIER;
    }

    public static function getAvailableStatic()
    {
        $availableCarriers = [
            self::URGENT_CURRIER => __('Urgent Cargus'),
            self::FAN_CURRIER => __('FAN Courier'),
            self::DPD => __('DPD'),
            self::GLS => __('GLS'),
            self::DRAGON_STAR => __('Dragon Star'),
            self::X_CURIER => __('xCurier'),
            self::NEMO_EXPRESS => __('Nemo Express'),
            self::SAMEDAY => __('Sameday Courier'),
            self::PACKETA_SAMEDAY => __('Packeta Sameday'),
        ];

        return $availableCarriers;
    }

    public function isPostcodeRequired()
    {
        return in_array($this->carierId, self::$carriersRequirePostcode);
    }

    public function hasCitySelector()
    {
        return in_array($this->carierId, self::$carriersCitySelector);
    }

    public function weNeedThisField($fieldName)
    {
        return !in_array($fieldName, $this->_excludeCarrierFields);
    }

    public function setCarrierId($carrierId)
    {
        $this->carierId = $carrierId;
        $this->_excludeCarrierFields = $this->_fieldsHelper->getExcludeFieldsByCarrier($this->carierId);
    }
}
