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

use MGProvision\Proxmox\v2\models\Snapshot;
use ModulesGarden\ProxmoxAddon\App\Services\ApiService;
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;


class SnapshotProvider extends BaseDataProvider implements ClientArea
{
    use ProductService;
    use ApiService;

    public function read()
    {
        if ($this->actionElementId)
        {
            $this->data = json_decode(base64_decode($this->actionElementId), true);
        }
    }

    public function create()
    {

        $this->acl()->snapshot();
        $this->resourceGuard()->snapshotLimit();
        $vm = \ModulesGarden\ProxmoxAddon\Core\Helper\sl('Vm')->getVm();
        $snapshot = new Snapshot();
        $snapshot->setApi($this->api());
        $snapshot->setPath($vm->getPath() . "/snapshot");
        $snapshot->setAttributes([
            "name"        => $this->formData['name'],
            "description" => $this->formData['description'],
        ]);
        if ($this->formData['vmstate'])
        {
            $snapshot->setVmstate($this->formData['vmstate'] == "on" ? 1 : 0);
        }
        $taskId =  $snapshot->create();
        sleep(1);
        $task = $vm->node()->task($taskId);
         if($task->isFalied()){
             return (new HtmlDataJsonResponse())
                 ->setStatusError()
                 ->setMessageAndTranslate($task->getExitstatus());
         }
        return (new HtmlDataJsonResponse())
            ->setStatusSuccess()
            ->setMessageAndTranslate('The snapshot has been created successfully')
            ->addData('createButtonStatus', $this->resourceGuard()->hasSnapshotLimit())
            ->setCallBackFunction('pmToggleSnapshotButton');
    }

    public function update()
    {
        $this->acl()->snapshot();
        $vm = \ModulesGarden\ProxmoxAddon\Core\Helper\sl('Vm')->getVm();
        $snapshot = new Snapshot();
        $snapshot->setApi($this->api());
        $snapshot->setPath($vm->getPath() . "/snapshot/" . $this->formData['name']);
        $snapshot->setAttributes([
            "name"        => $this->formData['name'],
            "description" => $this->formData['description'],
        ]);
        $snapshot->update();
        return (new HtmlDataJsonResponse())
            ->setStatusSuccess()
            ->setMessageAndTranslate('The snapshot has been updated successfully');
    }

    public function delete()
    {
        $this->acl()->snapshot();
        $vm = \ModulesGarden\ProxmoxAddon\Core\Helper\sl('Vm')->getVm();
        $snapshot = new Snapshot();
        $snapshot->setApi($this->api());
        $snapshot->setPath($vm->getPath() . "/snapshot/" . $this->formData['name']);
        $snapshot->delete();
        sleep(1);
        return (new HtmlDataJsonResponse())
            ->setStatusSuccess()
            ->setMessageAndTranslate('The snapshot has been deleted successfully')
            ->addData('createButtonStatus', $this->resourceGuard()->hasSnapshotLimit())
            ->setCallBackFunction('pmToggleSnapshotButton');
    }

    public function deleteMass()
    {
        $this->acl()->snapshot();
        $vm = \ModulesGarden\ProxmoxAddon\Core\Helper\sl('Vm')->getVm();
        $snapshot = new Snapshot();
        $snapshot->setApi($this->api());
        foreach ($this->request->get('massActions') as $id)
        {
            $data = json_decode(base64_decode($id), true);
            $name = $data['name'];
            $snapshot->setPath($vm->getPath() . "/snapshot/" . $name);
            $snapshot->delete();
            sleep(1);
        }
        return (new HtmlDataJsonResponse())
            ->setStatusSuccess()
            ->setMessageAndTranslate('The snapshots have been deleted successfully')
            ->addData('createButtonStatus', $this->resourceGuard()->hasSnapshotLimit())
            ->setCallBackFunction('pmToggleSnapshotButton');
    }

    public function rollback()
    {
        $this->acl()->snapshot();
        $vm = \ModulesGarden\ProxmoxAddon\Core\Helper\sl('Vm')->getVm();
        $snapshot = new Snapshot();
        $snapshot->setApi($this->api());
        $snapshot->setPath($vm->getPath() . "/snapshot/" . $this->formData['name']);
        $snapshot->rollback();
        return (new HtmlDataJsonResponse())
            ->setStatusSuccess()
            ->setMessageAndTranslate('The snapshot has been rolled back successfully');
    }

}