<?php
    class Cron {
        protected $db;
        protected $utils;
        protected $endpoint;
        protected $api_key;

        public function __construct(Database $db, Utils $utils){
            $this->db = $db;
            $this->utils = $utils;
        }

        function setCredeantials() {
            $cron_settings  = $this->utils->getOption('cron_api');
            $this->endpoint = $cron_settings[0]["endpoint"];
            $this->api_key  = $cron_settings[0]["api_key"];
        }

        public function getJobs() {
            $this->setCredeantials();
            $curl = curl_init();
            curl_setopt_array($curl, array(
                CURLOPT_URL => $this->endpoint . 'jobs',
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_ENCODING => '',
                CURLOPT_MAXREDIRS => 10,
                CURLOPT_TIMEOUT => 0,
                CURLOPT_FOLLOWLOCATION => true,
                CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                CURLOPT_CUSTOMREQUEST => 'GET',
                CURLOPT_HTTPHEADER => array(
                    "Content-Type: application/json",
                    "authorization: Bearer {$this->api_key}",
                ),
            ));
            
            $response = curl_exec($curl);
            if($errno = curl_errno($curl)) {
                $this->utils->errorLog("error", "Get Cron Jobs. Curl Error: ".curl_error($curl), 0, $_SERVER['REQUEST_URI']);
                return array();
            }
            if(curl_getinfo($curl, CURLINFO_HTTP_CODE) != 200) {
                $this->utils->errorLog("error", "Cannot get cron jobs. Http Code: ".curl_getinfo($curl, CURLINFO_HTTP_CODE), 0, $_SERVER['REQUEST_URI']);
                return array();
            }
            curl_close($curl);
            $result = json_decode($response, true);
            
            if(!isset($result['jobs']) || empty($result['jobs'])) {
                $this->utils->errorLog("error", "Cron list empty.", 0, $_SERVER['REQUEST_URI']);
                return array();
            }

            return $result;
        }

        public function create($body) {
            $this->setCredeantials();
            $curl = curl_init();
            curl_setopt_array($curl, array(
                CURLOPT_URL => $this->endpoint . 'jobs',
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_ENCODING => '',
                CURLOPT_MAXREDIRS => 10,
                CURLOPT_TIMEOUT => 0,
                CURLOPT_FOLLOWLOCATION => true,
                CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                CURLOPT_CUSTOMREQUEST => 'PUT',
                CURLOPT_POSTFIELDS => $body,
                CURLOPT_HTTPHEADER => array(
                    "Content-Type: application/json",
                    "authorization: Bearer {$this->api_key}",
                ),
            ));
            
            $response = curl_exec($curl);
            if($errno = curl_errno($curl)) {
                $this->utils->errorLog("error", "Create Cron Job. Curl Error: ".curl_error($curl), 0, $_SERVER['REQUEST_URI']);
                return array();
            }
            if(curl_getinfo($curl, CURLINFO_HTTP_CODE) != 200) {
                $this->utils->errorLog("error", "Cannot create cron job. Http Code: ".curl_getinfo($curl, CURLINFO_HTTP_CODE), 0, $_SERVER['REQUEST_URI']);
                return array();
            }
            curl_close($curl);
            $result = json_decode($response, true);

            if(!isset($result['jobId']) || empty($result['jobId'])) {
                $this->utils->errorLog("error", "Result Empty.", 0, $_SERVER['REQUEST_URI']);
                return array();
            }

            return $result;
        }

        public function update($body, $job_id) {
            $this->setCredeantials();
            $curl = curl_init();
            curl_setopt_array($curl, array(
                CURLOPT_URL => $this->endpoint . 'jobs/'.$job_id,
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_ENCODING => '',
                CURLOPT_MAXREDIRS => 10,
                CURLOPT_TIMEOUT => 0,
                CURLOPT_FOLLOWLOCATION => true,
                CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                CURLOPT_CUSTOMREQUEST => 'PATCH',
                CURLOPT_POSTFIELDS => $body,
                CURLOPT_HTTPHEADER => array(
                    "Content-Type: application/json",
                    "authorization: Bearer {$this->api_key}",
                ),
            ));
            
            $response = curl_exec($curl);
            if($errno = curl_errno($curl)) {
                $this->utils->errorLog("error", "Create Cron Job. Curl Error: ".curl_error($curl), 0, $_SERVER['REQUEST_URI']);
                return false;
            }
            if(curl_getinfo($curl, CURLINFO_HTTP_CODE) != 200) {
                $this->utils->errorLog("error", "Cannot create cron job. Http Code: ".curl_getinfo($curl, CURLINFO_HTTP_CODE), 0, $_SERVER['REQUEST_URI']);
                return false;
            }
            curl_close($curl);

            return true;
        }

        public function getJob($id) {
            $this->setCredeantials();
            $curl = curl_init();
            curl_setopt_array($curl, array(
                CURLOPT_URL => $this->endpoint . 'jobs/'.$id,
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_ENCODING => '',
                CURLOPT_MAXREDIRS => 10,
                CURLOPT_TIMEOUT => 0,
                CURLOPT_FOLLOWLOCATION => true,
                CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                CURLOPT_CUSTOMREQUEST => 'GET',
                CURLOPT_HTTPHEADER => array(
                    "Content-Type: application/json",
                    "authorization: Bearer {$this->api_key}",
                ),
            ));
            
            $response = curl_exec($curl);
            if($errno = curl_errno($curl)) {
                $this->utils->errorLog("error", "Get Cron Job. Curl Error: ".curl_error($curl), 0, $_SERVER['REQUEST_URI']);
                return false;
            }
            if(curl_getinfo($curl, CURLINFO_HTTP_CODE) != 200) {
                $this->utils->errorLog("error", "Cannot get cron job. Http Code: ".curl_getinfo($curl, CURLINFO_HTTP_CODE), 0, $_SERVER['REQUEST_URI']);
                return false;
            }

            curl_close($curl);
            $result = json_decode($response, true);
            
            if(!isset($result) || empty($result)) {
                $this->utils->errorLog("error", "Cron list empty.", 0, $_SERVER['REQUEST_URI']);
                return array();
            }

            return $result;
        }

        public function getJobHistory($job_id) {
            $this->setCredeantials();
            $curl = curl_init();
            curl_setopt_array($curl, array(
                CURLOPT_URL => $this->endpoint . 'jobs/'.$job_id.'/history',
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_ENCODING => '',
                CURLOPT_MAXREDIRS => 10,
                CURLOPT_TIMEOUT => 0,
                CURLOPT_FOLLOWLOCATION => true,
                CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                CURLOPT_CUSTOMREQUEST => 'GET',
                CURLOPT_HTTPHEADER => array(
                    "Content-Type: application/json",
                    "authorization: Bearer {$this->api_key}",
                ),
            ));
            
            $response = curl_exec($curl);
            if($errno = curl_errno($curl)) {
                $this->utils->errorLog("error", "Get Cron History. Curl Error: ".curl_error($curl), 0, $_SERVER['REQUEST_URI']);
                return false;
            }
            if(curl_getinfo($curl, CURLINFO_HTTP_CODE) != 200) {
                $this->utils->errorLog("error", "Cannot get cron history. Http Code: ".curl_getinfo($curl, CURLINFO_HTTP_CODE), 0, $_SERVER['REQUEST_URI']);
                return false;
            }
            curl_close($curl);
            $result = json_decode($response, true);
            
            if(!isset($result['history']) || empty($result['history'])) {
                $this->utils->errorLog("error", "Cron job history empty.", 0, $_SERVER['REQUEST_URI']);
                return false;
            }

            return $result;
        }

        public function deleteJob($id) {
            $this->setCredeantials();
            $curl = curl_init();
            curl_setopt_array($curl, array(
                CURLOPT_URL => $this->endpoint . 'jobs/'.$id,
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_ENCODING => '',
                CURLOPT_MAXREDIRS => 10,
                CURLOPT_TIMEOUT => 0,
                CURLOPT_FOLLOWLOCATION => true,
                CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                CURLOPT_CUSTOMREQUEST => 'DELETE',
                CURLOPT_HTTPHEADER => array(
                    "Content-Type: application/json",
                    "authorization: Bearer {$this->api_key}",
                ),
            ));
            
            $response = curl_exec($curl);
            if($errno = curl_errno($curl)) {
                $this->utils->errorLog("error", "Delete Cron Job. Curl Error: ".curl_error($curl), 0, $_SERVER['REQUEST_URI']);
                return false;
            }
            if(curl_getinfo($curl, CURLINFO_HTTP_CODE) != 200) {
                $this->utils->errorLog("error", "Cannot delete cron job. Http Code: ".curl_getinfo($curl, CURLINFO_HTTP_CODE), 0, $_SERVER['REQUEST_URI']);
                return false;
            }
            curl_close($curl);

            return true;
        }
    }
?>