<?php


namespace ModulesGarden\Servers\ProxmoxVps\App\UI\Home\Pages;


use ModulesGarden\ProxmoxAddon\App\Jobs\Vps\CloneQemuJob;
use ModulesGarden\ProxmoxAddon\App\Jobs\Vps\CreateLxcJob;
use ModulesGarden\ProxmoxAddon\App\Jobs\Vps\CreateQemuJob;
use ModulesGarden\ProxmoxAddon\App\Jobs\Vps\LoadBalancer\UpgradeVmJob;
use ModulesGarden\ProxmoxAddon\App\Jobs\Vps\MigrateVmJob;
use ModulesGarden\ProxmoxAddon\App\Jobs\Vps\Reinstall\CreateVmJob;
use ModulesGarden\ProxmoxAddon\App\Jobs\Vps\Reinstall\RestoreVm;
use ModulesGarden\ProxmoxAddon\App\Models\Job;
use ModulesGarden\Servers\ProxmoxVps\App\Helpers\UrlServiceHelper;
use ModulesGarden\Servers\ProxmoxVps\App\Http\Client\BaseClientController;
use ModulesGarden\Servers\ProxmoxVps\Core\UI\Builder\BaseContainer;

use ModulesGarden\Servers\ProxmoxVps\Core\UI\Interfaces\AdminArea;
use ModulesGarden\Servers\ProxmoxVps\Core\UI\Interfaces\AjaxElementInterface;
use ModulesGarden\Servers\ProxmoxVps\Core\UI\Interfaces\ClientArea;
use ModulesGarden\Servers\ProxmoxVps\Core\UI\ResponseTemplates\RawDataJsonResponse;
use function ModulesGarden\Servers\ProxmoxVps\Core\Helper\sl;

class VpsBuild extends BaseContainer implements ClientArea, AdminArea, AjaxElementInterface
{
    use  BaseClientController;

    protected $id = 'vpsBuild';
    protected $title = 'vpsBuild';
    protected $name = 'vpsBuild';
    protected $vueComponent = true;
    protected $defaultVueComponentName = 'mg-vpsBuild';

    public function initContent()
    {
        if ($this->isVmCreate())
        {
            $this->customTplVars['warning'] = sl("lang")->abtr('Creating VM in progress. Please try again later.');
        }
        else if ($this->isVmReinstall()) {
                $this->customTplVars['warning'] = sl("lang")->abtr('Rebuild VM in progress. Please try again later.');
        } else if ($this->isVmUpgrade()){
            $this->customTplVars['warning'] = sl("lang")->abtr('Upgrading VM in progress. Please try again later.');
        }else if(!$this->getWhmcsCustomField("vmid")){
            $this->customTplVars['warning'] = sl("lang")->abtr('Custom field VMID is empty.');
        }
    }

    private function isVmCreate()
    {
        $jobs = [
            CloneQemuJob::class,
            CreateQemuJob::class,
            CreateLxcJob::class,
        ];
        return Job::waiting()->ofHostingId($this->getWhmcsParamByKey("serviceid"))->ofJobs($jobs)->count();
    }

    private function isVmReinstall()
    {
        $jobs = [
            CreateVmJob::class
        ];
        return Job::waiting()->ofHostingId($this->getWhmcsParamByKey("serviceid"))->ofJobs($jobs)->count();
    }

    private function isVmUpgrade()
    {
        $jobs = [
            MigrateVmJob::class,
            \ModulesGarden\ProxmoxAddon\App\Jobs\Vps\RestoreVm::class,
            UpgradeVmJob::class
        ];
        return Job::waiting()->ofHostingId($this->getWhmcsParamByKey("serviceid"))->ofJobs($jobs)->count();
    }

    public function returnAjaxData()
    {
        $vars                = [];
        $response = new RawDataJsonResponse();
        if($this->isVpsCreated()){
            $response->setCallBackFunction('mgRedirect');
            $response->addData('redirectUrl' , ( new UrlServiceHelper())->home());

        }
        return  $response;
    }
}