<?php
/**
 * Copyright © 2018 EaDesign by Eco Active S.R.L. All rights reserved.
 * See LICENSE for license details.
 */

namespace Eadesigndev\Awb\Block\Adminhtml\Xtea;

use Eadesigndev\Awb\Block\Adminhtml\Xtea\Edit\Buttons\DuplicateButton;
use Eadesigndev\Awb\Block\Adminhtml\Xtea\Edit\Buttons\AprobaAwb;
use Eadesigndev\Awb\Model\Sources\StatusType;
use Magento\Backend\Block\Widget\Context;
use Magento\Backend\Block\Widget\Form\Container;
use Magento\Framework\Registry;
use Magento\Framework\App\Request\Http;
use Eadesigndev\Awb\Api\AwbRepositoryInterface;

/**
 * Class Edit
 * @package Eadesigndev\Awb\Block\Adminhtml\Xtea
 */
#[\AllowDynamicProperties]
class Edit extends Container
{
    private $duplicateButton;
    private $_httpRequest;
    private $aprobaAwb;

    /**
     * Core registry
     * @var Registry
     * @var DuplicateButton
     * @var AprobaAwb
     */

    private $coreRegistry = null;

    public function __construct(
        Context $context,
        Registry $registry,
        DuplicateButton $duplicateButton,
        AprobaAwb $aprobaAwb,
        Http $httpRequest,
        AwbRepositoryInterface $awbRepository,
        array $data = []
    ) {

        $this->coreRegistry = $registry;
        $this->duplicateButton = $duplicateButton;
        $this->aprobaAwb = $aprobaAwb;
        $this->_httpRequest = $httpRequest;
        $this->awbRepository = $awbRepository;
        parent::__construct(
            $context,
            $data
        );
    }

    /**
     *
     * @return void
     */
    public function _construct()
    {
        $this->_objectId = 'entity_id';
        $this->_blockGroup = 'Eadesigndev_Awb';
        $this->_controller = 'adminhtml_xtea';

        parent::_construct();

        $this->buttonList->remove('save');
        $this->buttonList->remove('reset');

        $awbId = $this->_httpRequest->getParam('entity_id');

        if (!empty($awbId)) {
            $model = $this->awbRepository->getById($awbId);
        }

        if (empty($model) || (!empty($model) && $model->getStatus() != StatusType::STATUS_DELETED)) {
            $this->getToolbar()->addChild(
                'save-split-button',
                'Magento\Backend\Block\Widget\Button\SplitButton',
                [
                    'id' => 'save-split-button',
                    'label' => __('Save & finish AWB'),
                    'class_name' => 'Magento\Backend\Block\Widget\Button\SplitButton',
                    'button_class' => 'widget-button-update',
                    'options' => [
                        [
                            'id' => 'save-button',
                            'label' => __('Save & finish AWB'),
                            'default' => true,
                            'data_attribute' => [
                                'mage-init' => [
                                    'button' => [
                                        'event' => 'save',
                                        'target' => '#edit_form'
                                    ]
                                ]
                            ]
                        ],
                        [
                            'id' => 'save-continue-button',
                            'label' => __('Save AWB'),
                            'data_attribute' => [
                                'mage-init' => [
                                    'button' => [
                                        'event' => 'saveAndContinueEdit',
                                        'target' => '#edit_form'
                                    ]
                                ]
                            ]
                        ]
                    ]
                ]
            );
        } else {
            $this->buttonList->remove('delete');
        }
    }
}