<?php
/* * ********************************************************************
*  ProxmoxVPS Product developed. (26.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\Disk\Pages;

use ModulesGarden\ProxmoxAddon\App\Libs\Format;
use ModulesGarden\ProxmoxAddon\App\Services\ApiService;
use ModulesGarden\ProxmoxAddon\App\Services\Cloud\ProductService;
use ModulesGarden\ProxmoxAddon\App\Services\Cloud\ResourceManager;
use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\Disk\Buttons\BackupSwitchButton;
use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\Disk\Buttons\CreateButton;
use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\Disk\Buttons\DeleteButton;
use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\Disk\Buttons\DeleteMassButton;
use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\Disk\Buttons\RestoreButton;
use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\Disk\Buttons\UpdateButton;
use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Interfaces\ClientArea;
use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Widget\DataTable\Column;
use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Widget\DataTable\DataProviders\Providers\ArrayDataProvider;
use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Widget\DataTable\DataTable;
use function ModulesGarden\Servers\ProxmoxCloudVps\Core\Helper\sl;

class DiskDataTable extends DataTable implements ClientArea
{
    use ProductService;
    use ApiService;
    protected $id = 'diskDataTable';
    protected $name = 'diskDataTable';
    protected $title = 'diskDataTableTitle';

    public function initContent()
    {
        $vm = \ModulesGarden\ProxmoxAddon\Core\Helper\sl('Vm')->getVm();
        $resourceManager = new ResourceManager();
        if(!preg_match('/disk/', $vm->config()['hotplug'])){
            $this->setInternalAlertMessage("Adding, editing or removing disk will reboot the virtual machine.");
        }
        //Create
        $createButton = new CreateButton();
        $createButton->addClass("pmCreateDiskButton");
        if (!$resourceManager->disk()->hasFreeTotal())
        {
            $createButton->addClass("hidden");
        }

        $this->unsetShowTitle();

        $this->addButton($createButton);
        //Update
        $this->addActionButton(new UpdateButton());
        //Delete
        $this->addActionButton(new DeleteButton());
    }

    protected function loadHtml()
    {
        $this->addColumn((new Column('name'))->setSearchable(true, "string")->setOrderable('ASC'))
            ->addColumn((new Column('bus'))->setSearchable(true, "string")->setOrderable())
            ->addColumn((new Column('format'))->setSearchable(true)->setOrderable())
            ->addColumn((new Column('backup'))->setSearchable(true)->setOrderable())
            ->addColumn((new Column('bytes'))->setSearchable(true)->setOrderable());
    }

    public function replaceFieldName($key, $row)
    {
        $id = preg_replace('/[^0-9]/', '', $row[$key]);
        $id++;
        sl("lang")->addReplacementConstant("id",$id);
        return sl("lang")->abtr('Disk :id:');
    }

    public function replaceFieldBus($key, $row)
    {
        return sl("lang")->tr($row[$key]);
    }

    public function replaceFieldFormat($key, $row)
    {
        if ($row[$key])
        {
            return $row[$key];
        }
        return "raw";
    }

    public function replaceFieldBytes($key, $row)
    {
        return Format::convertBytes($row['bytes']);
    }

    public function replaceFieldCompress($key, $row)
    {
        return sl('lang')->tr($row['compress']);
    }

    public function customColumnHtmlBackup()
    {
        return (new BackupSwitchButton())->getHtml();
    }

    public function replaceFieldBackup($key, $row)
    {
        return $row['backup'] == "0" ? "off" : "on";
    }

    protected function loadData()
    {
        $data = [];
        $vm = \ModulesGarden\ProxmoxAddon\Core\Helper\sl('Vm')->getVm();
        foreach ( $vm->getHardDisks() as $entity)
        {
            $data[] = [
                "id"     => $entity->getId(),
                "bus"    => $entity->getId(),
                "name"   => $entity->getName(),
                "format" => $entity->getFormat(),
                "backup" => $entity->getBackup(),
                "size"   => $entity->getSize(),
                "bytes"  => $entity->getBytes(),
                "master" => $entity->isMaster(),
            ];
        }
        $dataProv = new ArrayDataProvider();
        $dataProv->setDefaultSorting("name", 'ASC');
        $dataProv->setData($data);
        $this->setDataProvider($dataProv);
    }


}