<?php


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

use ModulesGarden\ProxmoxAddon\App\Events\Vps\LxcUpdateEvent;
use ModulesGarden\ProxmoxAddon\App\Events\Vps\QemuUpdateEvent;
use ModulesGarden\ProxmoxAddon\App\Jobs\Vps\BaseJob;
use ModulesGarden\ProxmoxAddon\App\Services\Vps\HighAvailabilityClusterService;
use ModulesGarden\ProxmoxAddon\App\Services\Vps\IpSetIpFilterService;
use ModulesGarden\ProxmoxAddon\App\Services\Vps\ProductService;
use function ModulesGarden\ProxmoxAddon\Core\Helper\fire;

class UpgradeVmJob extends BaseJob
{
    use ProductService;
    /**
     * @var  HighAvailabilityClusterService
     */
    protected $highAvailabilityClusterService;
    protected $ipSetIpFilterService;

    public function handle($text = null)
    {
        $this->initParams();
        $this->initServices();
        $this->highAvailabilityClusterService = new HighAvailabilityClusterService();
        $this->ipSetIpFilterService           = new IpSetIpFilterService();
        //Upgrade
        if ($this->configuration()->isQemu())
        {
            $evnet = new QemuUpdateEvent($this->vm());
            $evnet->isChangeVmPassword(false);
            fire($evnet);
        }
        elseif ($this->configuration()->isLxc())
        {
            fire(new LxcUpdateEvent($this->vm()));
        }
        //createHaResource
        if ($this->highAvailabilityClusterService->isConfigured()  )
        {
            $this->highAvailabilityClusterService->create();
        }
        //ip set filter
        if ($this->configuration()->isIpsetIpFilter())
        {
            $this->ipSetIpFilterService->create();
        }
        //start vm
        if (!$this->vm()->isRunning())
        {
            $this->vm()->start();
        }

    }

}