<?php


namespace ModulesGarden\ProxmoxAddon\App\Jobs\Vps\Agent;


use MGProvision\Proxmox\v2\ProxmoxApiException;
use ModulesGarden\ProxmoxAddon\App\Jobs\Vps\BaseJob;
use ModulesGarden\ProxmoxAddon\App\Services\Vps\ProductService;

class ConfigureNetworkJob extends BaseJob
{
    use ProductService;

    public function handle($text = null)
    {
        $this->initParams();
        $this->initServices();
        $this->setHostingId($this->getWhmcsParamByKey("serviceid"));
        try{
            if(!$this->vm()->isRunning()){
                $this->log->info(sprintf("VM %s - Start", $this->vm()->getVmid()));
                $this->vm()->start();
                $this->sleep(40);
                return false;
            }
            $this->vm()->agent()->ping();
            $this->agentService->configureNetwork();

        }catch (ProxmoxApiException $ex){
            if(preg_match("/not running/", $ex->getMessage())){
                $this->log->info($ex->getMessage());
            }else{
                $this->log->error($ex->getMessage());
            }
            //sleep
            $this->sleep(30);
            return false;
        }
    }

}