<?php

namespace ModulesGarden\ProxmoxAddon\App\Listeners\Vps;

use ModulesGarden\ProxmoxAddon\App\Events\Vps\VmCreatedEvent;
use ModulesGarden\ProxmoxAddon\App\Services\ApiService;
use ModulesGarden\ProxmoxAddon\App\Services\EmailService;
use ModulesGarden\ProxmoxAddon\App\Services\Vps\FirewallOptionService;
use ModulesGarden\ProxmoxAddon\App\Services\Vps\HighAvailabilityClusterService;
use ModulesGarden\ProxmoxAddon\App\Services\Vps\IpSetIpFilterService;
use ModulesGarden\ProxmoxAddon\App\Services\Vps\NetworkService;
use ModulesGarden\ProxmoxAddon\App\Services\Vps\ProductService;
use ModulesGarden\ProxmoxAddon\App\Services\Vps\UserService;
use ModulesGarden\ProxmoxAddon\Core\UI\Traits\WhmcsParams;


class VmCreatedListener
{
    use WhmcsParams;
    use ApiService;
    use UserService;
    use ProductService;

    /**
     * @var IpSetIpFilterService
     */
    private $ipSetFilterService;
    private $emailService;
    /**
     * @var NetworkService
     */
    private $networkService;

    /**
     * @var FirewallOptionService
     */
    protected $firewallOptionService;
    /**
     * VmCreatedListener constructor.
     */
    public function __construct()
    {
        $this->ipSetFilterService = new IpSetIpFilterService();
        $this->emailService       = new EmailService();
        $this->highAvailabilityClusterService = new HighAvailabilityClusterService();
        $this->networkService = new NetworkService();
        $this->firewallOptionService = new FirewallOptionService();
    }

    public function handle(VmCreatedEvent $event)
    {
        //update user permission
        $this->userUpdate();
        //Private Network
        if($this->configuration()->isPrivateNetwork()){
            //Get Private IP Addresses
            $ip = $this->networkService->getPrivateIpAddress();
            //Create network device
            $this->networkService->addPrivate([$ip]);
        }
        //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 (!$this->vm()->isRunning() && $this->configuration()->isStart())
        {
            $this->vm()->start();
        }
        //Welcome email notification
        if ($this->configuration()->getWelcomeEmailTemplateId())
        {
            $user = $this->getUser()->toArray();
            $user['password'] = decrypt($user['password']);
            $this->emailService->template($this->configuration()->getWelcomeEmailTemplateId())
                ->vars(["id" => $this->getWhmcsParamByKey("serviceid"),
                         'customvars' => base64_encode(serialize(['user' => $user ]))
                       ])
                ->send();
        }

    }
}