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

class Response {

	public $CI;

	protected $_data = array();

	protected $_message = array();

	protected $_type = '';

	public $message_code = array(
		'error' => 0,
		'success' => 1,
		'warning' => 2,
		'info' => 3
	);

	public function __construct() {
		$this->CI = & get_instance();
	}

	public function error($message='', $write=false) {
		$this->set_message($message, 'error', $write);
		return $this;
	}

	public function success($message='', $write=false) {
		$this->set_message($message, 'success', $write);
		return $this;
	}

	public function set_message($message='', $type='success', $write=false) {
		$this->_type = $type;
		if (!empty($message))
		{
			$this->_message[$type][] = lang($message);
		}

		if ($write === true)
		{
			$this->write();
		}
	}

/**
	1-value
	1-key, 2-value
	1-value, 2-prepare
	1-key, 2-value, 3-prepare
*/
	public function set_data($key='', $val='', $prepare=FALSE) {
		if (empty($key))
		{
			return $this;
		}

		if (empty($val))
		{
			if (is_array($key))
			{
				$this->_data = array_merge($this->_data, $key);
			}
			else
			{
				// $this->_data[]	= $key;
				$this->_data[$key] = $prepare ? array() : "";
			}
		}
		else
		{
			if (is_array($key) && $val === true)
			{
				$this->_data = array_merge($this->_data, $key);
			}
			elseif (is_string($key) && is_array($val))
			{
				if ($prepare)
				{
					$val = $this->prepare($val);
				}
				$this->_data[$key] = $val;
			}
			elseif (is_string($key))
			{
				$this->_data[$key] = $val;
			}
		}

		return $this;
	}

	public function prepare($data) {
		if (!empty($data)) {
			$format_data = array(
				'obkeys' => array(),
				'obvalues' => array()
			);

			$format_data['obkeys'] = array_keys(reset($data));

			foreach ($data as $row) {
				$format_data['obvalues'][] = array_values($row);
			}
			$data = $format_data;
		}

		return $data;
	}

	public function clear_message($type) {
		if (empty($type)) {
			$this->message	= array();
			return $this;
		}

		unset($this->message[$type]);

		return $this;
	}

	public function get_messages($type='') {
		$return	= '';
		if (!empty($this->_message)) {
			if (empty($type)) {
				foreach ($this->_message as $type=>$msgArr) {
					$return[$type]	= nl2br(implode(PHP_EOL, $msgArr));
				}
			} else if(!empty($this->_message[$type])) {
				$return	= nl2br(implode(PHP_EOL, $this->_message[$type]));
			}
		}

		return $return;
	}

	public function get_data($key='', $default='') {
		if (empty($key)) {
			return $this->_data;
		}

		return (array_key_exists($key, $this->_data)) ? $this->_data[$key] : $default;
	}

	public function unset_data($key='') {
		if (empty($key)) {
			return false;
		}

		$key = is_array($key) ? $key : array($key);

		foreach ($key as $k=>$v) {
			unset($this->_data[$v]);
		}

		return $this;
	}

	public function truncate_data() {
		$this->_data = array();
		return $this;
	}

	public function inValidMethod() {

	}

	public function show_405($msg='')
	{
		$msg = $msg ? $msg : 'Method not allowed';
		set_status_header(405, $msg);
		$this->error($msg);
		return $this;
	}

	public function show_404($msg='')
	{
		$msg = $msg ? $msg : 'File not found';
		set_status_header(404, $msg);
		$this->error($msg);
		return $this;
	}

	public function write($data='') {
		// $this->set_data($data);

		$messages = $this->get_messages($this->_type);

		if (is_array($messages)) {
			foreach ($messages as $type=>$msg) {
				$this->CI->session->set_flashdata($type, $msg);
			}
		} else {
			$this->CI->session->set_flashdata($this->_type, $messages);
		}

		if (!empty($data['redirect'])) {
			redirect($data['redirect']);
		}

		return true;
	}
}

/*
	Ajax Response

	Usage:

	$this->response->success();
	$this->response->set_data('items', $items, TRUE);
	$this->response->write();

	$this->response->error('ACTIVE_JOB_ERROR');
	$this->response->write();

*/
class ResponseAjax extends Response {

	public function __construct() {
		$this->CI = & get_instance();
	}

	public function write($key='', $val='', $prepare=false) {
		$this->set_data($key, $val, $prepare);

		$this->_type = empty($this->_type) ? "success" : $this->_type;

		$code = $this->message_code[$this->_type];
		$message = $this->get_messages($this->_type);

		$return	= array($code, $message, $this->_data);

		$this->echo_json($return);
	}

	public function show_error($message='') {
		$this->echo_json(array(false, lang('INVALID_REQUEST') ));
	}

	public function echo_json($data=array()){
		// ob_clean();
// set_status_header
		header('Content-type: application/json');
		echo json_encode($data);
		// exit;
	}
}


