<?php

namespace ModulesGarden\ProxmoxAddon\App\Listeners\Cloud;

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

class VmCreatedListener extends VmListener
{

    public function handle(VmCreatedEvent $event)
    {
        $this->event    = $event;
        $this->api();
        $this->initVmAnVmModel();
        //update user permission
        $this->userUpdatePermission([$this->event->getVmModel()->vmid]);
        //Update firewal Options
        $this->firewallOptionService->update();
        //IP Set
        if ($this->configuration()->isIpsetIpFilter())
        {
            $this->ipSetFilterService->create();
        }
        //HA
        if ($this->highAvailabilityClusterService->isConfigured() && !$this->highAvailabilityClusterService->exist())
        {
            $this->highAvailabilityClusterService->create();
        }
        //Start the VM
        if (!sl('Vm')->getVm()->isRunning() && $this->configuration()->isStart())
        {
            sl('Vm')->getVm()->start();
        }
    }
}