<?php

namespace Benhauer\Salesmanago\Controller\Adminhtml\Config;

use Benhauer\Salesmanago\Helper\Config as ConfigHelper;

use Magento\Backend\App\Action;
use Magento\Framework\Message\ManagerInterface;

use SALESmanago\Factories\FactoryOrganizer;
use SALESmanago\Provider\UserProvider;

use Benhauer\Salesmanago\Model\User as UserModel;

class Logout extends \Magento\Backend\App\Action
{
    protected $storeId = 0;
    protected $scope = 'default';
    protected $messageManager;

    public function __construct(
        Action\Context $context,
        UserModel $model,
        FactoryOrganizer $factoryOrganizer,
        ConfigHelper $config
    ) {
        parent::__construct($context);
        $this->model = $model;
        $this->config = $config;
        $this->factoryOrganizer = $factoryOrganizer;
        $this->messageManager = $context->getMessageManager();
    }

    public function execute()
    {
        $this->storeResolver();

        if ($this->model->removePluginConfiguration()) {
            $this->messageManager->addSuccess(__("Success"));
            return $this->redirectTo('login');
        }

        $this->messageManager->addError(__("Something went wrong"));
        return $this->redirectTo('index');
    }

    public function redirectTo($redirectTo = 'index')
    {
        $store = '';
        if (!empty($this->storeId) && $this->storeId != 0) {
            $store = '/store/'.$this->storeId ;
        }
        return $this->_redirect('*/*/'.$redirectTo.$store);
    }

    public function storeResolver()
    {
        $this->storeId = $this->getRequest()->getParam('store');

        if ($this->storeId) {
            $this->model->setScopeId($this->storeId);
            $this->model->setScope('stores');
        }

        return $this;
    }
}
