<?php
/**
 * Copyright © 2018 EaDesign by Eco Active S.R.L. All rights reserved.
 * See LICENSE for license details.
 */

namespace Eadesigndev\Awb\Ui\Component\Listing\Column;

use Magento\Framework\View\Element\UiComponent\ContextInterface;
use Magento\Framework\View\Element\UiComponentFactory;
use Magento\Ui\Component\Listing\Columns\Column;
use Magento\Framework\UrlInterface;

#[\AllowDynamicProperties] class PostActions extends Column
{
    /** Url path */
    const AWB_TEMPLATE_EDIT = 'shipping_awb/index/edit';
    const AWB_TEMPLATE_DELETE = 'shipping_awb/index/delete';
    const AWB_TEMPLATE_PRINT = 'shipping_awb/index/preview';

    /** @var UrlInterface */
    private $urlBuilder;

    /**
     * @var string
     */
    private $editUrl;
    private $printUrl;
    private $deleteUrl;

    /**
     * @param ContextInterface $context
     * @param UiComponentFactory $uiComponentFactory
     * @param UrlInterface $urlBuilder
     * @param array $components
     * @param array $data
     * @param string $editUrl
     */
    public function __construct(
        ContextInterface $context,
        UiComponentFactory $uiComponentFactory,
        UrlInterface $urlBuilder,
        array $components = [],
        array $data = [],
        $editUrl = self::AWB_TEMPLATE_EDIT,
        $printUrl = self::AWB_TEMPLATE_PRINT,
        $deleteUrl = self::AWB_TEMPLATE_DELETE
    ) {
        $this->urlBuilder = $urlBuilder;
        $this->editUrl = $editUrl;
        $this->printUrl = $printUrl;
        $this->deleteUrl = $deleteUrl;
        parent::__construct($context, $uiComponentFactory, $components, $data);
    }

    /**
     * Prepare Data Source
     *
     * @param array $dataSource
     * @return array
     */
    public function prepareDataSource(array $dataSource)
    {
        if (isset($dataSource['data']['items'])) {
            foreach ($dataSource['data']['items'] as & $item) {
                $name = $this->getData('name');
                if (isset($item['entity_id'])) {
                    $item[$name]['edit'] = [
                        'href' => $this->urlBuilder->getUrl(
                            $this->editUrl,
                            ['entity_id' => $item['entity_id']]
                        ),
                        'label' => __('Edit')
                    ];
                    $item[$name]['print'] = [
                        'href' => $this->urlBuilder->getUrl(
                            $this->printUrl,
                            [
                                'carrier' => $item['carrier_id'],
                                'identifier' => $item['awb_number'],
                                'pickup_id' => $item['awb_pickup_id']
                            ]
                        ),
                        'label' => __('Print')
                    ];
                    $item[$name]['delete'] = [
                        'href' => $this->urlBuilder->getUrl(
                            $this->deleteUrl,
                            [
                                'carrier' => $item['carrier_id'],
                                'identifier' => $item['awb_number'],
                                'entity_id' => $item['entity_id']
                            ]
                        ),
                        'label' => __('Delete')
                    ];
                }
            }
        }

        return $dataSource;
    }
}
