<?php

/*
	1. Get all requests for a pilot
	2. Get all freinds 
	3. Get all blocked Pilots
	4. Get all accept request
	5. Reject Request
	6. report profile

*/
class ICSFriends extends CodonData {
		
	public static function check_self_acc($profile_id, $user_id) {
		if($profile_id != $user_id) {
			return 1;
		} else {
			return 0;
		}
	}
	
	public static function profile_check_self_acc($profile_id, $user_id) {
		if($profile_id == $user_id) {
			return 1;
		} else {
			return 0;
		}
	}
	
	public static function getRequest($id) {
		$sql = 'SELECT * FROM `icrewsystems_friends` 
							WHERE `id` = '.$my_id.'';
				
		 return DB::get_results($sql);
		 
	}
	
	public static function checkPending($pilotid) {
		$sql = 'SELECT * FROM `icrewsystems_friends` 
							WHERE `friend_id` = '.$pilotid.'';
				
		 return DB::get_results($sql);
		 
	}
	
	public static function send_request($self_id, $friend_id) {
		$sql = "INSERT INTO `icrewsystems_friends` (`self_id`, `friend_id`, `date`, `blocked`, `req_status`) 
		VALUES ('".$self_id."', '".$friend_id."', CURRENT_TIMESTAMP, '0', '0')";
				
		 return DB::get_results($sql);
		 
	}
	
	public static function getAllRequests($my_id) {
		$sql = 'SELECT * FROM `icrewsystems_friends` 
							WHERE `friend_id` = '.$my_id.' AND `req_status` = 0';
				
		 return DB::get_results($sql);
		 
	}
	
	public static function getAll($my_id) {
		$sql = 'SELECT * FROM `icrewsystems_friends` 
							WHERE `self_id` = '.$my_id.'';
				
		 return DB::get_results($sql);
		 
	}
	
	public static function checkFriends($friend_id, $my_id) {
		return DB::get_row('SELECT * FROM `icrewsystems_friends` 
							WHERE `friend_id` =' . $friend_id .' AND `self_id` = '.$my_id.'');
	}
	
	public static function AcceptRequest($friend_id, $my_id) {
		$sql = "UPDATE `icrewsystems_friends` 
				SET `req_status` = '1'
				WHERE `friend_id` =" . $friend_id ." AND `self_id` = ".$my_id."";
				
		 return DB::get_results($sql);
	}
	
	public static function RejectRequest($friend_id, $my_id) {
		$sql = "UPDATE `icrewsystems_friends` 
				SET `req_status` = '2'
				WHERE `friend_id` =" . $friend_id ." AND `self_id` = ".$my_id."";
				
		 return DB::get_results($sql);
	}
	
	public static function send_new_mail($who_to, $who_from, $subject, $newmessage) {
  		
  		$thread_id = time();
  		
        $sql="INSERT INTO ".TABLE_PREFIX."airmail (`who_to`, `who_from`, `date`, `subject`, `message`, `notam`, `thread_id`)
			VALUES ('$who_to', '$who_from', NOW(), '$subject', '$newmessage', '0', '$thread_id')";

        DB::query($sql);

        $code = DB::errno();
        if ($code != 0){
            $message = DB::error();
            throw new Exception($message, $code);
        }

        return;
    }
}
