<?php

use KupShop\AdminBundle\AdminBlocksTrait;
use KupShop\AdminBundle\Util\BlocksHistory;
use KupShop\ContentBundle\Util\BlocksTrait;
use KupShop\KupShopBundle\Util\Compat\ServiceContainer;
use Query\Operator;

$main_class = 'SectionProducer';

class SectionProducer extends Window
{
    use BlocksTrait;
    use AdminBlocksTrait;

    protected $tableName = 'section_producer';

    /** @var BlocksHistory */
    private $blocksHistory;

    public function __construct()
    {
        $this->blocksHistory = ServiceContainer::getService(BlocksHistory::class);
    }

    public function getIDs()
    {
        return array_combine(['id_section', 'id_producer'], explode('-', $this->getID()));
    }

    protected function getObject()
    {
        $id = $this->getIDs();
        $object = sqlFetch($this->selectSQL($this->getTableName(), $id));
        $object['id'] = $this->getID();

        return $object;
    }

    public function get_vars()
    {
        $vars = parent::get_vars();
        $body = &$vars['body'];

        $id = $this->getIDs();
        $body['producer'] = $this->fetchObject('producers', $id['id_producer']);
        $body['section'] = $this->fetchObject('sections', $id['id_section']);

        if (findModule('sliders')) {
            $body['sliders'] = sqlFetchAll($this->selectSQL('sliders', []), ['id' => 'name']);
        }

        if (isset($body['data']['id_block'])) {
            $body['data']['blocks'] = $this->getBlocks($body['data']['id_block']);
            $body['data']['blocks_history'] = $this->blocksHistory->getBlocksHistory($body['data']['id_block']);
        }

        return $vars;
    }

    public function handleUpdate()
    {
        $data = $this->getData();

        $id = $this->getIDs();
        $sqlFields = $this->getSQLFields();
        $fields = array_diff_key($sqlFields, $id);
        $blocks = getVal('blocks', $data, []);
        unset($blocks[0]); // ignore block with index 0 (it is the default one)
        $empty = empty(array_filter($fields)) && empty($blocks);

        $ID = sqlQueryBuilder()
            ->select('id')->from('section_producer')
            ->andWhere(Operator::equals($id))
            ->execute()->fetchOne();
        if ($ID) {
            if ($empty) {
                $this->deleteSQL($this->getTableName(), $id);
                $ID = null;
            } else {
                $this->updateSQL($this->getTableName(), $sqlFields, $id);
            }
        } elseif (!$empty) {
            $this->insertSQL($this->getTableName(), $sqlFields);
            $ID = sqlInsertId();
        }

        if ($ID) {
            // historii je treba ulozit jeste pred ulozenim bloku
            $this->blocksHistory->saveBlocksHistory($blocks);
            $this->saveBlocks($data, $ID, 'section_producer');
        }

        return true;
    }

    public function hasRights($name = null)
    {
        switch ($name) {
            case Window::RIGHT_DELETE:
            case Window::RIGHT_DUPLICATE:
                return false;
            default:
                return parent::hasRights($name);
        }
    }
}
