<?php
/**
 * @package    Proxim
 * @author     Mike Kivu <mikepaul620@gmail.com | https://webexpert.co.ke>
 * @copyright  2019 Proxim
 * @version    1.5.0
 * @since      File available since Release 1.0.0
 */

namespace Proxim\Dashboard;

use Db;
use Exception;
use Proxim\Database\DbQuery;
use Proxim\Controller;
use Proxim\Pager;
use Proxim\Site\Site;
use Proxim\Configuration;
use Proxim\Module\Module;
use Proxim\Order\Order;
use Proxim\User\Employee;
use Proxim\User\Customer;
use Proxim\Validate;
use Proxim\Tools;
use Proxim\Util\DateUtils;
use Proxim\Util\ArrayUtils;

class Modules extends Controller {

    /*
    ** @var array map with $_GET keywords and their callback
    */
    protected $map = array(
        'check' => 'check',
        'install' => 'install',
        'uninstall' => 'uninstall',
        'configure' => 'getContent',
        'update' => 'update',
        'delete' => 'delete',
        'checkAndUpdate' => 'checkAndUpdate',
        'updateAll' => 'updateAll',
    );

    public function listModules() {
        $app = $this->app;
        $user = $app->container->user;
        $params = $app->request->get();
        $smarty = $app->container->smarty;

        if(!$user->is_admin) {
            $this->setTemplate('404');
            $this->display();
        }

        $this->setVars([
            'page' => [
                'title' => "Modules› Installed"
            ],
        ]);

        $all_modules = Module::getModulesOnDisk();
        $modules_list = [];

        foreach ($all_modules as $module) {
            if($module->installed) {
                $modules_list[$module->name] = $module;
            }
        }

        ksort($modules_list);

        $smarty->assign([
            'view' => 'modules',
            'sub_view' => 'installed',
            'modules_list' => $modules_list
        ]);
        
        $this->setTemplate('dashboard/modules/modules');
        $this->display(); 
    }

    public function installModules() {
        $app = $this->app;
        $user = $app->container->user;
        $params = $app->request->get();
        $smarty = $app->container->smarty;

        if(!$user->is_admin) {
            $this->setTemplate('404');
            $this->display();
        }

        $this->setVars([
            'page' => [
                'title' => "Modules › Add New"
            ],
        ]);

        $all_modules = Module::getModulesOnDisk(true);
        $modules_list = [];

        foreach ($all_modules as $module) {
            if(!$module->installed) {
                $modules_list[$module->name] = $module;
            }
        }

        ksort($modules_list);

        $smarty->assign([
            'view' => 'modules',
            'sub_view' => 'add-new',
            'modules_list' => $modules_list
        ]);
        
        $this->setTemplate('dashboard/modules/modules');
        $this->display(); 
    }

    public function initModule( $module_name, $controller = 'configure' ) {
        $app = $this->app;
        $user = $app->container->user;
        $params = $app->request->get();
        $smarty = $app->container->smarty;

        $module = Module::getInstanceByName($module_name);
        if(!Validate::isLoadedObject($module)) {
            $this->setTemplate('404');
            $this->display();
        }

        $site_id = ArrayUtils::get($params, 'site_id');

        if($site_id) {
            $site = new Site( (int) $site_id );
            if(Validate::isLoadedObject($site)) {
                $app->container->site = $site;
            }
        }

        $method = ArrayUtils::get($this->map, $controller, $controller);

        $smarty->assign([
            'module_name' => $module->name,
            'module_icon' => $module->icon,
            'module_display_name' => $module->displayName
        ]);

        if(!$module->checkAccess()) {
            $this->setTemplate('404');
            $this->display();
        }

        $this->setVars([
            'page' => [
                'title' => $module->displayName
            ],
        ]);

        switch( $controller ) {
            case "configure":
                if( Module::isInstalled($module->name)) {
                    // We check if method of module exists
                    if (!method_exists($module, $method)) {
                        return $this->showError("Module Not Found", "Method of module cannot be found");
                    }

                    $output = $module->{$method}();

                    $smarty->assign('module_content', $output);
                    $this->setTemplate('dashboard/modules/module');
                    return $this->display();
                }
                break;

            default:
                if (!method_exists($module, $method)) {
                    return $this->showError("Module Not Found", "Method of module cannot be found");
                }
                
                $module->{$method}();
                break;
        }
    }

    public function purchaseModule( $module_name ) {
        $app = $this->app;
        $user = $app->container->user;
        $smarty = $app->container->smarty;

        if(!$user->is_admin) {
            $this->setTemplate('404');
            $this->display();
        }

        $this->setVars([
            'page' => [
                'title' => "Modules › Purchase"
            ],
        ]);

        $this->setTemplate('dashboard/modules/purchase');
        $this->display();
    }
}