<?php

namespace ModulesGarden\Servers\OpenXchange\Core\App\Controllers\Instances;

use ModulesGarden\Servers\OpenXchange\Core\App\Controllers\Http\PageNotFound;
use ModulesGarden\Servers\OpenXchange\Core\App\Controllers\Interfaces\DefaultController;
use ModulesGarden\Servers\OpenXchange\Core\Helper;

abstract class AddonController implements DefaultController
{
    use \ModulesGarden\Servers\OpenXchange\Core\Traits\Lang;
    use \ModulesGarden\Servers\OpenXchange\Core\Traits\OutputBuffer;
    use \ModulesGarden\Servers\OpenXchange\Core\Traits\IsAdmin;
    use \ModulesGarden\Servers\OpenXchange\Core\UI\Traits\RequestObjectHandler;
    use \ModulesGarden\Servers\OpenXchange\Core\Traits\ErrorCodesLibrary;
    use \ModulesGarden\Servers\OpenXchange\Core\Traits\AppParams;

    public function runExecuteProcess($params = null)
    {
        $resault = $this->execute($params);
        if ($this->isValidIntegrationCallback($resault))
        {
            $this->setAppParam('IntegrationControlerName', $resault[0]);
            $this->setAppParam('IntegrationControlerMethod', $resault[1]);

            $resault = Helper\di($resault[0], $resault[1]);
        }

        if ($resault instanceof \ModulesGarden\Servers\OpenXchange\Core\UI\ViewAjax)
        {
            $this->resolveAjax($resault);
        }

        if (!$resault instanceof \ModulesGarden\Servers\OpenXchange\Core\UI\ViewIntegrationAddon)
        {
            return $resault;
        }

        $addonIntegration = $this->getIntegrationControler($params['action']);

        return $addonIntegration->runExecuteProcess($resault);

    }

    public function isValidIntegrationCallback($callback = null)
    {
        if (is_array($callback) && !empty($callback[0]) && !empty($callback[0]) && method_exists($callback[0], $callback[1]))
        {
            return true;
        }

        return false;
    }

    public function resolveAjax($resault)
    {
        $ajaxResponse = $resault->getResponse();

        $resolver = new \ModulesGarden\Servers\OpenXchange\Core\App\Controllers\ResponseResolver($ajaxResponse);

        $resolver->resolve();
    }

    protected function getIntegrationControler($action = null)
    {
        switch ($action)
        {
            case 'ConfigOptions':
                return Helper\di(\ModulesGarden\Servers\OpenXchange\Core\App\Controllers\Instances\Http\ConfigOptionsIntegration::class);
                break;
            case 'AdminServicesTabFields':
                return Helper\di(\ModulesGarden\Servers\OpenXchange\Core\App\Controllers\Instances\Http\AdminServicesTabFieldsIntegration::class);
                break;
            default:
                return Helper\di(\ModulesGarden\Servers\OpenXchange\Core\App\Controllers\Instances\Http\AddonIntegration::class);
        }
    }
}
