<?php
namespace App\Traits;

use App\CallLog;
use DB;

trait CallLogTrait
{
	private $targetSimNos = [
		'8991000900017194500'		=>	[
			'number'	=>	'9039322000',
			'name'		=>	'Ujjawal',
			'username'	=>	'Ujjawal'
		],
		'8991000901134831032'        =>    [
           'number'    =>    '9009222344',
           'name'      =>    'Ajeet Singh',
           'username'  =>    'Ajeet Singh'
       ],
		'8991931603954831795'		=>	[
			'number'	=>	'7879520387',
			'name'		=>	'Dipen',
			'username'	=>	'Dipen'
		],
		'8991000900423283038'		=>	[
			'number'	=>	'9770258888',
			'name'		=>	'CoalMint Office',
			'username'	=>	'CoalMint Office'
		],
		'8991930915857466276'		=>	[
			'number'	=>	'9039270082',
			'name'		=>	'Rounak',
			'username'	=>	'Rounak'
		],
		'8991930313581845686'		=>	[
			'number'	=>	'9039722000',
			'name'		=>	'Manish',
			'username'	=>	'Manish'
		],
		'8991000900569910485'		=>	[
			'number'	=>	'9770058888',
			'name'		=>	'Kabir',
			'username'	=>	'Kabir'
		],
		'8991930415740797201'		=>	[
			'number'	=>	'7024148333',
			'name'		=>	'Faizan',
			'username'	=>	'Faizan'
		],
		'8991000900577947560'		=>	[
			'number'	=>	'9993967907',
			'name'		=>	'Pankaj',
			'username'	=>	'Pankaj'
		],
		'8991000900569914065'		=>	[
			'number'	=>	'7024149333',
			'name'		=>	'Amit',
			'username'	=>	'Amit'
		],
		'8991863040005058959'		=>	[
			'number'	=>	'8770591840',
			'name'		=>	'Subhubensu Day',
			'username'	=>	'Subhubensu'
		],
		'8991863040005058961'		=>	[
			'number'	=>	'8770591842',
			'name'		=>	'Bhumika',
			'username'	=>	'Bhumika'
		],
		'8991000900262234936'		=>	[
			'number'	=>	'8602271178',
			'name'		=>	'Motiram',
			'username'	=>	'Motiram'
		],
		'8991000900569909974'		=>	[
			'number'	=>	'7024240930',
			'name'		=>	'Nagendra',
			'username'	=>	'Nagendra'
		],
		'8991931603930174195'		=>	[
			'number'	=>	'9685299747',
			'name'		=>	'Amit Soni',
			'username'	=>	'Amit'
		],
		'8991864040803447546'		=>	[
			'number'	=>	'8602270907',
			'name'		=>	'Aditya Raj',
			'username'	=>	'Aditya'
		],
		'8991931603930174138'        =>    [
           'number'    	=>	'9685299235',
           'name'       =>	'Prasasti Giri ',
           'username'   =>	'Prasasti Giri '
       ],
       '8991000900017195721'		=>    [
            'number'    =>	'7024240920',
            'name'      =>	'Akash Shukla ',
            'username'  =>	'Akash Shukla '
        ]
	];

	private $callSource = 'SM_RECORDING_APP';

	public function insertCallLog($data = array())
	{
		if(isset($data['id']) && !empty($data['id'])) {

			$checkCallLog = DB::table('tbl_dialler_records' )
				            ->select('id')
			                ->where('call_log_id' , '=' , $data['id'])
			                ->first();

			if(!$checkCallLog) {

				$caller_memid = 0;
				$caller_no = '';
				$caller_name = '';
				$caller_email = '';
				$caller_username = '';
				$caller_phoneno = '';

				$receiver_memid = 0;
				$receiver_no = '';
				$receiver_name = '';
				$receiver_email = '';
				$receiver_username = '';
				$receiver_phoneno = '';

				$phoneno = $data['phone_no'];
				if(strlen($phoneno) > 0) {
					$phoneno = substr($phoneno, -10);
				}

				$callType = $this->getCallTypeDetail($data['call_type'], $data['duration']);

				$target_sim_no = substr($data['target_sim_no'], 0, 19);

				if(strtoupper($callType['type']) == 'INBOUND') {
					$member = $this->getMemberByPhoneNo($phoneno);

					$caller_memid = $member['id'];
					$caller_name = $member['name'];
					$caller_email = $member['email'];
					$caller_username = $member['username'];
					$caller_phoneno = $data['phone_no'];

					$targetSimNo = array();
					if(isset($this->targetSimNos[$target_sim_no])) {
						$targetSimNo = $this->targetSimNos[$target_sim_no];
					}
					$receiver_phoneno = (isset($targetSimNo['number'])) ? $targetSimNo['number'] : $target_sim_no;
					$receiver_name = (isset($targetSimNo['name'])) ? $targetSimNo['name'] : '';
					$receiver_username = (isset($targetSimNo['username'])) ? $targetSimNo['username'] : '';				

				} else if(strtoupper($callType['type']) == 'OUTBOUND') {
					$targetSimNo = array();
					if(isset($this->targetSimNos[$target_sim_no])) {
						$targetSimNo = $this->targetSimNos[$target_sim_no];
					}
					$caller_phoneno = (isset($targetSimNo['number'])) ? $targetSimNo['number'] : $target_sim_no;
					$caller_name = (isset($targetSimNo['name'])) ? $targetSimNo['name'] : '';
					$caller_username = (isset($targetSimNo['username'])) ? $targetSimNo['username'] : '';
					
					$member = $this->getMemberByPhoneNo($phoneno);
					$receiver_memid = $member['id'];
					$receiver_name = $member['name'];
					$receiver_email = $member['email'];
					$receiver_username = $member['username'];
					$receiver_phoneno = $data['phone_no'];
				}

				date_default_timezone_set('Asia/Kolkata');

				$callLog = new CallLog;
				$callLog->datetime = date('Y-m-d H:i:s');
				$callLog->company = 'steelmint';
				$callLog->caller_no = $caller_phoneno;
				$callLog->caller_name = $caller_name;
				//$callLog->caller_username = $caller_username;
				$callLog->caller_email = $caller_email;
				$callLog->caller_memid = $caller_memid;

				$callLog->receiver_no = $receiver_phoneno;
				$callLog->receiver_name = $receiver_name;
				//$callLog->receiver_username = $receiver_username;
				$callLog->receiver_email = $receiver_email;
				$callLog->receiver_memid = $receiver_memid;

				$callLog->call_start_datetime = date('Y-m-d H:i:s', ($data['start_time']/1000));

				$callLog->call_type = $callType['type'];
				$callLog->caller_answer_status = '';
				$callLog->receiver_answer_status = $callType['status'];
				$callLog->call_duration = $data['duration'];
				$callLog->call_bill_sec = '';
				$callLog->call_recordpath = (isset($data['recored_file'])) ? $data['recored_file'] : '';
				$callLog->call_source = (isset($data['call_source'])) ? $data['call_source'] : $this->callSource;
				$callLog->call_log_id = (isset($data['id'])) ? $data['id'] : '';

				$callLog->save();

				return $callLog->id;
			}
		}

		return false;
	}

	protected function getMemberByPhoneNo($number = '')
	{
		$member = array(
			'id'		=>	0,
			'name'		=>	'',
			'email'		=>	'',
			'username'	=>	''
		);

		if($number) {
			$sql = "SELECT `id`, `name`, `email`, `username` FROM `tbl_members` WHERE `phoneno` = '" . $number . "' ORDER BY `id` LIMIT 0, 1";
			$result = DB::select($sql);
			if(isset($result[0]) && !empty($result[0])) {
				$member = $result[0];
			}
		}

		return $member;
	}

	protected function getCallTypeDetail($call_type = '', $duration = 0)
	{
		$type = '';
		$status = '';

		if($call_type == 2) {
			$type = 'OUTBOUND';
			$status = ($duration > 0) ? 'RECIEVED' : 'REJECTED';
		} else {
			$type = 'INBOUND';
			if($call_type == 1 && $duration > 0) {
				$status = 'ANSWERED';
			} else if($call_type == 1 && $duration <= 0) {
				$status = 'REJECTED';
			} else if($call_type == 3 && $duration <= 0) {
				$status = 'MISSED';
			}
		}

		return array(
			'type'		=>	$type,
			'status'	=>	$status
		);
	}

}