<?php

namespace App\Http\Controllers\API;

use Illuminate\Http\Request;
use App\Http\Requests\RegisterRequest;
use App\Http\Controllers\Api\BaseController;
use Illuminate\Support\Facades\Auth;

Use App\Staff;
Use App\Model\StaffDetail;

Use Hash;
use Carbon\Carbon;
use Crypt;
use File;
use Mail;

class LoginMobile extends BaseController
{
	function random_code($limit) {
		return substr(base_convert(sha1(uniqid(mt_rand())), 16, 36), 0, $limit);
	}
	
	public function encryptPassword($action, $string) {
		$output = false;
		$encrypt_method = "AES-256-CBC";
		$secret_key = '28472B4B6250655368566D5970337336';
		$secret_iv =  md5(md5($string)); //'This is my secret iv';
		
		// hash
		$key = hash('sha256', $secret_key);
		
		$iv = substr(hash('sha256', $secret_iv), 0, 16);
		if ( $action == 'encrypt' ) {
			$output = openssl_encrypt($string, $encrypt_method, $key, 0, $iv);
			$output = base64_encode($output);
		} else if( $action == 'decrypt' ) {
			$output = openssl_decrypt(base64_decode($string), $encrypt_method, $key, 0, $iv);
		}
		return $output;
	}
	
	/** 
	/* Login Function
	**/
	
	public function requestLogin(Request $request) {
		$user = ''; $data = array();
		
		$rememberMe = false;
		$user = Staff::where('email', $request->email)->where('roles_access','Installer')->first();
		if (!empty($user)) {
			
			if($this->encryptPassword('encrypt', $request->password) == $user->password || $this->encryptPassword('encrypt', $request->password) == $user->password2){
				
				$token_id = '';
				do {
					$token_id = str_random(32);
				} while (Staff::where("api_token", "=", $token_id)->first() instanceof Staff);
				
				$user->api_token = $token_id;
				$user->last_login_at = Carbon::now(new \DateTimeZone('Asia/Kuala_Lumpur'))->toDateTimeString();
				$user->last_login_ip = $request->getClientIp();
				$user->device_id = $request->deviceID;
				$user->password2 = Hash::make($request->password);
				$user->save();
				
				$loginDetail = StaffDetail::where('email',$user->email)->first();
				
				// login
				array_push($data, array(
					'User' => $loginDetail,
					'token' => $token_id
				));
				
				return $this->sendResponse($data, 'Login Successfull');
				
			}else {
				return $this->sendError('Invalid Login Detail', 'Failed');
			}
		}else{
			return $this->sendError('User not found', 'Failed');
		}
	}
	
	/** 
	/* Logout Function
	**/
	public function requestLogout(Request $request){
		
		$loginUser = Staff::where('api_token', $request->api_token)->first();
		
		if(!empty($loginUser)){
			
			$loginUser->last_login_at = Carbon::now(new \DateTimeZone('Asia/Kuala_Lumpur'))->toDateTimeString();
			$loginUser->last_login_ip = $request->getClientIp();
			$loginUser->save();
			
			return $this->sendResponse('Success', 'User logged out successfully');
		}else {
			return $this->sendError('Failed', 'Sorry, the user cannot be logged out');
		}
	}
	
	/** 
	/* Update Profile && Generate QR
	**/
	public function updateProfile(Request $request){
		
		
	}
	
	/** 
	/* Update Profile Image
	**/
	public function updateProfileImage(Request $request){
		
	}
	
	/** 
	/* Update Password
	**/
	public function updatePassword(Request $request) {
		$user = Staff::where('email', $request->email)->where('roles_access','Installer')->first();
		if(!empty($user)){
			$user->password = $this->encryptPassword('encrypt', $request->password);
			$user->save();
			return $this->sendResponse('Success', 'Your password successfully updated');
		}else{
			return $this->sendError('Failed', 'Sorry, user not found');
		}
	}
}
