<?php

namespace ThurData\Servers\KerioEmail\Core\App\Controllers\Instances;

use ThurData\Servers\KerioEmail\Core\App\Controllers\Instances\Http\AddonIntegration;
use ThurData\Servers\KerioEmail\Core\App\Controllers\Instances\Http\AdminServicesTabFieldsIntegration;
use ThurData\Servers\KerioEmail\Core\App\Controllers\Instances\Http\ConfigOptionsIntegration;
use ThurData\Servers\KerioEmail\Core\App\Controllers\ResponseResolver;
use ThurData\Servers\KerioEmail\Core\Http\JsonResponse;
use ThurData\Servers\KerioEmail\Core\UI\ViewAjax;
use ThurData\Servers\KerioEmail\Core\UI\ViewIntegrationAddon;
use ThurData\Servers\KerioEmail\Core\App\Controllers\Interfaces\DefaultController;
use ThurData\Servers\KerioEmail\Core\Helper;

/**
 * Class AddonController
 * @package ThurData\Servers\KerioEmail\Core\App\Controllers\Instances
 */
abstract class AddonController implements DefaultController
{
    use \ThurData\Servers\KerioEmail\Core\Traits\Lang;
    use \ThurData\Servers\KerioEmail\Core\Traits\OutputBuffer;
    use \ThurData\Servers\KerioEmail\Core\Traits\IsAdmin;
    use \ThurData\Servers\KerioEmail\Core\UI\Traits\RequestObjectHandler;
    use \ThurData\Servers\KerioEmail\Core\Traits\ErrorCodesLibrary;
    use \ThurData\Servers\KerioEmail\Core\Traits\AppParams;

    /**
     * @param null $params
     * @return mixed|Helper\type
     */
    public function runExecuteProcess($params = null)
    {
        /**
         * load lang context
         */
        $this->loadLangContext();

        /**
         *
         * result of execute
         */
        $resault = $this->execute($params);

        /**
         *
         * return as json result
         */
        if ($resault instanceof JsonResponse)
        {
            $resolver = new ResponseResolver($resault);

            $resolver->resolve();
        }

        /**
         *
         * check if is callable function & add params
         */
        if ($this->isValidIntegrationCallback($resault))
        {
            $this->setAppParam('IntegrationControlerName', $resault[0]);
            $this->setAppParam('IntegrationControlerMethod', $resault[1]);

            //to do catch exceptions
            $resault = Helper\di($resault[0], $resault[1]);
        }

        /**
         *
         * return if view ajax
         */
        if ($resault instanceof ViewAjax)
        {
            $this->resolveAjax($resault);
        }

        /**
         *  return as view integration addon
         */
        if (!$resault instanceof ViewIntegrationAddon)
        {
            return $resault;
        }

        /**
         *
         */
        if ($resault instanceof JsonResponse)
        {
            $resolver = new ResponseResolver($resault);

            $resolver->resolve();
        }

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

        return $addonIntegration->runExecuteProcess($resault);

    }

    /**
     * @param null $callback
     * @return bool
     */
    public function isValidIntegrationCallback($callback = null)
    {
        if(!is_array($callback))
            {
                return false;
            }
        $callback = array_reverse($callback);
        $class = array_pop($callback);
        $method = array_pop($callback);
         
        return is_string($class) &&  is_string($method) && method_exists($class, $method);
    }

    /**
     * @param $resault
     */
    public function resolveAjax($resault)
    {
        $ajaxResponse = $resault->getResponse();

        $resolver = new ResponseResolver($ajaxResponse);

        $resolver->resolve();
    }

    /**
     * @param null $action
     * @return HttpController | ConfigOptionsIntegration | AdminServicesTabFieldsIntegration | AddonIntegration
     */
    protected function getIntegrationControler($action = null)
    {
        switch ($action)
        {
            case 'ConfigOptions':
                return Helper\di(\ThurData\Servers\KerioEmail\Core\App\Controllers\Instances\Http\ConfigOptionsIntegration::class);
                break;
            case 'AdminServicesTabFields':
                return Helper\di(\ThurData\Servers\KerioEmail\Core\App\Controllers\Instances\Http\AdminServicesTabFieldsIntegration::class);
                break;
            default:
                return Helper\di(\ThurData\Servers\KerioEmail\Core\App\Controllers\Instances\Http\AddonIntegration::class);
        }
    }

    /**
     *
     */
    public function loadLangContext()
    {
        $this->loadLang();

        if ($this->getAppParam('IntegrationControlerName'))
        {
            $parts = explode('\\', $this->getAppParam('IntegrationControlerName'));

            $controller = end($parts);
        }
        else
        {
            $parts = explode('\\', get_class($this));

            $controller = end($parts);
        }

        $this->lang->setContext(($this->getAppParam('moduleAppType') . ($this->isAdmin() ? 'AA' : 'CA')), lcfirst($controller));
    }
}
