<?php


namespace ModulesGarden\ProxmoxAddon\App\Listeners\Cloud;


use ModulesGarden\ProxmoxAddon\App\Events\Cloud\VmReinstalledEvent;
use function ModulesGarden\ProxmoxAddon\Core\Helper\sl;

class VmReinstalledListener extends VmListener
{

    public function handle(VmReinstalledEvent $event)
    {
        $this->event    = $event;
        $this->api();
        $this->initVmAnVmModel();
        //IP Set
        if ($this->configuration()->isIpsetIpFilter())
        {
            $this->ipSetFilterService->create();
        }
        //User permission
        $this->userUpdatePermission([$this->event->getVmModel()->vmid]);
        //HA
        if ($this->highAvailabilityClusterService->isConfigured() && !$this->highAvailabilityClusterService->exist())
        {
            $this->highAvailabilityClusterService->create();
        }
        if (!sl('Vm')->getVm()->isRunning() && $this->configuration()->isStart())
        {
            sl('Vm')->getVm()->start();
        }
    }
}