<?php

namespace KupShop\UserManagerBundle\View;

use KupShop\KupShopBundle\Context\ContextManager;
use KupShop\KupShopBundle\Context\LanguageContext;
use KupShop\KupShopBundle\Context\UserContext;
use KupShop\KupShopBundle\Email\PasswordResetAdminEmail;
use KupShop\KupShopBundle\Exception\RedirectException;
use KupShop\KupShopBundle\Util\Contexts;
use KupShop\KupShopBundle\Views\View;
use KupShop\UserManagerBundle\Util\UserManagerUtil;
use Query\Operator;

class UserManagerView extends View
{
    protected $template = 'user-manager/users.tpl';

    private $contextManager;

    private $userManagerUtil;

    private $passwordResetAdminEmail;

    public function __construct(
        ContextManager $contextManager,
        UserManagerUtil $userManagerUtil,
        PasswordResetAdminEmail $passwordResetAdminEmail,
    ) {
        $this->contextManager = $contextManager;
        $this->userManagerUtil = $userManagerUtil;
        $this->passwordResetAdminEmail = $passwordResetAdminEmail;
    }

    public function getBodyVariables()
    {
        $vars = parent::getBodyVariables();

        $userContext = Contexts::get(UserContext::class);

        if (!$userContext->getActiveId() || !$this->userManagerUtil->isUserManager($userContext->getActiveId())) {
            throw new RedirectException(
                path('account')
            );
        }

        $vars['users'] = $this->userManagerUtil->getManagedUsers(
            $userContext->getActiveId()
        );

        return $vars;
    }

    public function getTitle()
    {
        return translate('titleList', 'UserManager');
    }

    public function sendSetPasswordEmail(int $userId): void
    {
        if ($user = \User::createFromId($userId)) {
            $message = $this->contextManager->activateContexts(
                [
                    LanguageContext::class => $user->id_language ?? null,
                ],
                function () use ($user) {
                    $this->passwordResetAdminEmail->setUser($user);

                    return $this->passwordResetAdminEmail->getEmail();
                }
            );

            $message['to'] = $user->email;

            if ($this->passwordResetAdminEmail->sendEmail($message)) {
                $this->addSuccessMessage(translate('passwordEmailSentSuccess', 'UserManager'));
            } else {
                $this->addErrorMessage(translate('passwordEmailSentError', 'UserManager'));
            }
        }
    }

    public function updateUserFigure(int $userId, string $figure): void
    {
        if ($user = \User::createFromId($userId)) {
            if (in_array($figure, ['Y', 'N'])) {
                sqlQueryBuilder()
                    ->update('users')
                    ->directValues(['figure' => $figure])
                    ->where(Operator::equals(['id' => $userId]))
                    ->execute();

                $this->addSuccessMessage(translate('updateFigureSuccess', 'UserManager'));
            }
        }
    }
}
