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

use MGProvision\Proxmox\v2\models\Kvm;
use MGProvision\Proxmox\v2\models\Lxc;
use ModulesGarden\ProxmoxAddon\App\Services\ApiService;
use ModulesGarden\ProxmoxAddon\App\Services\Cloud\HostingService;
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;
use function ModulesGarden\Servers\ProxmoxCloudVps\Core\Helper\isAdmin;
use function ModulesGarden\Servers\ProxmoxCloudVps\Core\Helper\sl;


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

    public function read()
    {
        $vm = \ModulesGarden\ProxmoxAddon\Core\Helper\sl('Vm')->getVm();
        if ($vm instanceof Kvm)
        {
            //hostname
            $this->data['hostname'] = $vm->config()['name'];
            //ISO
            if(isAdmin() || $this->configuration()->isPermissionIsoImage()){
                $this->data['iso']            = $vm->cdrom()['isoRaw'];
                $this->availableValues['iso'] = ["none" => sl("lang")->abtr("template", "None")];

                foreach ($this->isoRepository()->fetch() as $file)
                {
                    if ($this->configuration()->isPermissionIsoImages() && !in_array($file->getVolid(), $this->configuration()->getPermissionIsoImages()))
                    {
                        continue;
                    }
                    $this->availableValues['iso'][$file->getVolid()] = sl("lang")->abtr("template", $file->getFriendlyName());
                }
            }
            //Boot order
            $bootOrder                           = $vm->getBootOrder();
            $this->data['bootOrder0']            = $bootOrder[0];
            $this->data['bootOrder1']            = $bootOrder[1];
            $this->data['bootOrder2']            = $bootOrder[2];
            $options                             = [
                0   => sl("lang")->tr("None"),
                "c" => sl("lang")->tr("Disk"),
                "d" => sl("lang")->tr("CD-ROM"),
                "n" => sl("lang")->tr("Network"),
            ];
            $this->availableValues['bootOrder0'] = $options;
            $this->availableValues['bootOrder1'] = $options;
            $this->availableValues['bootOrder2'] = $options;
            //sshkeys
            $this->data['sshkeys'] = rawurldecode($vm->config()['sshkeys']);
        }
        else
        {
            if ($vm instanceof Lxc)
            {
                //hostname
                $this->data['hostname'] = $vm->config()['hostname'];
            }
        }

    }

    public function update()
    {
        $vm = \ModulesGarden\ProxmoxAddon\Core\Helper\sl('Vm')->getVm();
        $vmModel = \ModulesGarden\ProxmoxAddon\Core\Helper\sl('Vm')->getVmModel();
        if ($vm instanceof Kvm)
        {
            //Hostname
            $vmModel->name = $this->formData['hostname'];
            $vm->updateConfig(["name" => $this->formData['hostname']]);
            $vmModel->save();
            //iso
            if ($vm->cdrom())
            {
                $vm->updateCdrom($this->formData['iso']);
            }
            //Boot order
            $bootOrder = null;
            for ($i = 0; $i <= 2; $i++)
            {
                if ($this->formData['bootOrder' . $i])
                {
                    $bootOrder .= $this->formData['bootOrder' . $i];
                }
            }
            $vm->changeBootOrder($bootOrder);
            //sshkeys
            if($this->configuration()->isPermissionSshkeys()){
                if ($this->formData['sshkeys'])
                {
                    $vm->updateConfig(["sshkeys" => rawurlencode($this->formData['sshkeys'])]);
                } else if($vm->config()['sshkeys'])
                {
                    $vm->deleteConfig('sshkeys');
                }
            }
        }
        else
        {
            if ($vm instanceof Lxc)
            {
                //Hostname
                $vmModel->name = $this->formData['hostname'];
                $vm->updateConfig(["hostname" => $this->formData['hostname']]);
                $vmModel->save();
            }
        }

        return (new HtmlDataJsonResponse())
            ->setStatusSuccess()
            ->setMessageAndTranslate('Service information has been changed')
            ->addRefreshTargetId('serviceInformationDataTable');

    }

}