<?php if (!defined('BASEPATH')) {
    exit('No direct script access allowed');
}

use GO\Scheduler;

class MY_Controller extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->helper('general');
        $this->load->driver('cache', array('adapter' => 'file'));
        $this->setSite(0);
        date_default_timezone_set($this->config->item('time_reference'));
        define('SITE_NAME', $this->data['settings']->siteTitle);
        define('THEME', $this->data['settings']->siteTheme);
        define('THEME_FOLDER', BASE_URL . '/theme/' . THEME);
        if (defined('HOST_ONLY_MAINTENANCE') || $this->data['settings']->siteMaintenance) {
            $this->maintenance();            
        } else {
            $this->schedule();
            if (isAdminSite()) {
                $this->load->model('post_model', 'post');
                $this->load->model('page_model', 'page');
                $this->load->model('category_model', 'category');
            }
        }
    }

    private function setSite($siteID)
    {
        r(
            function () use ($siteID) {
                $this->load->model('settings_model');
                $this->data = ['settings' => $this->settings_model->findBySiteID($siteID)];
            },
            ['n' => 'settings', 't' => 60 * 60]
        );
    }

    private function schedule()
    {
        $this->load->library('parser');
        $this->load->library('cacher');
        s(function () {
            $this->load->model('post_model');
            $posts = $this->post_model->getPostsScheduled();
            $publishing = [];
            $t = time();
            foreach ($posts as $p) {
                if (strtotime($p->run_at) < $t) {
                    array_push($publishing, $p->postID);
                }
            }
            if (count($publishing)) {
                $this->post_model->publishPosts($publishing);
            }
        }, 60 * 10, 'publish');
    }

    protected function isMobile()
    {
        return defined('MOBILE') || $this->ua->is_mobile();
    }

    protected function error()
    {
        $this->data['page']['pageTitle'] = "Oops, Error";
        $this->data['page']['pageDescription'] = "Oops, Error";
        $this->data['page']['pageKeywords'] = "Oops, Error";
        $this->data['page']['pageID'] = "0";
        $this->data['page']['pageTemplate'] = "error";
        $this->data['header'] = $this->load->view('templates/header', $this->data, true);
        $this->data['footer'] = $this->load->view('templates/footer', '', true);
        $this->load->view('templates/' . $this->data['page']['pageTemplate'], $this->data);
    }

    protected function maintenance()
    {
        // $this->data['page']['pageTitle'] = "Maintenance Mode";
        // $this->data['page']['pageDescription'] = "Maintenance Mode";
        // $this->data['page']['pageKeywords'] = "Maintenance Mode";
        // $this->data['page']['pageID'] = "0";
        // $this->data['page']['pageTemplate'] = "maintenance";
        // $this->data['header'] = $this->load->view('templates/header', $this->data, true);
        // $this->data['footer'] = $this->load->view('templates/footer', '', true);
        // $this->load->view('templates/' . $this->data['page']['pageTemplate'], $this->data);

        echo $this->load->view('templates/others/underconstruct', null, true);
        exit(3);
    }

    protected function response($body) {
        $this->output
            ->set_status_header(200)
            ->set_content_type('application/json')
            ->set_output(json_encode($body));
    }
}
