<?php
/**
 * Copyright © 2018 EaDesign by Eco Active S.R.L. All rights reserved.
 * See LICENSE for license details.
 */

namespace Eadesigndev\Awb\Model\Sources;

use Eadesigndev\Awb\Helper\Data;
use Magento\Framework\App\Request\Http;
use Eadesigndev\DPD\Model\Sources\PaymentMethod as PaymentMethodDPD;
use Eadesigndev\Awb\Model\Sources\CarrierType;
use Eadesigndev\Awb\Api\AwbRepositoryInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;

/**
 * Class PaymentMethod
 * @package Eadesigndev\Awb\ModelSource
 */
#[\AllowDynamicProperties]
class PaymentMethod extends AbstractSource
{
    /**
     * @var Data
     */
    public $helper;

    /**
     * PaymentMethod
     */
    const RAMBURS = 0;
    const CONT    = 1;

    public static $settingsFieldKey = "payment_method";

    public $carierId;
    public $con = null;
    public ScopeConfigInterface $scopeConfig;

    public function __construct(
        Http $request,
        AwbRepositoryInterface $awbRepository,
        ScopeConfigInterface $scopeConfig,
        PaymentMethodDPD $paymentMethodDPD,
        Data $helper
    )
    {
        $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::DPD:
                $this->con = $paymentMethodDPD;
                break;
        }

        $this->scopeConfig = $scopeConfig;
        $this->helper = $helper;
    }

    /**
     * @return array
     */
    public function getAvailable()
    {
        if (!empty($this->con)) {
            return $this->con->getAvailable();
        }

        $paymentMethod = [
            self::RAMBURS => __('Ramburs'),
            self::CONT => __('Cont Colector'),
        ];

        return $paymentMethod;
    }
}