<?php

namespace ModulesGarden\Servers\OpenXchange\Core\CommandLine;

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

class JobsLoop extends AbstractCommand
{
    /**
     * Loop interval in seconds
     * @var int
     */
    protected $loopInterval = 5;

    /**
     * @var int
     */
    protected $maxChildren  = 5;

    /**
     * @var int
     */
    protected $tasksPerChildren = 10;
    /**
     *
     */
    final protected function configure(): void
    {
        parent::configure();
    }

    /**
     *
     */
    final protected function setup()
    {
        $this->addOption('parent', InputOption::VALUE_OPTIONAL, 'Parent id', null);
        $this->addOption('id', InputOption::VALUE_OPTIONAL, 'Child id', null);
    }

    /**
     * Execute command in the loop
     * @param InputInterface $input
     * @param OutputInterface $output
     */
    final protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $parent             = $input->getOption('parent');
        $id                 = $input->getOption('id');
        $parentHypervisor   = new Hypervisor($this->getName());

        //Lock parent
        if(!$parent)
        {
            $parentHypervisor->lock();
        }
        else
        {
            $parentHypervisor->checkIfRunning();
        }

        if($id)
        {

        }

        do
        {
            parent::process($input, $output, new SymfonyStyle($input, $output));

            sleep($this->loopInterval);
        }
        while(!$parentHypervisor->isStopped());

        return self::SUCCESS;
    }

    /**
     * @param $jobs[]
     */
    protected function processJobs($jobs)
    {
        foreach($jobs as $job)
        {

        }
    }
}
