<?php

namespace ModulesGarden\Servers\ProxmoxVps\App\Http\Client;

use ModulesGarden\ProxmoxAddon\App\Models\KeyPair;
use ModulesGarden\ProxmoxAddon\App\Services\Vps\ProductService;
use ModulesGarden\Servers\ProxmoxVps\App\Helpers\AppParams;
use ModulesGarden\Servers\ProxmoxVps\App\Helpers\ProxmoxAddonValidator;
use ModulesGarden\Servers\ProxmoxVps\App\UI\Home\Pages\DetailsContainer;
use ModulesGarden\Servers\ProxmoxVps\App\UI\Home\Pages\ServiceActions;
use ModulesGarden\Servers\ProxmoxVps\App\UI\Home\Pages\ServiceManagement;
use ModulesGarden\Servers\ProxmoxVps\App\UI\IpAddress\Pages\IpAddressDataTable;
use ModulesGarden\Servers\ProxmoxVps\App\UI\ServiceInformation\Pages\ServiceInformation;
use ModulesGarden\Servers\ProxmoxVps\Core\Helper;
use ModulesGarden\Servers\ProxmoxVps\Core\Http\AbstractClientController;
use ModulesGarden\Servers\ProxmoxVps\Core\UI\Traits\WhmcsParams;
use Symfony\Component\HttpFoundation\StreamedResponse;

class Home extends AbstractClientController
{

    use WhmcsParams;
    use ProductService;
    use BaseClientController;

    public function index()
    {
        if ($this->getWhmcsParamByKey('status') != 'Active')
        {
            return;
        }
        elseif(!ProxmoxAddonValidator::isInstalled()){
            ProxmoxAddonValidator::isInstalledOrFail();
        }
        else if (!$this->isVpsCreated())
        {
            return $this->onVpsBuild();
        }
        (new AppParams())->initFromWhmcsParams();
        return Helper\view()->addElement(ServiceActions::class)
            ->addElement(ServiceManagement::class)
            ->addElement(ServiceInformation::class)
            ->addElement(IpAddressDataTable::class);
    }

    public function sshPublicKeyDownload()
    {
        $response = new StreamedResponse();
        $response->setStatusCode(200);
        $response->headers->set('Content-Type', 'application/x-pem-file; charset=utf-8');
        $response->headers->set('Content-Transfer-Encoding', 'Binary');
        $response->setCallback(function ()
        {
            /**
             * @var $keyPair KeyPair
             */
            $keyPair = KeyPair::ofHostingId($this->getWhmcsParamByKey("serviceid"))->firstOrFail();
            echo $keyPair->getPublic();
            die();
        });
        $response->headers->set(
            'Content-Disposition', 'attachment; filename="id_rsa.pub"'
        );
        $response->send();
    }

    public function sshPrivateKeyDownload()
    {
        $response = new StreamedResponse();
        $response->setStatusCode(200);
        $response->headers->set('Content-Type', 'application/x-pem-file; charset=utf-8');
        $response->headers->set('Content-Transfer-Encoding', 'Binary');
        $response->setCallback(function ()
        {
            /**
             * @var $keyPair KeyPair
             */
            $keyPair = KeyPair::ofHostingId($this->getWhmcsParamByKey("serviceid"))->firstOrFail();
            echo $keyPair->getPrivate();
            //delete private key
            if ($this->configuration()->isSshDeletePrivateKey())
            {
                $keyPair->setPrivate(null);
                $keyPair->save();
            }
            die();
        });
        $response->headers->set(
            'Content-Disposition', 'attachment; filename="id_rsa"'
        );
        $response->send();
    }
}
