<?php

/*
 * Copyright (c) 2018 Earworks.
 * Codeigniter Project Template Modification
 * Please contact our email for more information :
 * contact@earworks.co.id
 */
defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * First job before all action running
 * 1. define locale for date time
 * 2. define constant required for device detect :
 *      IS_MOBILE, IS_TABLET, IS_DESKTOP, MDETECT
 * 3. manage asset for all page
 * 
 * NOTE :
 * you can add new asset like css or js in view file with :
 * $this->render->add_css/add_js/add_meta()
 * or you can add manualy on layout file
 * 
 * @author Fajar Dwi Rianto <fajar@earworks.co.id>
 * @since 1.0
 */
class Base_Controller extends CI_Controller {

    public $is_login = false;

    const BACKOFFICE_LOGIN = 'backoffice/User/login';
    const BACKOFFICE_DASHBOARD = 'backoffice/Dashboard';
    const FRONTLOGIN = 'login/login';
    const FRONTDASHBOARD = 'site/home';
    const APILOGIN = 'api/User/login';
    const APIDASHBOARD = 'api/Dashboard/index';

    public function __construct() {

        parent::__construct();

        $this->_device_detect();

        if (APPLICATION == 'backoffice') {
            $this->_init_backend();
            $this->_check_login();
        } elseif (APPLICATION == 'frontoffice') {
            $this->_init_frontend();
        }
        $this->_set_language();
    }

    /**
     * language for datetime
     * language for text
     */
    private function _set_language() {
        $lang = $this->session->userdata('language');
        if ($lang == 'us' || !$lang) {
            $this->lang->load('app', 'english');
            setcookie(CI_NAME . '_lang', 'english', strtotime("+30 days", time()), '/');
            
        } elseif ($lang == 'id') {
            $this->lang->load('app', 'indonesian');
            setcookie(CI_NAME . '_lang', 'indonesian', strtotime("+30 days", time()), '/');
        }
    }

    private function _device_detect() {
        if (class_exists('Mobile_Detect')) {
            $mobile = new Mobile_Detect;
            defined('MDETECT') OR define('MDETECT', true);
            defined('IS_MOBILE') OR define('IS_MOBILE', $mobile->isMobile());
            defined('IS_TABLET') OR define('IS_TABLET', $mobile->isTablet());
            defined('IS_DESKTOP') OR define('IS_DESKTOP', (!IS_MOBILE && !IS_TABLET));
        } else {
            defined('MDETECT') OR define('MDETECT', false);
            defined('IS_MOBILE') OR define('IS_MOBILE', false);
            defined('IS_TABLET') OR define('IS_TABLET', false);
            defined('IS_DESKTOP') OR define('IS_DESKTOP', false);
        }
    }

    private function _init_backend() {
        $this->load->template_folder = APPLICATION;
        $child = get_called_class();
        if (IS_DESKTOP || MDETECT == false) {
            $this->load->add_meta('viewport', 'width=device-width, initial-scale=1');
        } else {
            $this->load->add_meta('viewport', 'width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no');
        }
        $this->load->model('user_model');
        $this->load->helper('cookie');
        if (!$this->session->has_userdata('language')) {
            $this->session->set_userdata('language', 'us');
        }
    }

    private function _check_login() {
        if (!$this->session->has_userdata(CI_NAME .'log')) {
            $auth = get_cookie(CI_NAME . '_auth_key', true);
            if ($auth && ($user = $this->user_model->get_auth($auth))) {
                $this->session->set_userdata(CI_NAME . 'log', $this->user_model->session_template($user));
            } else {
                return false; // not login
            }
        }
        $session = $this->session->userdata(CI_NAME .'log');
        $this->is_login = true;
        if (isset($this->allowed_role)) {
            if (!in_array($session['roleid'], $this->allowed_role)) {
                redirect(self::BACKOFFICE_DASHBOARD);
            }
        }
    }

    private function _check_login_frontend() {
        if (!$this->session->has_userdata(CI_NAME .'logfrontend')) {
            $auth = get_cookie(CI_NAME . '_auth_frontend', true);
            if ($auth && ($user = $this->user_model->get_auth($auth))) {
                $this->session->set_userdata(CI_NAME . 'logfrontend', $this->user_model->session_template($user));
            } else {
                return false; // not login
            }
        }
        $session = $this->session->userdata(CI_NAME .'logfrontend');
        $this->is_login = true;
        if (isset($this->allowed_role)) {
            if (!in_array($session['roleid'], $this->allowed_role)) {
                redirect(self::FRONTDASHBOARD);
            }
        }
    }

    private function _init_frontend() {
        $min = (ENVIRONMENT == 'development') ? null : '.min';

        /*if (IS_DESKTOP || MDETECT == false) {
            $this->load->add_meta('viewport', 'width=device-width, initial-scale=1');
        } else {
            $this->load->add_meta('viewport', 'width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no');
        } */
        $this->load->add_meta('viewport', 'width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no');

        /*$this->load->add_css('assets/plugins/bootstrap/css/bootstrap' . $min . '.css');
        $this->load->add_css('assets/plugins/font-awesome/css/font-awesome' . $min . '.css');
        $this->load->add_css('assets/css/style.css');

        $this->load->add_js('assets/plugins/jquery/jquery' . $min . '.js');
        $this->load->add_js('assets/plugins/bootstrap/js/bootstrap' . $min . '.js');
        $this->load->add_js('assets/js/script.js'); */
    }

}
