<?php

namespace Benhauer\Salesmanago\Cron;

use \Magento\Framework\App\Console;
use \Magento\Framework\App\Cron;
use \Magento\Framework\App\State;
use \Magento\Framework\ObjectManagerInterface;

use \Psr\Log\LoggerInterface;

use \Benhauer\Salesmanago\Model\Config\PurchasesConfig;
use \Benhauer\Salesmanago\Controller\Adminhtml\Config\CronExportData;

class Purchase extends Cron
{
    protected $logger;
    protected $purchasesConfig;
    protected $cronExportData;
    protected $date;

    public function __construct(
        State $state,
        Console\Request $request,
        Console\Response $response,
        ObjectManagerInterface $objectManager,

        PurchasesConfig $purchasesConfig,
        LoggerInterface $logger,
        CronExportData $cronExportData,
        \Magento\Framework\Stdlib\DateTime\TimezoneInterface $date,
        array $parameters = []
    ) {
        parent::__construct($state, $request, $response, $objectManager, $parameters);
        $this->logger = $logger;
        $this->purchasesConfig = $purchasesConfig;
        $this->cronExportData = $cronExportData;
        $this->date = $date;
    }

    public function execute()
    {
        if (!$this->purchasesConfig->isCronActive()) {
            return false;
        }

        $confTimeHours = $this->purchasesConfig->getConfig();

        $intervalUnixTime = $confTimeHours*60*60;

        $fromTime = time()-$intervalUnixTime;
        $fromTime = date('Y-m-d H:i:s', $fromTime);

        $toTime = time();
        $toTime = date('Y-m-d H:i:s', $toTime);

        $response = $this->cronExportData
            ->setAdvancedExport($fromTime, $toTime)
            ->setEvents();

        $this->logger->info($response);
    }

}
