<?php

/*
 * *************************************************
 * Copyright (C) 2021 Earworks.                    *
 * Codeigniter Project Template Modification       *
 * Please contact our email for more information : *
 * contact@earworks.co.id                          *
 * *************************************************
 */

/**
 *
 * @author Andy
 */
trait MY_Controller_Trait_Api {

    protected function _init_api($mustHaveUser = true) {
        $api_token = $this->input->get_request_header("Authorization");
        $user_id = $this->input->get_request_header("User-ID");
        $user_token = $this->input->get_request_header("Session-Token");
        if (!$api_token || $api_token != API_TOKEN) {
            return $this->_version_invalid();
        }
        if ($mustHaveUser == true) {
            if (!$user_id || !$user_token) {
                return $this->_invalid_login();
            } else {
                $user = User_model::get_dbo()->where('id', $user_id)->where('login_token', $user_token)->first();
                if (!$user) {
                    return $this->_invalid_login();
                } else {
                    $this->user['user'] = $user;
                    $this->user['profile'] = $user->profile;
                }
            }
        }
    }

    protected function _success($message, $data = [], $as_json = true) {
        $parse_array = array(
            self::$field_time => round(microtime(true) * 1000) - APP_START,
            self::$field_status => self::CODE_SUCCESS,
            self::$field_message => $message,
            self::$field_data => $data,
        );

        return $this->_output_parse($parse_array, 200, $as_json);
    }

    protected function _error($message, $data = [], $as_json = true) {
        $parse_array = array(
            self::$field_time => round(microtime(true) * 1000) - APP_START,
            self::$field_status => self::CODE_ERROR,
            self::$field_message => $message,
            self::$field_data => $data,
        );

        return $this->_output_parse($parse_array, 200, $as_json);
    }

    protected function _invalid_parameter($data = [], $as_json = true) {
        $parse_array = array(
            self::$field_time => round(microtime(true) * 1000) - APP_START,
            self::$field_status => self::CODE_INVALID_PARAMETER,
            self::$field_message => lang('err_invalid_parameter'),
            self::$field_data => $data,
        );
        return $this->_output_parse($parse_array, 200, $as_json);
    }

    protected function _invalid_request($as_json = true) {
        $parse_array = array(
            self::$field_time => round(microtime(true) * 1000) - APP_START,
            self::$field_status => self::CODE_INVALID_REQUEST,
            self::$field_message => lang('err_invalid_request'),
            self::$field_data => [],
        );
        return $this->_output_parse($parse_array, 200, $as_json);
    }

    protected function _permission_denied($as_json = true) {
        $parse_array = array(
            self::$field_time => round(microtime(true) * 1000) - APP_START,
            self::$field_status => self::CODE_PERMISSION_DENIED,
            self::$field_message => lang('err_permission_denied'),
            self::$field_data => [],
        );
        return $this->_output_parse($parse_array, 200, $as_json);
    }

    protected function _invalid_login($as_json = true) {
        $parse_array = array(
            self::$field_time => round(microtime(true) * 1000) - APP_START,
            self::$field_status => self::CODE_NOT_LOGIN,
            self::$field_message => lang('err_invalid_login'),
            self::$field_data => [],
        );
        return $this->_output_parse($parse_array, 200, $as_json);
    }

    protected function _not_found($as_json = true) {
        $parse_array = array(
            self::$field_time => round(microtime(true) * 1000) - APP_START,
            self::$field_status => self::CODE_NOT_FOUND,
            self::$field_message => lang('err_not_found'),
            self::$field_data => [],
        );
        return $this->_output_parse($parse_array, 200, $as_json);
    }

    protected function _server_error($as_json = true) {
        $parse_array = array(
            self::$field_time => round(microtime(true) * 1000) - APP_START,
            self::$field_status => self::CODE_ERROR,
            self::$field_message => lang('err_server'),
            self::$field_data => [],
        );

        return $this->_output_parse($parse_array, 500, $as_json);
    }

    protected function _version_invalid($as_json = true) {
        $parse_array = array(
            self::$field_time => round(microtime(true) * 1000) - APP_START,
            self::$field_status => self::CODE_VERSION_INVALID,
            self::$field_message => lang('err_version_invalid'),
            self::$field_data => [],
        );
        return $this->_output_parse($parse_array, 200, $as_json);
    }

    protected function _other_output($code, $message = null, $data = [], $as_json = true) {
        $parse_array = array(
            self::$field_time => round(microtime(true) * 1000) - APP_START,
            self::$field_status => $code,
            self::$field_message => $message,
            self::$field_data => $data,
        );
        return $this->_output_parse($parse_array, 200, $as_json);
    }

    private function _output_parse($parse_array, $status = 200, $as_json = true) {
        if ($as_json == true) {
            header('Content-Type: application/json');
            echo json_encode($parse_array);
            exit;
        }
        return $parse_array;
    }

}
