<?php

/* * ********************************************************************
 * ProxmoxAddon product developed. (Sep 19, 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\Cron;

use MGProvision\Proxmox\v2 as proxmox;
use ModulesGarden\ProxmoxAddon as main;
use ModulesGarden\ProxmoxAddon\Core\CommandLine\Command;
use ModulesGarden\ProxmoxAddon\Core\CommandLine\Hypervisor;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

/**
 * Description of Task
 *
 * @author Pawel Kopec <pawelk@modulesgardne.com>
 */
class Task extends Command
{

    use main\App\Services\BaseService;
    /**
     * Command name
     * @var string
     */
    protected $name = 'task';

    /**
     * Command description
     * @var string
     */
    protected $description = '';

    /**
     * Command help text
     * @var string
     */
    protected $help = '';

    protected function process(InputInterface $input, OutputInterface $output, SymfonyStyle $io)
    {
        $io->title('Synchronize tasks: Starting');
        $i = 0;
        foreach (main\App\Models\TaskHistory::where('status', "0")->get() as $taskHistory)
        {
            try
            {
                /* @var $taskHistory main\App\Models\TaskHistory */
                $i++;
                if (!$taskHistory->hosting)
                {
                    $taskHistory->delete();
                    $io->warning(sprintf("Task: %s, Hosting %s does not exist.", $taskHistory->id, $taskHistory->hosting_id));
                    continue;
                }
                $this->setServerId($taskHistory->hosting->server);
                if ($this->getServer()->id != $this->getServerId())
                {
                    unset($this->server, $this->api);
                }
                $this->getApi()->setInstance();
                $node        = new proxmox\models\Node($taskHistory->node);
                $proxmoxTask = $node->task($taskHistory->upid);
                if (!$proxmoxTask->getExitstatus())
                {
                    $output->writeln(sprintf("Task: %s current in progress.", $taskHistory->id));
                    continue;
                }
                $taskHistory->description = $proxmoxTask->getExitstatus();
                $taskHistory->start_time  = $proxmoxTask->getStartDate();
                $taskHistory->status      = $proxmoxTask->getExitstatus() == "OK" ? "1" : "2";
                $taskHistory->save();
                $output->writeln(sprintf("Task: %s has been synchronized.", $taskHistory->id));
            }
            catch (\Exception $ex)
            {
                if ($taskHistory)
                {
                    $io->error("Task Id #{$taskHistory->id}, " . $ex->getMessage());
                }
                else
                {
                    $io->error($ex->getMessage());
                }
            }
            (new Hypervisor($this->getName(), $input->getOptions()))
                ->ping();
        }
        $output->writeln("");
        $io->success([
            sprintf("Synchronize tasks: %s Entries Processed.", $i),
            "Synchronize tasks: Done"
        ]);
    }
}
