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

class Api_login_activity_cek extends CI_Controller
{

	public function __construct()
	{
		parent::__construct();
		$this->load->helper(['jwt', 'authorization']);
	}


	public function index()
	{
		if ($_SERVER['REQUEST_METHOD'] == 'POST') {
			date_default_timezone_set("Asia/Jakarta");
			$mst_user_email =  $this->mst_user_email();
			$mst_user_password =  sha1($this->mst_user_password());

			$mst_user_token =  "TOKEN-" . strtoupper(substr(sha1(uniqid()), 0, 10)) . "-" . rand(0, 999) . "-" . date('d-m-Y--H:m:s');

			//CEK EMAIL
			$email = $this->db->query("SELECT * FROM tbl_mst_user WHERE mst_user_email='$mst_user_email'");
			if ($email->num_rows() > 0) {
				//CEK KARDENSIAL LOGIN
				$cek = $this->db->query("SELECT * FROM tbl_mst_user WHERE mst_user_email='$mst_user_email' and mst_user_password='$mst_user_password'");
				if ($cek->num_rows() > 0) {
					//UPDATE TOKEN
					if ($mst_user_email != "dummy@gmail.com") {
						$this->db->query("UPDATE tbl_mst_user set mst_user_token='$mst_user_token' WHERE mst_user_email='$mst_user_email'");
					}


					//PANGGIL USER
					$user = $this->db->query("SELECT * FROM tbl_mst_user WHERE mst_user_email='$mst_user_email'");
					if ($user->num_rows() > 0) {
						foreach ($user->result() as $row) {
							$mst_user_status = $row->mst_user_status;
							$data['user'][] = array(
								'mst_user_kode' => encrypt($row->mst_user_kode, $this->config->item('key')),
								'mst_user_nama_lengkap' => encrypt($row->mst_user_nama_lengkap, $this->config->item('key')),
								'mst_user_nomor_hp' => encrypt($row->mst_user_nomor_hp, $this->config->item('key')),
								'mst_user_email' => encrypt($row->mst_user_email, $this->config->item('key')),
								'mst_user_tipe' => encrypt($row->mst_user_tipe, $this->config->item('key')),
								'mst_user_referal_kode' => encrypt($row->mst_user_referal_kode, $this->config->item('key')),
								'mst_user_referal_bonus' => encrypt($row->mst_user_referal_bonus, $this->config->item('key')),
								'mst_user_token' => encrypt($row->mst_user_token, $this->config->item('key')),
								'mst_user_status' => encrypt($row->mst_user_status, $this->config->item('key')),
							);
						}

						if ($mst_user_status == "0") {
							$sess["status"] = "001";
							$sess["pesan"] = $this->config->item('error_login_berhasil');
							$keytoken = strtoupper(substr(sha1(uniqid()), 0, 16));;
							$sess['data'] = array(
								'token' =>  encrypt($keytoken, $this->config->item('keytoken')),
								'value' =>  encrypt(json_encode($data), $keytoken)
							);
							echo json_encode($sess);
						}

						//AKUN PENDING
						if ($mst_user_status == "1") {
							$sess["status"] = "005";
							$sess["pesan"] = $this->config->item('error_login_pending');
							$keytoken = strtoupper(substr(sha1(uniqid()), 0, 16));;
							$sess['data'] = array(
								'token' =>  encrypt($keytoken, $this->config->item('keytoken')),
								'value' =>  encrypt(json_encode(array()), $keytoken)
							);
							echo json_encode($sess);
						}

						//AKUN DIBLOKIR
						if ($mst_user_status == "2") {
							$sess["status"] = "006";
							$sess["pesan"] = $this->config->item('error_login_blokir');
							$keytoken = strtoupper(substr(sha1(uniqid()), 0, 16));;
							$sess['data'] = array(
								'token' =>  encrypt($keytoken, $this->config->item('keytoken')),
								'value' =>  encrypt(json_encode(array()), $keytoken)
							);
							echo json_encode($sess);
						}
					} else {
						$sess["status"] = "003";
						$sess["pesan"] = $this->config->item('error_login_gagal');
						$keytoken = strtoupper(substr(sha1(uniqid()), 0, 16));;
						$sess['data'] = array(
							'token' =>  encrypt($keytoken, $this->config->item('keytoken')),
							'value' =>  encrypt(json_encode(array()), $keytoken)
						);
						echo json_encode($sess);
					}
				} else {
					//EMAIL ATAU PASSWORD SALAH
					$sess["status"] = "002";
					$sess["pesan"] = $this->config->item('error_login_salah');
					$keytoken = strtoupper(substr(sha1(uniqid()), 0, 16));;
					$sess['data'] = array(
						'token' =>  encrypt($keytoken, $this->config->item('keytoken')),
						'value' =>  encrypt(json_encode(array()), $keytoken)
					);
					echo json_encode($sess);
				}
			} else {
				//BELUM TERDAFTAR
				$sess["status"] = "007";
				$sess["pesan"] = $this->config->item('error_login_belum_terdaftar');
				$keytoken = strtoupper(substr(sha1(uniqid()), 0, 16));;
				$sess['data'] = array(
					'token' =>  encrypt($keytoken, $this->config->item('keytoken')),
					'value' =>  encrypt(json_encode(array()), $keytoken)
				);
				echo json_encode($sess);
			}
		}
	}


	private function mst_user_email()
	{
		$input = decrypt($this->input->post("mst_user_email", true), $this->config->item('key'));
		$token = AUTHORIZATION::generateToken($input);
		try {
			$data = AUTHORIZATION::validateToken($token);
			if ($data === false) {
				$sess["status"] = "004";
				$sess["pesan"] = $this->config->item('error_parameter');
				$keytoken = strtoupper(substr(sha1(uniqid()), 0, 16));;
				$sess['data'] = array(
					'token' =>  encrypt($keytoken, $this->config->item('keytoken')),
					'value' =>  encrypt(json_encode(array()), $keytoken)
				);
				echo json_encode($sess);
				exit();
			} else {
				return $data;
			}
		} catch (Exception $e) {
			$sess["status"] = "004";
			$sess["pesan"] = $this->config->item('error_parameter');
			$keytoken = strtoupper(substr(sha1(uniqid()), 0, 16));;
			$sess['data'] = array(
				'token' =>  encrypt($keytoken, $this->config->item('keytoken')),
				'value' =>  encrypt(json_encode(array()), $keytoken)
			);
			echo json_encode($sess);
			exit();
		}
	}

	private function mst_user_password()
	{
		$input = decrypt($this->input->post("mst_user_password", true), $this->config->item('key'));
		$token = AUTHORIZATION::generateToken($input);
		try {
			$data = AUTHORIZATION::validateToken($token);
			if ($data === false) {
				$sess["status"] = "004";
				$sess["pesan"] = $this->config->item('error_parameter');
				$keytoken = strtoupper(substr(sha1(uniqid()), 0, 16));;
				$sess['data'] = array(
					'token' =>  encrypt($keytoken, $this->config->item('keytoken')),
					'value' =>  encrypt(json_encode(array()), $keytoken)
				);
				echo json_encode($sess);
				exit();
			} else {
				return $data;
			}
		} catch (Exception $e) {
			$sess["status"] = "004";
			$sess["pesan"] = $this->config->item('error_parameter');
			$keytoken = strtoupper(substr(sha1(uniqid()), 0, 16));;
			$sess['data'] = array(
				'token' =>  encrypt($keytoken, $this->config->item('keytoken')),
				'value' =>  encrypt(json_encode(array()), $keytoken)
			);
			echo json_encode($sess);
			exit();
		}
	}
}
