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

use MGProvision\Proxmox\v2\models\BackupSchedule;
use ModulesGarden\ProxmoxAddon\App\Services\ApiService;
use ModulesGarden\ProxmoxAddon\App\Services\Cloud\ProductService;
use ModulesGarden\ProxmoxAddon\App\Enum\Cloud\ConfigurableOption;
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 BackupJobProvider extends BaseDataProvider implements ClientArea
{
    use ApiService;
    use ProductService;

    public function read()
    {
        if ($this->actionElementId)
        {
            $this->data = json_decode(base64_decode($this->actionElementId), true);
            //down
            $dow               = $this->data['dow'];
            $this->data['dow'] = explode(",", $dow);
            //mailto
            $mailto               = $this->data['mailto'];
            $this->data['mailto'] = $mailto ? "on" : "off";
        }
    }

    public function create()
    {
        $this->acl()->backupJob();
        $this->resourceGuard()->backupJobLimit();
        $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;
        }
        $backupSchedule = new BackupSchedule();
        $backupSchedule->setApi($this->api());
        $vmid = \ModulesGarden\ProxmoxAddon\Core\Helper\sl('Vm')->getVmModel()->vmid;
        $backupSchedule->setAttributes([
            "vmid"      => $vmid,
            "storage"   => $storage,
            "remove"    => $routing,
            "maxfiles"  => $maxFiles,
            "starttime" => $this->formData['starttime'],
            "dow"       => implode(",", $this->formData['dow']),
            "mode"      => $this->formData['mode'],
            "compress"  => $this->formData['compress'],
            "mailto"    => $this->formData['mailto'] == "on" ? $this->getWhmcsParamByKey('clientsdetails')['email'] : null,
        ]);
        $backupSchedule->create();
        return (new HtmlDataJsonResponse())
            ->setStatusSuccess()
            ->setMessageAndTranslate('The backup job has been created successfully')
            ->setRefreshTargetIds(['backupJobDataTable'])
            ->addData('createButtonStatus', $this->resourceGuard()->hasBackupJobLimit())
            ->setCallBackFunction('pmToggleBackupButton');
    }

    public function update()
    {
        $this->acl()->backupJob();
        $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;
        }
        $backupSchedule = new BackupSchedule();
        $backupSchedule->setApi($this->api());
        $vmid = \ModulesGarden\ProxmoxAddon\Core\Helper\sl('Vm')->getVmModel()->vmid;
        $backupSchedule->setAttributes([
            "id"        => $this->formData['backupScheduleId'],
            "vmid"      => $vmid,
            "storage"   => $storage,
            "remove"    => $routing,
            "maxfiles"  => $maxFiles,
            "starttime" => $this->formData['starttime'],
            "dow"       => implode(",", $this->formData['dow']),
            "mode"      => $this->formData['mode'],
            "compress"  => $this->formData['compress'],
            "mailto"    => $this->formData['mailto'] == "on" ? $this->getWhmcsParamByKey('clientsdetails')['email'] : null,
        ]);
        $backupSchedule->update();
        return (new HtmlDataJsonResponse())
            ->setStatusSuccess()
            ->setMessageAndTranslate('The backup job has been updated successfully');
    }

    public function delete()
    {
        $this->acl()->backupJob();
        $backupSchedule = new BackupSchedule();
        $backupSchedule->setApi($this->api());
        $backupSchedule->setId($this->formData['backupScheduleId']);
        $backupSchedule->delete();
        return (new HtmlDataJsonResponse())
            ->setStatusSuccess()
            ->setMessageAndTranslate('The backup job has been deleted successfully')
            ->setRefreshTargetIds(['backupJobDataTable'])
            ->addData('createButtonStatus', 1)
            ->setCallBackFunction('pmToggleBackupButton');
    }


}