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

use MGProvision\Proxmox\v2\models\File;
use ModulesGarden\ProxmoxAddon\App\Jobs\Vps\RestoreVm;
use ModulesGarden\ProxmoxAddon\App\Services\ApiService;
use ModulesGarden\ProxmoxAddon\App\Services\Vps\ProductService;
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 function ModulesGarden\ProxmoxAddon\Core\Helper\queue;
use ModulesGarden\Servers\ProxmoxVps\App\Enum\ConfigurableOption;

class BackupProvider 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()->backup();
        $this->resourceGuard()->backupLimit();
        $storage  = $this->configuration()->getBackupStorage();
        $routing  = $this->configuration()->isBackupRouting() ? 1 : 0;
        $maxFiles       =  $this->configuration()->getBackupMaxFiles();
        if($this->isWhmcsConfigOption(ConfigurableOption::BACKUPS_FILES) &&  $this->getWhmcsConfigOption(ConfigurableOption::BACKUPS_FILES)!="-1"){
            $maxFiles       = $this->getWhmcsConfigOption(ConfigurableOption::BACKUPS_FILES);
        }else if($this->getWhmcsConfigOption(ConfigurableOption::BACKUPS_FILES)=="-1"){
            $maxFiles       = null;
        }
        $this->vm()->backup($storage, $routing, $maxFiles, $this->formData['compress'], $this->formData['mode']);
        return (new HtmlDataJsonResponse())
            ->setStatusSuccess()
            ->setMessageAndTranslate('The backup creation is in progress')
            ->addData('createButtonStatus', $this->configuration()->isPermissionBackup() && $this->resourceGuard()->hasBackupLimit())
            ->setCallBackFunction('pmToggleButton');
    }

    public function restore()
    {
        queue(RestoreVm::class, ['volid' => $this->formData['volid']], null, 'hosting', $this->getWhmcsParamByKey('serviceid'));
        return (new HtmlDataJsonResponse())
            ->setStatusSuccess()
            ->setMessageAndTranslate('The virtual machine restore is in progress');
    }

    public function update()
    {

    }

    public function delete()
    {
        $this->acl()->backup();
        $storage    = $this->configuration()->getBackupStorage();
        $volid      = $this->formData['volid'];
        $backupFile = new File();
        $backupFile->setApi($this->api());
        $backupFile->setPath("/nodes/{$this->vm()->getNode()}/storage/{$storage}/content/{$volid}");
        $backupFile->delete();

        return (new HtmlDataJsonResponse())
            ->setStatusSuccess()
            ->setMessageAndTranslate('The backup has been deleted successfully')
            ->addData('createButtonStatus', $this->configuration()->isPermissionBackup() && $this->resourceGuard()->hasBackupLimit())
            ->setCallBackFunction('pmToggleButton');
    }

    public function deleteMass()
    {
        $this->acl()->backup();
        $storage    = $this->configuration()->getBackupStorage();
        $backupFile = new File();
        $backupFile->setApi($this->api());
        foreach ($this->request->get('massActions') as $id)
        {
            $data  = json_decode(base64_decode($id), true);
            $volid = $data['volid'];
            $backupFile->setPath("/nodes/{$this->vm()->getNode()}/storage/{$storage}/content/{$volid}");
            $backupFile->delete();
        }
        return (new HtmlDataJsonResponse())
            ->setStatusSuccess()
            ->setMessageAndTranslate('The backups have been deleted successfully')
            ->addData('createButtonStatus', $this->configuration()->isPermissionBackup() && $this->resourceGuard()->hasBackupLimit())
            ->setCallBackFunction('pmToggleButton');
    }

}