<?php


namespace ModulesGarden\Servers\ProxmoxCloudVps\App\UI\Snapshot\Pages;


use MGProvision\Proxmox\v2\models\Kvm;
use MGProvision\Proxmox\v2\repository\SnapshotRepository;
use ModulesGarden\ProxmoxAddon\App\Services\ApiService;
use ModulesGarden\ProxmoxAddon\App\Services\Cloud\ProductService;
use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\Snapshot\Buttons\CreateButton;
use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\Snapshot\Buttons\DeleteButton;
use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\Snapshot\Buttons\DeleteMassButton;
use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\Snapshot\Buttons\RollbackButton;
use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\Snapshot\Buttons\UpdateButton;
use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Widget\DataTable\Column;
use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Widget\DataTable\DataProviders\Providers\ArrayDataProvider;
use function ModulesGarden\Servers\ProxmoxCloudVps\Core\Helper\sl;

trait SnapshotTrait
{
    use ApiService;
    use ProductService;

    /**
     * @var SnapshotRepository
     */
    private $snapshotRepository;

    public function initContent()
    {
        //Create
        $createButton = new CreateButton();
        $createButton->addClass("pmCreateSnapshotButton");
        if (!$this->resourceGuard()->hasSnapshotLimit())
        {
            $createButton->addClass("hidden");
        }
        $this->unsetShowTitle();

        $this->addButton($createButton);
        //rollback
        $this->addActionButton(new RollbackButton());
        //Update
        $this->addActionButton(new UpdateButton());
        //Delete
        $this->addActionButton(new DeleteButton());
        //Mass delete
        $this->addMassActionButton(new DeleteMassButton());
        $this->snapshotRepository = new SnapshotRepository();
    }

    protected function loadHtml()
    {
        $vm = \ModulesGarden\ProxmoxAddon\Core\Helper\sl('Vm')->getVm();
        $this->addColumn((new Column('name'))->setSearchable(true, "string")->setOrderable())
            ->addColumn((new Column('status'))->setSearchable(true, "string")->setOrderable());
        if ($vm instanceof Kvm)
        {
            $this->addColumn((new Column('vmstate'))->setSearchable(true)->setOrderable());
        }
        $this->addColumn((new Column('snaptime'))->setSearchable(true, "date")->setOrderable('DESC'))
            ->addColumn((new Column('description'))->setSearchable(true)->setOrderable());
    }


    public function replaceFieldStatus($key, $row)
    {
        //current
        if ($this->snapshotRepository->getCurrent()->getParent() == $row['name'])
        {
            return '<span class="lu-label lu-label--success lu-label--status">' . sl('lang')->tr("Active") . '</span>';
        }
        return '<span class="lu-label lu-label--danger lu-label--status">' . sl('lang')->tr("Disabled") . '</span>';


    }

    public function replaceFieldVmstate($key, $row)
    {
        if ($row['vmstate'] == "1")
        {
            return '<span class="lu-label lu-label--success lu-label--status">' . sl('lang')->tr("Yes") . '</span>';
        }
        return '<span class="lu-label lu-label--danger lu-label--status">' . sl('lang')->tr("No") . '</span>';
    }

    public function replaceFieldSnaptime($key, $row)
    {
        return date('Y-m-d H:i:s', $row[$key]);
    }

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

    protected function loadData()
    {
        $vm = \ModulesGarden\ProxmoxAddon\Core\Helper\sl('Vm')->getVm();
        $this->snapshotRepository->setApi($this->api());
        $this->snapshotRepository->findByVm($vm);
        $this->snapshotRepository->ignoreCurrent(true);
        $data = [];
        foreach ($this->snapshotRepository->fetch() as $entity)
        {
            $row    = $entity->getAttributes();
            $data[] = array_merge(['id' => base64_encode(json_encode($row))], $row);
        }
        $dataProv = new ArrayDataProvider();
        $dataProv->setDefaultSorting("snaptime", 'DESC');
        $dataProv->setData($data);
        $this->setDataProvider($dataProv);
    }

}