<?php

namespace KupShop\StoresBundle\Admin\Actions;

use KupShop\AdminBundle\Admin\Actions\AbstractAction;
use KupShop\AdminBundle\Admin\Actions\ActionResult;
use KupShop\AdminBundle\Admin\Actions\IAction;
use KupShop\AdminBundle\Util\ActivityLog;
use KupShop\StoresBundle\StoresBundle;
use KupShop\StoresBundle\Utils\StoresTransfersImport;

class StoresTransfersImportAction extends AbstractAction implements IAction
{
    /**
     * @var StoresTransfersImport
     */
    protected $storesTransferImport;

    public function getTypes(): array
    {
        return ['storesTransfers'];
    }

    public function getName(): string
    {
        return 'Importovat ze souboru';
    }

    public function execute(&$data, array $config, string $type): ActionResult
    {
        $return = $this->storesTransferImport->importCSV($_FILES['import'], $this->getId());

        if ($return !== true) {
            return new ActionResult(false, $return);
        }

        addActivityLog(ActivityLog::TYPE_IMPORT, ActivityLog::SEVERITY_SUCCESS, "Převodka {$this->getId()} importována.", tags: [StoresBundle::LOG_TAG_STORES]);

        return new ActionResult(true, 'Převodka naimportována.');
    }

    /**
     * @required
     */
    public function setStoresTransferImport(StoresTransfersImport $storesTransferImport): void
    {
        $this->storesTransferImport = $storesTransferImport;
    }

    public function isVisible()
    {
        return ($this->dialogData['state'] ?? '') == 'O';
    }
}
