<?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\PohodaBundle\Utils\Importers\ProductsImporter;
use KupShop\StoresBundle\Utils\StoresInStore;
use Query\Operator;

class StoreChangeTypeAction extends AbstractAction implements IAction
{
    /**
     * @var ProductsImporter
     */
    protected $productsImporter;
    /**
     * @var StoresInStore
     */
    protected $storesInStore;

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

    public function getName(): string
    {
        return 'Změnit typ skladu';
    }

    public function execute(&$data, array $config, string $type): ActionResult
    {
        return sqlGetConnection()->transactional(function () {
            $store = $this->storesInStore->getStores()[$this->getId()];
            $data = getVal('data');

            if ($data['type'] == StoresInStore::TYPE_MAIN_STORE) {
                $store = sqlQueryBuilder()->select('*')->from('stores')->where(\Query\Operator::equals(['type' => StoresInStore::TYPE_MAIN_STORE]))->execute()->fetchAssociative();

                if (!empty($store)) {
                    return new ActionResult(false, "Nelze změnit na hlavní sklad. Jako hlavní sklad je již nastaven sklad {$store['name']} - ID {$store['id']}.");
                }
            }

            if ($store['type'] != $data['type']) {
                sqlQueryBuilder()->update('stores')->set('type', $data['type'])->where(Operator::equals(['id' => $this->getId()]))->execute();

                $this->storesInStore->recalcInStoreFromStores();

                if ($store['id_delivery'] && $data['change_delivery_figure'] ?? false) {
                    $qb = sqlQueryBuilder()->update('delivery_type')->where(Operator::equals(['id_delivery' => $store['id_delivery']]));
                    switch ($data['type']) {
                        case StoresInStore::TYPE_STORE:
                            $qb->directValues(['figure' => 'Y']);
                            break;
                        case StoresInStore::TYPE_EXTERNAL_STORE:
                            $qb->directValues(['figure' => 'N']);
                            break;
                        default:
                            throw new \Exception('Undefined type store in change type action!');
                    }
                    $qb->execute();
                }
            } else {
                return new ActionResult(false, 'Vybrán stejný typ skladu. Nezměněno.');
            }

            return new ActionResult(true, 'Typ skladu změněn.');
        });
    }

    public function getVars(): array
    {
        $data['store'] = $this->storesInStore->getStores()[$this->getId()];
        $data['types'] = StoresInStore::$store_types;

        return $data;
    }

    /**
     * @required
     */
    public function setStoresInStore(?StoresInStore $storesInStore): void
    {
        $this->storesInStore = $storesInStore;
    }

    /**
     * @required
     */
    public function setProductsImporter(?ProductsImporter $productsImporter): void
    {
        $this->productsImporter = $productsImporter;
    }
}
