<?php

namespace KupShop\ElninoBundle\Admin\Tabs;

use KupShop\AdminBundle\Admin\WindowTab;
use KupShop\ElninoBundle\Util\ReclamationReturnTicket;

class ElninoTicketsSettingsTab extends WindowTab
{
    /** @var ReclamationReturnTicket */
    protected $updater;

    protected $title = 'flapElninoTickets';

    protected $template = 'ElninoTicketsSettingsTab.tpl';

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

    public function getVars($smarty_tpl_vars)
    {
        $vars = [];
        if (findModule(\Modules::RECLAMATIONS)) {
            $vars['elninoTickets']['reclamations'] = translate('reclamations', 'elninoTicketsSettingsTab');
        }

        if (findModule(\Modules::RETURNS)) {
            $vars['elninoTickets']['returns'] = translate('returns', 'elninoTicketsSettingsTab');
        }

        return $vars;
    }

    public function handleTicketsSync()
    {
        $settings = \Settings::getDefault();

        if (findModule(\Modules::RECLAMATIONS) && $settings['elnino_customer_service_api']['reclamations']['active'] == 'Y') {
            $this->updater->processReclamations();
            $this->addHTMLError(translate('reclamationsDone', 'elninoTicketsSettingsTab'));
        }

        if (findModule(\Modules::RETURNS) && $settings['elnino_customer_service_api']['returns']['active'] == 'Y') {
            $this->updater->processReturns();
            $this->addHTMLError(translate('returnsDone', 'elninoTicketsSettingsTab'));
        }
    }

    /**
     * @required
     */
    public function setUpdater(ReclamationReturnTicket $updater): void
    {
        $this->updater = $updater;
    }
}
