<?php

/* * ********************************************************************
 * WordPress Manager product developed. (Feb 5, 2018)
 * *
 *
 *  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\ProxmoxAddon\App\UI\Templates\Pages;

use MGProvision\Proxmox\v2 as proxmox;
use ModulesGarden\ProxmoxAddon as main;
use ModulesGarden\ProxmoxAddon\App\UI\Templates\Buttons\CreateButton;
use ModulesGarden\ProxmoxAddon\App\UI\Templates\Buttons\DeleteButton;
use ModulesGarden\ProxmoxAddon\App\UI\Templates\Buttons\DeleteMassButton;
use ModulesGarden\ProxmoxAddon\Core\FileReader\Reader\Json;
use ModulesGarden\ProxmoxAddon\Core\ModuleConstants;
use ModulesGarden\ProxmoxAddon\Core\UI\Interfaces\AdminArea;
use ModulesGarden\ProxmoxAddon\Core\UI\Widget\DataTable\Column;
use ModulesGarden\ProxmoxAddon\Core\UI\Widget\RawDataTable\RawDataTable;

/**
 * Description of PluginInstalled
 *
 * @author Pawel Kopec <pawelk@modulesgardne.com>
 */
class TemplatesDataTable extends RawDataTable implements AdminArea
{

    use main\App\Services\BaseService;
    protected $id = 'template';
    protected $name = 'template';
    protected $title = 'templateTitle';

    public function isRawTitle()
    {
        return false;
    }

    public function initContent()
    {
        //Create
        $this->addButton(new CreateButton);
        //Update
        $this->addActionButton(new main\App\UI\Templates\Buttons\UpdateButton());
        //Delete
        $this->addActionButton(new DeleteButton);
        //Delete Mass
        $this->addMassActionButton(new DeleteMassButton);
        try
        {
            $this->setServerId($this->getRequestValue('id'))->getApi();
        }
        catch (\Exception $ex)
        {
            $this->setInternalAlertMessage($ex->getMessage())
                ->setInternalAlertMessageType('danger');
        }
    }

    public function replaceFieldOstype($key, $row)
    {
        $osType = new Json('ostype.json', ModuleConstants::getFullPath('storage', 'app'));
        if (is_null($row['ostype']))
        {
            return "Unspecified OS";
        }
        else
        {
            if ($osType->get($row['ostype']))
            {
                return $osType->get($row['ostype']);
            }
        }
        return $row['ostype'];
    }

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

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

    protected function loadHtml()
    {
        $this->addColumn((new Column('node'))->setSearchable(true, Column::TYPE_STRING)->setOrderable('ASC'))
            ->addColumn((new Column('vmid'))->setSearchable(true, Column::TYPE_STRING)->setOrderable())
            ->addColumn((new Column('name'))->setSearchable(true, Column::TYPE_STRING)->setOrderable())
            ->addColumn((new Column('description'))->setSearchable(true, Column::TYPE_STRING)->setOrderable())
            ->addColumn((new Column('ostype'))->setSearchable(true, Column::TYPE_STRING)->setOrderable())
            ->addColumn((new Column('ciuser'))->setSearchable(true, Column::TYPE_STRING)->setOrderable());
    }

    protected function loadData()
    {
        $this->setServerId($this->getRequestValue('id'))->getApi()->setInstance();
        $dataProv          = new main\Core\UI\Widget\DataTable\DataProviders\Providers\ArrayDataProvider();
        $data              = [];
        $clusterRepository = new proxmox\repository\ClusterResourcesRepository();
        $clusterRepository->findKvmTemplate();
        foreach ($clusterRepository->fetch() as $resurce)
        {
            if ($resurce->getTemplate() != '1')
            {
                continue;
            }
            $vm     = array_merge($resurce->toArray(), $resurce->getVm()->config());
            $data[] = array_merge($vm, ["id" => base64_encode(json_encode($vm))]);
        }
        $dataProv->setDefaultSorting("node", 'ASC');
        $dataProv->setData((array)$data);
        $this->setDataProvider($dataProv);
    }
}
