<?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\VirtualNetwork\Providers;

use ModulesGarden\ProxmoxAddon\App\Models\VirtualInterface;
use ModulesGarden\ProxmoxAddon\App\Models\VirtualNetwork;
use ModulesGarden\ProxmoxAddon\App\Services\ApiService;
use ModulesGarden\ProxmoxAddon\App\Services\Cloud\NetworkService;
use ModulesGarden\ProxmoxAddon\App\Services\Cloud\ProductService;
use ModulesGarden\ProxmoxAddon\App\Services\Cloud\ResourceManager;
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\BaseModelDataProvider;
use function ModulesGarden\Servers\ProxmoxCloudVps\Core\Helper\sl;
use function ModulesGarden\Servers\ProxmoxCloudVps\Core\Helper\t;


class VirtualNetworkProvider extends BaseModelDataProvider implements ClientArea
{
    use ProductService;
    use ApiService;

    /**
     * @var NetworkService
     */
    private $networkService;

    /**
     * VmUpdateProvider constructor.
     */
    public function __construct()
    {
        $this->model = new VirtualNetwork;
        $this->networkService = new NetworkService();
    }

    public function read()
    {
        if(!$this->getActionElementIdValue()){
            return;
        }
        $dbData = $this->model->where('id', $this->actionElementId)
                              ->ofHostingId($this->getWhmcsParamByKey('serviceid'))
                              ->first();
        if ($dbData !== null)
        {
            $this->data = $dbData->toArray();
        }
    }

    public function create()
    {
        $resourceManager = new ResourceManager();
        $this->model->fill($this->getFormDataValues());
        $this->model->tag = $this->networkService->nextTag();
        $this->model->hosting_id = $this->getWhmcsParamByKey('serviceid');
        $this->model->save();
        return (new HtmlDataJsonResponse())
            ->setStatusSuccess()
            ->setMessageAndTranslate('The Virtual :name: network has been created successfully')
            ->addData('createButtonStatus', $resourceManager->virtualNetworks()->hasFreeTotal())
            ->setCallBackFunction('pmToggleVirtualNetworkButton');
    }



    public function update()
    {
        $resourceManager = new ResourceManager();
        $dbData = $this->model->where('id',  $this->getFormDataValues()['id'])
            ->ofHostingId($this->getWhmcsParamByKey('serviceid'))
            ->firstOrFail();
        $dbData->fill($this->formData)->save();
        t()->addReplacementConstant('name',$dbData->name);
        return (new HtmlDataJsonResponse())
            ->setStatusSuccess()
            ->setMessageAndTranslate('The Virtual :name: network has been updated successfully')
            ->addData('createButtonStatus', $resourceManager->virtualNetworks()->hasFreeTotal())
            ->setCallBackFunction('pmToggleVirtualNetworkButton');
    }

    public function delete()
    {
        if(VirtualInterface::ofHostingId($this->getWhmcsParamByKey('serviceid'))->ofVnId($this->getFormDataValues()['id'])->count()){
            return (new HtmlDataJsonResponse())
                ->setStatusError()
                ->setMessageAndTranslate('cannotDeleteTheVirtualNetwork');
        }
        $resourceManager = new ResourceManager();
        $this->model->where('id', $this->getFormDataValues()['id'])
                    ->ofHostingId($this->getWhmcsParamByKey('serviceid'))
                     ->delete();

        return (new HtmlDataJsonResponse())
            ->setStatusSuccess()
            ->setMessageAndTranslate('The Virtual :name: network has been deleted successfully')
            ->addData('createButtonStatus', $resourceManager->virtualNetworks()->hasFreeTotal())
            ->setCallBackFunction('pmToggleVirtualNetworkButton');
    }


}