<?php

class User extends CI_Controller {

	public function login() {
		$phone = $this->input->post('phone');
		$password = $this->input->post('password');
		$date = $this->input->post('date');
		$androidID = $this->input->post('android_id');
		$users = $this->db->query("SELECT * FROM `viperuser` WHERE `phone`='" . $phone . "' AND `password`='" . $password . "'")->result_array();
		if (sizeof($users) > 0) {
			$viperuser = $users[0];
			/*if ($viperuser['android_id'] == NULL || $viperuser['android_id'] == null || $viperuser['android_id'] == '') {
				$this->db->where('id', intval($viperuser['id']));
				$this->db->update('viperuser', array(
					'android_id' => $androidID
				));
				echo json_encode(array(
					'response_code' => 1,
					'user_id' => intval($viperuser['id'])
				));
			} else {*/
				if ($viperuser['android_id'] != $androidID) {
					echo json_encode(array(
						'response_code' => -1
					));
				} else {
					$expiry = $viperuser['expiry'];
					if (strtotime($date) >= strtotime($expiry)) {
						echo json_encode(array(
							'response_code' => -3,
							'user_id' => intval($viperuser['id'])
						));
					} else {
						echo json_encode(array(
							'response_code' => 1,
							'user_id' => intval($viperuser['id']),
							'expiry' => $viperuser['expiry']
						));
					}
				}
			/*}*/
		} else {
			echo json_encode(array(
				'response_code' => -2
			));
		}
	}

	public function login_with_google() {
		$phone = $this->input->post('phone');
		$googleID = $this->input->post('google_id');
		$androidID = $this->input->post('android_id');
		$expiry = $this->input->post('expiry');
		$users = $this->db->query("SELECT * FROM `viperuser` WHERE `google_id`='" . $googleID . "'")->result_array();
		if (sizeof($users) > 0) {
			$viperuser = $users[0];
			$userID = intval($viperuser['id']);
			$this->db->query("UPDATE `viperuser` SET `android_id`='" . $androidID . "', `phone`='" . $phone . "', `expiry`='" . $expiry . "' WHERE `id`=" . $userID);
			echo json_encode(array(
				'response_code' => 1,
				'user_id' => $userID,
				'profile_completed' => intval($viperuser['profile_completed']),
				'viperuser' => $viperuser
			));
		} else {
			$this->db->query("INSERT INTO `viperuser` (`android_id`, `google_id`, `phone`, `expiry`) VALUES ('" . $androidID . "', '" . $googleID . "', '" . $phone . "', '" . $expiry . "')");
			$userID = intval($this->db->insert_id());
			echo json_encode(array(
				'response_code' => -1,
				'user_id' => $userID
			));
		}
	}
	
	public function complete_data() {
		$userID = intval($this->input->post('id'));
		$firstName = $this->input->post('first_name');
		$lastName = $this->input->post('last_name');
		$email = $this->input->post('email');
		$this->db->where('id', $userID);
		$this->db->update('viperuser', array(
			'first_name' => $firstName,
			'last_name' => $lastName,
			'email' => $email,
			'profile_completed' => 1
		));
	}
	
	public function is_profile_completed() {
		$userID = intval($this->input->post('id'));
		$users = $this->db->query("SELECT * FROM `viperuser` WHERE `id`=" . $userID)->result_array();
		if (sizeof($users) > 0) {
			$viperuser = $users[0];
			echo json_encode(array(
				'profile_completed' => intval($viperuser['profile_completed']),
				'viperuser' => $viperuser
			));
		} else {
			echo json_encode(array(
				'profile_completed' => 0
			));
		}
	}
}
