<?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\ProxmoxVps\App\UI\IpAddress\Providers;

use MGProvision\Proxmox\v2\Api;
use ModulesGarden\ProxmoxAddon\App\Models\IpAddress;
use ModulesGarden\ProxmoxAddon\App\Models\VmIpAddress;
use ModulesGarden\ProxmoxAddon\App\Services\ApiService;
use ModulesGarden\ProxmoxAddon\App\Services\Utility;
use ModulesGarden\ProxmoxAddon\App\Services\Vps\HostingService;
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\VmIpAddressConveter;
use ModulesGarden\Servers\ProxmoxVps\Core\UI\Interfaces\ClientArea;
use ModulesGarden\Servers\ProxmoxVps\Core\UI\ResponseTemplates\HtmlDataJsonResponse;
use ModulesGarden\Servers\ProxmoxVps\Core\UI\Widget\Forms\DataProviders\BaseDataProvider;
use Illuminate\Database\Capsule\Manager as DB;


class IpAddressProvider extends BaseDataProvider implements ClientArea
{
    use ApiService;
    use ProductService;
    use HostingService;

    private $networkService;
    /**
     * @var IpSetIpFilterService
     */
    private $ipSetIpFilterService;

    /**
     * IpAddressProvider constructor.
     */
    public function __construct()
    {
        parent::__construct();
        $this->networkService       = new NetworkService();
        $this->ipSetIpFilterService = new IpSetIpFilterService();
    }

    public function read()
    {
        if ($this->actionElementId && $this->actionElementId != "diskDataTable")
        {
            $this->data['id'] = $this->actionElementId;
            $this->data['ip'] = VmIpAddress::where("id", $this->actionElementId)->value("ip");
        }
    }

    public function create()
    {
        try{
            DB::beginTransaction();
            Api::beginTransaction();
            $ip = new VmIpAddress();
            $ip->fill($this->formData);
            $ip->hosting_id = $this->getWhmcsParamByKey("serviceid");
            $ip->server_id  = $this->getWhmcsParamByKey("serverid");
            $this->setHostingId($this->getWhmcsParamByKey("serviceid"));
            if($this->getFormDataValues()['network']=='on'){
                $ipConveter = new VmIpAddressConveter([$ip], $this->vm());
                $config = $ipConveter->asConfig();
                $this->vm()->updateConfig($config);
            }
            //insert to data base
            $ip->save();
            $this->hosting()->ipAdd($ip->ip);
            //lock ip in proxmox addon
            if (!Utility::isIpManagerProxmoxVPSIntegration())
            {
                IpAddress::where('ip', $ip->ip)
                    ->update(["hosting_id" => $ip->hosting_id]);
            }
            DB::commit();
        }catch (\Exception $ex){
            Api::commit();
            DB::rollBack();
            throw $ex;
        }
        return (new HtmlDataJsonResponse())
            ->setStatusSuccess()
            ->setMessageAndTranslate('The IP Address has been added successfully');
    }

    public function update()
    {
    }

    public function delete()
    {
        $this->networkService->deleteByIpAddresses([$this->formData['id']]);
        if ($this->configuration()->isIpsetIpFilter())
        {
            $this->ipSetIpFilterService->create();
        }
        return (new HtmlDataJsonResponse())
            ->setStatusSuccess()
            ->setMessageAndTranslate('The IP Address has been deleted successfully');
    }


}