<?php

declare(strict_types=1);

namespace External\PompoBundle\Admin\Tabs;

use External\PompoBundle\Exception\PompoException;
use External\PompoBundle\Util\PompoContentUtil;
use KupShop\AdminBundle\Util\ActivityLog;
use Query\Operator;

class ProducersTab extends SectionsTab
{
    protected $title = 'flapProducersPompo';

    protected $template = 'window/producers.pompo.tpl';

    public static function getTypes()
    {
        return [
            'producers' => -1,
        ];
    }

    public function getVars($smarty_tpl_vars)
    {
        return $smarty_tpl_vars;
    }

    public function handleUpdate()
    {
        if (!$this->getID()) {
            return;
        }

        if (!empty($_FILES['producerCustomContent']['tmp_name'])) {
            $this->uploadCustomContent($_FILES['producerCustomContent']);
        }
    }

    public function getLabel()
    {
        return 'Ostatní';
    }

    public function handleDeleteCustomContent(): void
    {
        if ($this->pompoContentUtil->deleteCustomContent((string) $this->getID(), PompoContentUtil::CONTENT_TYPE_PRODUCER)) {
            $this->updateProducerCustomData('customContent', 0);
        }

        $this->window->returnOK();
    }

    public function handleDownloadCustomContent(): void
    {
        try {
            $this->pompoContentUtil->downloadCustomContent((string) $this->getID(), PompoContentUtil::CONTENT_TYPE_PRODUCER);
        } catch (PompoException $e) {
            addActivityLog(
                ActivityLog::SEVERITY_ERROR,
                ActivityLog::TYPE_CHANGE,
                $e->getMessage(),
                $e->getData()
            );

            $this->window->returnError($e->getMessage());
        }

        $this->window->returnOK();
    }

    private function uploadCustomContent(array $file): void
    {
        try {
            if ($this->pompoContentUtil->uploadContent($file, (string) $this->getID(), PompoContentUtil::CONTENT_TYPE_PRODUCER)) {
                $this->updateProducerCustomData('customContent', 1);
            }
        } catch (PompoException $e) {
            $this->window->returnError($e->getMessage());
        }

        $this->window->returnOK();
    }

    private function updateProducerCustomData(string $key, $value): void
    {
        $data = sqlQueryBuilder()
            ->select('data')
            ->from('producers')
            ->where(Operator::equals(['id' => $this->getID()]))
            ->execute()->fetchOne();

        $data = json_decode($data ?? '', true) ?? [];

        $data[$key] = $value;

        sqlQueryBuilder()
            ->update('producers')
            ->directValues(
                [
                    'data' => json_encode($data),
                ]
            )
            ->where(Operator::equals(['id' => $this->getID()]))
            ->execute();
    }

    private function getZipMimeTypes(): array
    {
        return ['application/zip', 'application/octet-stream', 'application/x-zip-compressed', 'multipart/x-zip'];
    }
}
