<?php
/* * ********************************************************************
*  ProxmoxVPS Product developed. (27.03.19)
* *
*
*  CREATED BY MODULESGARDEN       ->       http://modulesgarden.com
*  CONTACT                        ->       contact@modulesgarden.com
*
*
* This software is furnished under a license and may be used and copied
* only  in  accordance  with  the  terms  of such  license and with the
* inclusion of the above copyright notice.  This software  or any other
* copies thereof may not be provided or otherwise made available to any
* other person.  No title to and  ownership of the  software is  hereby
* transferred.
*
*
* ******************************************************************** */

namespace ModulesGarden\Servers\ProxmoxCloudVps\App\UI\Network\Providers;

use ModulesGarden\ProxmoxAddon\App\Services\ApiService;
use ModulesGarden\ProxmoxAddon\App\Services\Cloud\NetworkService;
use ModulesGarden\ProxmoxAddon\App\Services\Cloud\ProductService;
use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Interfaces\ClientArea;
use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\ResponseTemplates\HtmlDataJsonResponse;
use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Widget\Forms\DataProviders\BaseDataProvider;

/**
 * @deprecated
 */
class NetworkProvider extends BaseDataProvider implements ClientArea
{
    use ProductService;
    use ApiService;
    /**
     * @var NetworkService
     */
    private $networkService;

    public function read()
    {
        if ($this->actionElementId)
        {
            $this->data['id'] = $this->actionElementId;
        }
        $this->data['bridge'] = $this->configuration()->getPrivateBridge();
    }

    public function create()
    {
        $vm = \ModulesGarden\ProxmoxAddon\Core\Helper\sl('Vm')->getVm();
        if ($vm->getNetworkDevices($this->configuration()->getPrivateBridge()))
        {
            return (new HtmlDataJsonResponse())
                ->setStatusError()
                ->setMessageAndTranslate('Private network limit exceeded');
        }
        $this->networkService = new NetworkService();
        if (!$this->networkService->hasPrivateIpAddress())
        {
            throw new \Exception("Private IP Addresses are not available.");
        }
        if ($this->configuration()->isLxc())
        {
            $this->lxc();
        }
        else
        {
            if ($this->configuration()->isQemu())
            {
                $this->qemu();
            }
        }
        return (new HtmlDataJsonResponse())
            ->setStatusSuccess()
            ->setMessageAndTranslate('The Network Device has been created successfully')
            ->addData('createButtonStatus', false)
            ->setCallBackFunction('pmToggleButton');
    }

    private function lxc()
    {
        //Get Private IP Addresses
        $ip = $this->networkService->getPrivateIpAddress();
        $ip->vm_id = \ModulesGarden\ProxmoxAddon\Core\Helper\sl('Vm')->getVmModel()->id;
        //Create network device
        $this->networkService->addPrivate([$ip]);

    }

    private function qemu()
    {
        //Get Private IP Addresses
        $ip = $this->networkService->getPrivateIpAddress();
        $ip->vm_id = \ModulesGarden\ProxmoxAddon\Core\Helper\sl('Vm')->getVmModel()->id;
        //Create network device
        $this->networkService->addPrivate([$ip]);
    }

    public function update()
    {
    }

    public function delete()
    {
        $vm = \ModulesGarden\ProxmoxAddon\Core\Helper\sl('Vm')->getVm();
        if (!$vm->getNetworkDevices($this->configuration()->getPrivateBridge()))
        {
            return (new HtmlDataJsonResponse())
                ->setStatusError()
                ->setMessageAndTranslate('Private network device not found');
        }
        $this->networkService = new NetworkService();
        $this->networkService->deleteByNetworkId([$this->formData['id']]);
        return (new HtmlDataJsonResponse())
            ->setStatusSuccess()
            ->setMessageAndTranslate('The Network Device has been deleted successfully')
            ->addData('createButtonStatus', true)
            ->setCallBackFunction('pmToggleButton');
    }


}