<?php


namespace ModulesGarden\ProxmoxAddon\App\Jobs\Vps;


use MGProvision\Proxmox\v2\models\Lxc;
use ModulesGarden\ProxmoxAddon\App\Enum\Vps\ConfigurableOption;
use ModulesGarden\ProxmoxAddon\App\Events\Vps\VmCreatedEvent;
use ModulesGarden\ProxmoxAddon\App\Services\Vps\ProductService;
use ModulesGarden\ProxmoxAddon\App\Services\Vps\UserService;
use function ModulesGarden\ProxmoxAddon\Core\Helper\fire;

class MoveDiskJob extends BaseJob
{

    use ProductService;
    use UserService;

    public function handle()
    {
        $this->initParams();
        $this->initServices();
        $this->setHostingId($this->getWhmcsParamByKey("serviceid"));
        //create task validation
        if ($this->isDone())
        {
            //boot vm
            if(!$this->vm()->isRunning()){
                $this->vm()->start();
            }
            return true;
        }
        elseif ($this->isTaskRunning())
        {
            //sleep
            $this->sleep(5);
            return false;
        }
        list($storage,$diskSize) = explode(":", $this->getWhmcsConfigOption(ConfigurableOption::STORAGE),2);
        //stop vm
        if($this->vm()->isRunning()){
            $this->vm()->shutdown();
            sleep(5);
        }
        $masterHardDisk =  $this->vm()->getMasterHardDisk();
        $masterHardDisk->setStorage($storage);
        $taskId = $masterHardDisk->move(1);
        //save task id
        $this->putModelDataAndSave(["taskId" => $taskId, 'node' => $this->vm()->getNode()]);
        $this->sleep();
        return false;

    }
}