<?php

use Illuminate\Database\Capsule\Manager as DB;
use ModulesGarden\Servers\ProxmoxVps\Core\App\AppContext;

if (!defined("WHMCS"))
{
    die("This file cannot be accessed directly");
}

if (!defined('DS'))
{
    define('DS', DIRECTORY_SEPARATOR);
}

require_once __DIR__ . DIRECTORY_SEPARATOR . 'core' . DIRECTORY_SEPARATOR . 'WhmcsErrorIntegration.php';





function proxmoxVPS_CreateAccount(array $params)
{
    require_once __DIR__ . DIRECTORY_SEPARATOR . 'core' . DIRECTORY_SEPARATOR . 'App' . DIRECTORY_SEPARATOR . 'AppContext.php';
    
    
    
    $appContext = new AppContext();
    return $appContext->runApp(__FUNCTION__, $params);
}

function proxmoxVPS_SuspendAccount(array $params)
{
    require_once __DIR__ . DIRECTORY_SEPARATOR . 'core' . DIRECTORY_SEPARATOR . 'App' . DIRECTORY_SEPARATOR . 'AppContext.php';
    
    
    
    $appContext = new AppContext();    
    
    return $appContext->runApp(__FUNCTION__, $params);
}

function proxmoxVPS_UnsuspendAccount(array $params)
{
    require_once __DIR__ . DIRECTORY_SEPARATOR . 'core' . DIRECTORY_SEPARATOR . 'App' . DIRECTORY_SEPARATOR . 'AppContext.php';
    
    
    
    $appContext = new AppContext();    
    
    return $appContext->runApp(__FUNCTION__, $params);
}

function proxmoxVPS_TerminateAccount(array $params)
{
    require_once __DIR__ . DIRECTORY_SEPARATOR . 'core' . DIRECTORY_SEPARATOR . 'App' . DIRECTORY_SEPARATOR . 'AppContext.php';
    
    
    
    $appContext = new AppContext();    
    
    return $appContext->runApp(__FUNCTION__, $params);
}

if($_REQUEST['id'] || $_REQUEST['serviceid']){
    try{
        $serviceId = $_REQUEST['id'] ? $_REQUEST['id'] : $_REQUEST['serviceid'];
        $statement = DB::connection()
            ->getPdo()
            ->prepare("SELECT p.`value`
                                                              FROM `ProxmoxAddon_ProductConfiguration` p
                                                              LEFT JOIN tblhosting h ON ( h.packageid = p.product_id )
                                                              WHERE h.id =:id
                                                              AND p.`setting` = :setting
                                                              AND p.`value`  LIKE :value");
        $statement->execute(["id" => $serviceId, "setting" => "cloudInit", "value" => '"on"' ]);
        $proxmoxVpsChangePassword =  $statement->rowCount() == 1;
        if(!$proxmoxVpsChangePassword){
            $statement = DB::connection()
                ->getPdo()
                ->prepare("SELECT p.`value`
                                                              FROM `ProxmoxAddon_ProductConfiguration` p
                                                              LEFT JOIN tblhosting h ON ( h.packageid = p.product_id )
                                                              WHERE h.id =:id
                                                              AND p.`setting` = :setting
                                                              AND p.`value`  LIKE :value");
            $statement->execute(["id" => $serviceId, "setting" => "agentServicePassword", "value" => '"on"' ]);
            $proxmoxVpsChangePassword =  $statement->rowCount() == 1;
        }
    } catch (\Exception $ex) {
    }
}

if($proxmoxVpsChangePassword){
    function proxmoxVPS_ChangePassword(array $params)
    {
        require_once __DIR__ . DIRECTORY_SEPARATOR . 'core' . DIRECTORY_SEPARATOR . 'App' . DIRECTORY_SEPARATOR . 'AppContext.php';
        $appContext = new AppContext();
        return $appContext->runApp(__FUNCTION__, $params);
    }
}

function proxmoxVPS_ChangePackage(array $params)
{
    require_once __DIR__ . DIRECTORY_SEPARATOR . 'core' . DIRECTORY_SEPARATOR . 'App' . DIRECTORY_SEPARATOR . 'AppContext.php';
    
    
    
    $appContext = new AppContext();    
    
    return $appContext->runApp(__FUNCTION__, $params);
}

function proxmoxVPS_TestConnection(array $params)
{
    require_once __DIR__ . DIRECTORY_SEPARATOR . 'core' . DIRECTORY_SEPARATOR . 'App' . DIRECTORY_SEPARATOR . 'AppContext.php';
    
    $appContext = new AppContext();
    return $appContext->runApp(__FUNCTION__, $params);
}

function proxmoxVPS_UsageUpdate(array $params)
{

}

function proxmoxVPS_ConfigOptions(array $params)
{
    require_once __DIR__ . DIRECTORY_SEPARATOR . 'core' . DIRECTORY_SEPARATOR . 'App' . DIRECTORY_SEPARATOR . 'AppContext.php';
    
    $appContext = new AppContext();    
    
    return $appContext->runApp(__FUNCTION__, $params);
}

function proxmoxVPS_MetaData()
{
    require_once __DIR__ . DIRECTORY_SEPARATOR . 'core' . DIRECTORY_SEPARATOR . 'App' . DIRECTORY_SEPARATOR . 'AppContext.php';
    
    $appContext = new AppContext();    
    
   return $appContext->runApp(__FUNCTION__, []);
}

/**
 * FUNCTION proxmoxVPS_AdminCustomButtonArray
 * This function can be used to define custom functions.
 * @param type $params
 * @return string
 */
function proxmoxVPS_AdminCustomButtonArray($params) {
    $buttons = array(
        "Change User Role" => "ChangeUserRole",
        "Reset Bandwidth" => "ResetBandwidth",
    );
    return $buttons;
}

function proxmoxVPS_ChangeUserRole($params)
{
    require_once __DIR__ . DIRECTORY_SEPARATOR . 'core' . DIRECTORY_SEPARATOR . 'App' . DIRECTORY_SEPARATOR . 'AppContext.php';
    $appContext = new AppContext();
    return $appContext->runApp(__FUNCTION__, $params);
}

function proxmoxVPS_ResetBandwidth($params)
{
    require_once __DIR__ . DIRECTORY_SEPARATOR . 'core' . DIRECTORY_SEPARATOR . 'App' . DIRECTORY_SEPARATOR . 'AppContext.php';
    $appContext = new AppContext();
    return $appContext->runApp(__FUNCTION__, $params);
}

function proxmoxVPS_AdminServicesTabFields($params)
{
    require_once __DIR__ . DIRECTORY_SEPARATOR . 'core' . DIRECTORY_SEPARATOR . 'App' . DIRECTORY_SEPARATOR . 'AppContext.php';
    $appContext = new AppContext();
    return $appContext->runApp(__FUNCTION__, $params);
}



function proxmoxVPS_ClientArea($params)
{
    if($params['status']!='Active'){
        return;
    }
    require_once __DIR__ . DIRECTORY_SEPARATOR . 'core' . DIRECTORY_SEPARATOR . 'App' . DIRECTORY_SEPARATOR . 'AppContext.php';

    

    $appContext = new AppContext();

    return $appContext->runApp('clientarea', $params);
}

