<?php

declare(strict_types=1);

namespace External\PompoBundle\Admin\Tabs;

use External\PompoBundle\DataGo\Synchronizer\ProductSynchronizer;
use External\PompoBundle\DataGo\Util\PhotoUpdater;
use External\PompoBundle\Util\PompoUtil;
use KupShop\AdminBundle\Admin\WindowTab;
use KupShop\DropshipBundle\Util\DropshipmentUtil;
use KupShop\KupShopBundle\Util\Functional\Mapping;
use KupShop\KupShopBundle\Util\StringUtil;

class PompoSettingsTab extends WindowTab
{
    public const MARKETPLACE_DEFAULT_KEY = 'marketplace-default';

    protected ?DropshipmentUtil $dropshipmentUtil;

    /** @required */
    public PhotoUpdater $photoUpdater;

    /** @required */
    public PompoUtil $pompoUtil;

    protected $title = 'flapSettingsPompo';

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

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

    public function getVars($smarty_tpl_vars)
    {
        $dbcfg = \Settings::getDefault();
        $pompo = $dbcfg->loadValue('pompo');
        $dataGo = $dbcfg->loadValue('datago');

        $photosUpdate = null;
        if ($timestamp = $pompo['photosUpdate']['timestamp'] ?? null) {
            $photosUpdate = (new \DateTime())->setTimestamp($timestamp)->format('d.m.Y H:i:s');
        }

        $newProductsLastSync = null;
        if ($newProductsTimestamp = $dataGo['product'][ProductSynchronizer::PROCESS_TYPE_NEW]['updated'] ?? null) {
            $newProductsLastSync = (new \DateTime())->setTimestamp($newProductsTimestamp)->format('d.m.Y H:i:s');
        }

        $diffProductsLastSync = null;
        if ($diffProductsTimestamp = $dataGo['product'][ProductSynchronizer::PROCESS_TYPE_DIFF]['updated'] ?? null) {
            $diffProductsLastSync = (new \DateTime())->setTimestamp($diffProductsTimestamp)->format('d.m.Y H:i:s');
        }

        return [
            'photosUpdateLastSync' => $photosUpdate,
            'newProductsLastSync' => $newProductsLastSync,
            'diffProductsLastSync' => $diffProductsLastSync,
            'expandoMarketplaces' => $this->getExpandoMarketplaces(),
        ];
    }

    public function handleSyncPhotos(): void
    {
        $this->photoUpdater->updatePhotosLatestChanges(true);

        $this->window->returnOK('Obrázky úspěšně sesynchronizovány');
    }

    public function handleSyncNewProducts(): void
    {
        $this->pompoUtil->synchronizeDataGoProducts([ProductSynchronizer::PROCESS_TYPE_NEW]);

        $this->window->returnOK('Soubor s novýma produktama byl úspěšně sesynchronizován');
    }

    public function handleSyncDiffProducts(): void
    {
        $this->pompoUtil->synchronizeDataGoProducts([ProductSynchronizer::PROCESS_TYPE_DIFF]);

        $this->window->returnOK('Soubor se změnama v produktech byl úspěšně sesynchronizován');
    }

    public function getLabel()
    {
        return 'Pompo';
    }

    private function getExpandoMarketplaces(): array
    {
        if (!$this->dropshipmentUtil) {
            return [];
        }

        $expando = $this->dropshipmentUtil->getDropshipment(1);

        $marketplaces = array_filter(Mapping::mapKeys($expando['configuration']['marketplaces'] ?? [], fn ($k, $v) => [StringUtil::slugify($v['name']), $v['name']]));
        // add default marketplace
        $marketplaces[self::MARKETPLACE_DEFAULT_KEY] = '';

        return $marketplaces;
    }

    /**
     * @required
     */
    public function setDropshipmentUtil(?DropshipmentUtil $dropshipmentUtil = null): void
    {
        $this->dropshipmentUtil = $dropshipmentUtil;
    }
}
