<?php

namespace Webkul\ScheduleCronBundle\Services;

use Psr\Container\ContainerInterface;
use Webkul\ScheduleCronBundle\Entity\CronJobsMapping;

class ScheduleCronConnector
{
    private $stepExecution;

    public function __construct(private ContainerInterface $container,private \Doctrine\ORM\EntityManager $em)
    {}
    
    public function setStepExecution($stepExecution)
    {
        $this->stepExecution = $stepExecution;
    }

    public function saveData($params)
    {
        $customCronCheck = isset($params['check_custom_cron']) ? $params['check_custom_cron'] : false;
        $customcron = "N/A";
        $intervelday ="N/A";
        $interveltime ="N/A";
        $jobType= $params['jobType'];

        if ($customCronCheck === 'true') {
            $customcron = isset($params['custom_cron']) ? str_replace('_', ' ', $params['custom_cron']) : null;
        } else {
            $intervelday = isset($params['intervelday']) ? $params['intervelday'] : null;
            $interveltime = isset($params['interveltime']) ? $params['interveltime'] : null;
        }

        $config =  new CronJobsMapping;
        $config->setName($params['name']);
        $config->setStatus("1");
        $config->setIntervelDay($intervelday);
        $config->setIntervelTime($interveltime);
        $config->setCustomCron($customcron);
        $config->setJobProfile($params['job']);
        $config->setCronCmd($params['cronCmd']);
        $config->setjobType($jobType);
        $this->em->persist($config);
        $this->em->flush();
    }

    public function getData()
    {
        $config = $this->em->getRepository('ScheduleCronBundle:CronJobsMapping')->findAll();

        return $config;
    }
    
    public function getRootPath()
    {
        return $this->container->get('kernel')->getProjectDir();
    }
}
