<?php
/**
 * Copyright © 2018 EaDesign by Eco Active S.R.L. All rights reserved.
 * See LICENSE for license details.
 */
namespace Eadesigndev\Awb\Block\Adminhtml\Xtea\Edit\Tab;

use Eadesigndev\Awb\Model\Awb;
use Eadesigndev\Awb\Model\Sources\CarrierType;
use Magento\Backend\Block\Template\Context;
use Magento\Backend\Block\Widget\Tab\TabInterface;
use Magento\Framework\Data\Form;
use Magento\Framework\Data\FormFactory;
use Magento\Framework\Phrase;
use Magento\Framework\Registry;
use Magento\Backend\Block\Widget\Form\Generic;
use Magento\Config\Model\Config\Source\Yesno;
use Magento\Store\Model\System\Store as SystemStore;
use Eadesigndev\Awb\Model\Sources\StreetType;
use Eadesigndev\Awb\Model\Sources\City as CitySource;
/**
 * Class General
 * @package Eadesigndev\Awb\Block\Adminhtml\Xtea\Edit\Tab
 */
#[\AllowDynamicProperties]
class General extends Generic implements TabInterface
{
    /**
     * @var Yesno
     */
    private $yesNo;

    /**
     * @var SystemStore
     */
    private $systemStore;

    /**
     * @var Registry
     */
    private $registry;

    private $carrierType;
    private $_streetType;

    public function __construct(
        Context $context,
        Registry $registry,
        FormFactory $formFactory,
        Yesno $yesNo,
        SystemStore $systemStore,
        CarrierType $carrierType,
        StreetType $streetType,
        CitySource $citySource,
        array $data = []
    ) {
        $this->registry    = $registry;
        $this->yesNo       = $yesNo;
        $this->systemStore = $systemStore;
        $this->carrierType = $carrierType;
        $this->_streetType = $streetType;
        $this->_citySource = $citySource;
        parent::__construct(
            $context,
            $registry,
            $formFactory,
            $data
        );
    }

    /**
     * @return $this
     * @throws \Magento\Framework\Exception\LocalizedException
     */
    public function _prepareForm()
    {
        /** @var Awb $model */
        $model = $this->registry->registry('awb_data');

        if ($carrierId = $model->getCarrierId()) {
            $this->carrierType->setCarrierId($carrierId);
        }

        /** @var Form $form */
        $form = $this->_formFactory->create();

        $fieldSet = $form->addFieldset(
            'base_fieldset',
            [
                'legend' => __('Info')
            ]
        );

        if ($this->carrierType->weNeedThisField('recipient')) {
            $fieldSet->addField(
                'recipient',
                'text',
                [
                    'name' => 'recipient',
                    'label' => __('Recipient'),
                    'title' => __('Recipient'),
                    'required' => true,
                ]
            );
        }

        if ($this->carrierType->weNeedThisField('country_id')) {
            $fieldSet->addField(
                'country_id',
                'text',
                [
                    'name' => 'country_id',
                    'label' => __('Country'),
                    'title' => __('Country'),
                    'required' => true,
                ]
            );
        }

        if ($this->carrierType->weNeedThisField('region_id')) {
            $fieldSet->addField(
                'region_id',
                'text',
                [
                    'name' => 'region_id',
                    'label' => __('State/Province'),
                    'title' => __('State/Province'),
                    'required' => true,
                ]
            );
        }

        if ($this->carrierType->hasCitySelector()) {
            $fieldSet->addField(
                'city',
                'select',
                [
                    'name' => 'city',
                    'label' => __('City'),
                    'title' => __('City'),
                    'required' => true,
                    'values' => $this->_citySource->getAvailable()
                ]
            );
        } else {
            $fieldSet->addField(
                'city',
                'text',
                [
                    'name' => 'city',
                    'label' => __('City'),
                    'title' => __('City'),
                    'required' => true,
                ]
            );
        }

        $fieldSet->addField(
            'street',
            'text',
            [
                'name' => 'street',
                'label' => __('Street'),
                'title' => __('Street'),
                'required' => true,
            ]
        );

        if ($this->carrierType->carierId == CarrierType::DPD) {
            $fieldSet->addField(
                'street_no',
                'text',
                [
                    'name' => 'street_no',
                    'label' => __('Nr. Strada'),
                    'title' => __('Nr. Strada'),
                    'required' => true,
                ]
            );

            $fieldSet->addField(
                'street_type',
                'select',
                [
                    'name' => 'street_type',
                    'label' => __('Tip Strada'),
                    'title' => __('Tip Strada'),
                    'required' => true,
                    'values' => $this->_streetType->getAvailable()
                ]
            );
        }

        if ($this->carrierType->weNeedThisField('telephone')) {
            $fieldSet->addField(
                'telephone',
                'text',
                [
                    'name' => 'telephone',
                    'label' => __('Telephone'),
                    'title' => __('Telephone'),
                    'required' => true,
                ]
            );
        }

        if ($this->carrierType->weNeedThisField('customer_email')) {
            $fieldSet->addField(
                'customer_email',
                'text',
                [
                    'name' => 'customer_email',
                    'label' => __('E-mail'),
                    'title' => __('E-mail'),
                    'required' => true,
                ]
            );
        }

        if ($this->carrierType->weNeedThisField('postcode')) {
            $fieldSet->addField(
                'postcode',
                'text',
                [
                    'name' => 'postcode',
                    'label' => __('Cod postal'),
                    'title' => __('Cod postal'),
                    'required' => false,
                ]
            );
        }

        if ($model->getId()) {
            $fieldSet->addField('entity_id', 'hidden', ['name' => 'entity_id']);
        }

        $form->setValues($model->getData());
        $this->setForm($form);

        parent::_prepareForm();

        return $this;
    }

    /**
     * @return Phrase
     */
    public function getTabLabel()
    {
        return __('Address');
    }

    /**
     * Prepare title for tab
     *
     * @return Phrase
     */
    public function getTabTitle()
    {
        return __('Address');
    }

    /**
     * @return bool
     */
    public function canShowTab()
    {
        return true;
    }

    /**
     * @return bool
     */
    public function isHidden()
    {
        return false;
    }


}