<?php

namespace App\Http\Controllers;

use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\http\Request;
use Illuminate\http\Response;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Input;
use Hash;


use DB;


class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
   
    public function test (Request $request)
    {
        return "good";
    }


    public function PersonSignup (Request $request){ 
	 if(isset($request['email_id']) && !empty($request['email_id']) 
		AND isset($request['username']) && !empty($request['username'])
		AND isset($request['height']) && !empty($request['height'])
		AND isset($request['weight']) && !empty($request['weight'])
		AND isset($request['password']) && !empty($request['password'])
		AND isset($request['name']) && !empty($request['name'])
		AND isset($request['dob']) && !empty($request['dob'])
		AND isset($request['image']) && !empty($request['image'])
		AND isset($request['user_type']) && !empty($request['user_type'])){
    	    try{
    	       $email_id =$request['email_id'];
    	       $password =$request['password'];
    	       $username = $request['username'];
    	       $height = $request['height'];
			   $weight = $request['weight'];
			   $user_type = $request['user_type'];
			   $name = $request['name'];
			   $dob = $request['dob'];
			   $image = $request['image'];
			   $medals = null;
			   $uniquecode=round(microtime(true));
			    
			   
			   $destinationPath='public/pictures';
                $destination = "pictures/";
                $request->file('image');  
                $file1=$request->image;
                $extention1=$request->image->extension();
                $fileName1=round(microtime(true)).".".$extention1;
                $file1->move($destinationPath,$fileName1);
                $add_image = $destination. $fileName1;
                if(!File::exists(public_path($add_image))){
                    return response()->json(['status'=>'failed','error'=>'Upload image again']);
                }
    	       $inserted = DB::insert('insert into user_detail (email_id,password,username, height,weight,image,user_type,name,dob,unique_code) 
			   values (?,?,?,?,?,?,?,?,?,?)',[$email_id,$password,$username,$height,$weight,$add_image,$user_type,$name,$dob,$uniquecode]);
    	       if($inserted){
    	           $users = DB::select("select * from user_detail  where email_id ='$email_id'");
    	           
    	           return response()->json(['status'=>'success','data'=>$users]); 
    	       }
    	       else{
    	           return response()->json(['status'=>'failed','error'=>'server error, Try again']);
    	       } 
    	    }
    	    catch(\Illuminate\Database\QueryException $ex){ 		
	            return response()->json(['status'=>'failed','error'=>'User already exist.']);
    	    }
    	}
    	else{
    	    return response()->json(['status'=>'failed','error'=>'Incomplete Parameters']);
    	}
    }
	public function PersonSignin (Request $request)
    { 
	 if(isset($request['email_id']) && !empty($request['email_id'])  AND
    	isset($request['password']) && !empty($request['password']) AND
		isset($request['user_type']) && !empty($request['user_type'])){
    	    $email_id = $request['email_id'];
    	    $password=$request['password'];
			$type=$request['user_type'];
			try {
				$users = DB::select("select * from user_detail  where email_id ='$email_id' AND password='$password'  AND user_type='$type'");
				if($users){
					return response()->json(['status'=>'success','data'=>$users]);
				}
				else{
					return response()->json(['status'=>'failed','error'=>'Wrong Username or Password']);
				}	
			} 
			catch(\Illuminate\Database\QueryException $ex){ 		
				return response()->json(['status'=>'failed','error'=>'Sql Error']);
			}
		}
		else{
			return response()->json(['status'=>'failed','error'=>'Incomplete Parameters']);
		}
	}
	public function ForgetPasswords (Request $request){
		
	}
	public function viewPersonalInfo (Request $request){
		if(isset($request['user_id']) && !empty($request['user_id']) ){
    	    try{
    	       $user_id =$request['user_id'];
			   $user = DB::select("select * from user_detail where user_id='$user_id' ");		   
                if($user){
					foreach($user as $u){
						$type=$u->user_type;
						if($type==1){
							$Teams = DB::select("select * from team_details where coach_id='$user_id' ");
						}
						else{
							$Teams = DB::select("select * from member_details where athlete_id='$user_id' ");
						}
						$achievement = DB::select("select * from achievement_details where user_id='$user_id' ");
						$u->teams=$Teams;
						$u->achievements=$achievement;												
					}				
                    return response()->json(['status'=>'success','data'=>$user]);
                }
                else{
                    return response()->json(['status'=>'failed','error'=>'No Data found']);
                }	
            } 
            catch(\Illuminate\Database\QueryException $ex){ 		
                return response()->json(['status'=>'failed','error'=>'Sql Error']);
            }
        }else{
            return response()->json(['status'=>'failed','error'=>'Incomplete Parameters']);
        }
	}
	
	public function create_team (Request $request){ 
	 if(isset($request['sport']) && !empty($request['sport']) 
		AND isset($request['team_name']) && !empty($request['team_name'])
		AND isset($request['team_logo']) && !empty($request['team_logo'])
		AND isset($request['coach_id']) && !empty($request['coach_id'])
		){
			
    	    try{
                $sport =$request['sport'];
                $team_name =$request['team_name'];
                $team_logo = $request['team_logo'];
                $coach_id = $request['coach_id'];
                $teams = DB::select("select user_type from user_detail where user_id='$coach_id'");
                if($teams){
                    foreach($teams as $team){
                    $user_type= $team->user_type;
                    if($user_type==1){
                        $destinationPath='public/pictures';
                        $destination = "pictures/";
                        $request->file('team_logo');  
                        $file1=$request->team_logo;
                        $extention1=$request->team_logo->extension();
                        $fileName1=round(microtime(true)).".".$extention1;
                        $file1->move($destinationPath,$fileName1);
                        $add_image = $destination. $fileName1;
                        if(!File::exists(public_path($add_image))){
                            return response()->json(['status'=>'failed','error'=>'Upload image again']);
                            
                        }
                        $inserted = DB::insert('insert into team_details (sport,team_name,team_logo,coach_id) 
                        values (?,?,?,?)',[$sport,$team_name,$add_image,$coach_id]);
                        if($inserted){
                            return response()->json(['status'=>'success']); 
                            
                        }
                        else{
                            return response()->json(['status'=>'failed','error'=>'Try again']);
                            
                        } 
                        
                    }
                    else{
                        return response()->json(['status'=>'failed','error'=>'Only Coach can create team']);
                        
                    }
                        
                    }
                    
                }else{
                    return response()->json(['status'=>'failed','error'=>'No Such User Exist.']);
                    
                }
    	    }
    	    catch(\Illuminate\Database\QueryException $ex){ 		
	            return response()->json(['status'=>'failed','error'=>'SQL Error.']);
    	    }
    	}
    	else{
    	    return response()->json(['status'=>'failed','error'=>'Incomplete Parameters']);
    	}
    }
    public function CreateSchedule (Request $request){
       if(isset($request['event_time']) && !empty($request['event_time'])
		AND isset($request['event_name']) && !empty($request['event_name'])
		AND isset($request['event_date']) && !empty($request['event_date'])		   
		AND isset($request['team_id']) && !empty($request['team_id'])
		AND isset($request['coach_id']) && !empty($request['coach_id'])){
    	try{
    	       $event_time =$request['event_time'];
			   $event_name =$request['event_name'];
			   $event_date =$request['event_date'];
    	       $team_id =$request['team_id'];     
			   $coach_id = $request['coach_id'];  
    	       $inserted = DB::insert('insert into event_details (event_time,event_name,event_date,team_id) 
			   values (?,?,?,?)',[$event_time,$event_name,$event_date,$team_id]);
    	       if($inserted){
    	           return response()->json(['status'=>'success']); 
    	       }
    	       else{
    	           return response()->json(['status'=>'failed','error'=>'Try again']);
    	       }            	
        } 
        catch(\Illuminate\Database\QueryException $ex){ 		
            return response()->json(['status'=>'failed','error'=>'Sql Error']);
        }
		}
    	else{
    	    return response()->json(['status'=>'failed','error'=>'Incomplete Parameters']);
    	}
    }
    
    public function GetTeamSchedule (Request $request){
        if(isset($request['team_id']) && !empty($request['team_id']) ){
    	    try{
    	       $team_id =$request['team_id'];
    	       $data = DB::select("select * from event_details where team_id=$team_id  ");
                if($data){
                    return response()->json(['status'=>'success','data'=>$data]);
                }
                else{
                    return response()->json(['status'=>'failed','error'=>'No data Found']);
                }	
            } 
            catch(\Illuminate\Database\QueryException $ex){ 		
                return response()->json(['status'=>'failed','error'=>'Sql Error']);
            }
        }else{
            return response()->json(['status'=>'failed','error'=>'Incomplete Parameters']);
        }
    }
    
    public function RequestToJoinTeam (Request $request){
        if(isset($request['team_id']) && !empty($request['team_id'])
			AND isset($request['athlete_id']) && !empty($request['athlete_id'])
			AND isset($request['status']) && !empty($request['status'])
			){
			    
			    $team_id =$request['team_id'];
			    $athlete_id =$request['athlete_id'];
			    $status=$request['status'];
			    if($status=='Pending' OR $status=="Declined" OR $status=="Accepted"){
			    try{
			        
			        $users = DB::select("select user_type from user_detail where user_id='$athlete_id'");
			        if($users){
			            foreach($users as $user){
			                $user_type=$user->user_type;
			                if($user_type==2){
			                    $data=DB::select("select * from member_details where athlete_id='$athlete_id' AND team_id='$team_id'");
			                    if($data){
			                        foreach($data as $d){
			                           $stat= $d->status;
			                           if($stat=='Pending'){
			                               if ($status=="Declined"){
			                                   $deleted = DB::delete ("delete from  member_details  where team_id='$team_id' AND athlete_id='$athlete_id' AND status='$stat'");
			                                   return response()->json(['status'=>'success','error'=>'Request has been canceled.']);
			                                   
			                               }else{
			                                   return response()->json(['status'=>'failed','error'=>'Yo have already requested.']);
			                               }
			                               
			                           }else if($stat=='Approved'){
			                               return response()->json(['status'=>'failed','error'=>'You are already member of this team.']);
			                           }else if($stat=='Invite'){
			                               if($status=="Accepted"){
			                                   $inserted = DB::update("update  member_details SET status='$status' where athlete_id='$athlete_id' AND team_id='$team_id'AND status='$stat'");
			                                   if($inserted){
                            	                    return response()->json(['status'=>'success']); 
                                	           }
                                	            else{
                                	                return response()->json(['status'=>'failed','error'=>'server error, Try again']);
                                	            } 
			                               }else if ($status=="Declined"){
			                                   $deleted = DB::delete ("delete from  member_details  where team_id='$team_id' AND athlete_id='$athlete_id' AND status='$stat'");
			                                   return response()->json(['status'=>'success','error'=>'Request has been canceled.']);
			                               }else{
			                                   return response()->json(['status'=>'failed','error'=>'Wrong Action.']);
			                               }
			                               
			                           }else{
			                               return response()->json(['status'=>'failed','error'=>'Wrong Action.']);
			                           }
			                        }
			                    }else{
			                        if($status=="Pending"){
			                            $inserted = DB::insert( 'insert into member_details (team_id,athlete_id,status) values (?,?,?)',[ $team_id,$athlete_id,$status]);
        	                            if($inserted){
                        	               return response()->json(['status'=>'success']); 
                            	       }
                            	       else{
                            	           return response()->json(['status'=>'failed','error'=>'server error, Try again']);
                            	       } 
			                        }else {
			                             return response()->json(['status'=>'failed','error'=>'Wrong Action.']);
			                        }
			                    }
			                }else{
			                    return response()->json(['status'=>'failed','error'=>'Coach can not Join Team']);
			                }
			            }

			        }else{
			            return response()->json(['status'=>'failed','error'=>'No Such User Found']);
			        }
			    }catch(\Illuminate\Database\QueryException $ex){
			        return response()->json(['status'=>'failed','error'=>'Sql Error']);
			        
			    }
		    }else{
		         return response()->json(['status'=>'failed','error'=>'Wrong Possible Action.']);
		    }
        }else{
            return response()->json(['status'=>'failed','error'=>'Incomplete Parameters']);
        }
    }
	public function GetTeams (Request $request){
        if(isset($request['athlete_id']) && !empty($request['athlete_id']) ){
    	    try{
    	       $athlete_id =$request['athlete_id'];
			   $teams = DB::select("select * from team_details ");		   
                if($teams){
					$joinedTeams = DB::select("select * from member_details where athlete_id='$athlete_id' ");
                    return response()->json(['status'=>'success','teams'=>$teams, 'joined_teams'=>$joinedTeams]);
                }
                else{
                    return response()->json(['status'=>'failed','error'=>'No Data found']);
                }	
            } 
            catch(\Illuminate\Database\QueryException $ex){ 		
                return response()->json(['status'=>'failed','error'=>'Sql Error']);
            }
            
        }else{
            return response()->json(['status'=>'failed','error'=>'Incomplete Parameters']);
        }
    }
    
    public function GetALLTeams (Request $request){
       try{
			   $teams = DB::select("select * from team_details ");		   
                if($teams){
				
                    return response()->json(['status'=>'success','teams'=>$teams]);
                }
                else{
                    return response()->json(['status'=>'failed','error'=>'No Data found']);
                }	
            } 
            catch(\Illuminate\Database\QueryException $ex){ 		
                return response()->json(['status'=>'failed','error'=>'Sql Error']);
            }
        
    }
    
   
    	public function ApproveTeamRequest (Request $request){
        if(isset($request['coach_id']) && !empty($request['coach_id']) 
        AND isset($request['athlete_id']) && !empty($request['athlete_id']) 
        AND isset($request['team_id']) && !empty($request['team_id']) 
        AND isset($request['status']) && !empty($request['status']) ){
    	    try{
    	       $athlete_id =$request['athlete_id'];
    	       $coach_id =$request['coach_id'];
    	       $team_id =$request['team_id'];
    	       $status =$request['status'];
    	       if($status=="Approved" OR $status=="Rejected" OR $status=="Invite" ){
    	       $p="Pending";
    	       $teams = DB::select("select coach_id from team_details where team_details_id='$team_id'");
    	       if($teams){
    	           foreach($teams as $team){
    	            $coach=   $team->coach_id;
    	            if($coach==$coach_id){
    	                if($status=="Approved"){
    	                    $inserted = DB::update ("update member_details SET status='$status' where team_id='$team_id' AND athlete_id='$athlete_id' AND status='$p'");
    	                    if($inserted){
                                return response()->json(['status'=>'success']); 
                            }
                            else{
                                return response()->json(['status'=>'failed','error'=>'You can not Approve.']);
                            }
    	                }else if ($status=="Rejected"){
    	                     $deleted = DB::delete ("delete from  member_details  where team_id='$team_id' AND athlete_id='$athlete_id' AND (status='$p' OR status='Invite')");
    	                     if($deleted){
                                return response()->json(['status'=>'success']); 
                            }
                            else{
                                return response()->json(['status'=>'failed','error'=>'You can not Reject.']);
                            }
    	                }else if($status=="Invite"){
    	                    $user_data=DB::select("select * from member_details where team_id='$team_id' AND athlete_id='$athlete_id'");
    	                    if($user_data){
    	                        return response()->json(['status'=>'failed','error'=>'You can not Invite this athelete.']);
    	                    }else{
    	                       $inserted = DB::insert( 'insert into member_details (team_id,athlete_id,status) 
    	                            values (?,?,?)',[ $team_id,$athlete_id,$status]);
                    	       if($inserted){
                    	          
                    	           return response()->json(['status'=>'success']); 
                    	       }
                    	       else{
                    	           return response()->json(['status'=>'failed','error'=>'server error, Try again']);
                    	       } 
    	                    }
    	                }
    	                else{
    	                    return response()->json(['status'=>'failed','error'=>'Wrong Action.']);
    	                }
                             
    	            }else{
    	                return response()->json(['status'=>'failed','error'=>'Coach ID is incorrect.']);
    	            }
    	           }
    	       }else{
    	           return response()->json(['status'=>'failed','error'=>'No such Team Exist.']);
    	       }
    	       }else{
    	           return response()->json(['status'=>'failed','error'=>'Wrong  possible action']);
    	       }
            } 
            catch(\Illuminate\Database\QueryException $ex){ 		
                return response()->json(['status'=>'failed','error'=>'Sql Error']);
            }
            
        }else{
            return response()->json(['status'=>'failed','error'=>'Incomplete Parameters']);
        }
    }
    public function EditProfile (Request $request){ 
	 if(isset($request['email_id']) && !empty($request['email_id']) 
		AND isset($request['username']) && !empty($request['username'])
		AND isset($request['height']) && !empty($request['height'])
		AND isset($request['weight']) && !empty($request['weight'])
		AND isset($request['name']) && !empty($request['name'])
		AND isset($request['dob']) && !empty($request['dob'])){
    	    try{
    	       $email_id =$request['email_id'];
    	       $username = $request['username'];
    	       $height = $request['height'];
			   $weight = $request['weight'];
			   $name = $request['name'];
			   $dob = $request['dob'];
			  
			   
			   if( isset($request['image']) && !empty($request['image'])){
			        $image = $request['image'];
			        $destinationPath='public/pictures';
                    $destination = "pictures/";
                    $request->file('image');  
                    $file1=$request->image;
                    $extention1=$request->image->extension();
                    $fileName1=round(microtime(true)).".".$extention1;
                    $file1->move($destinationPath,$fileName1);
                    $add_image = $destination. $fileName1;
                    if(!File::exists(public_path($add_image))){
                        return response()->json(['status'=>'failed','error'=>'Upload image again']);
                    }
                    $inserted = DB::update("update  user_detail SET username='$username', height='$height' , weight='$weight' , image='$add_image'
                    , name='$name' , dob='$dob' where email_id='$email_id'");
    			   }else{
    			      $inserted = DB::update("update  user_detail SET username='$username' , height='$height' , weight='$weight'
                    , name='$name' , dob='$dob' where email_id='$email_id'");
    			   }
  
        	       if($inserted){
        	           $users = DB::select("select * from user_detail  where email_id ='$email_id'");
        	           
        	           return response()->json(['status'=>'success','data'=>$users]); 
        	       }
        	       else{
        	           return response()->json(['status'=>'failed','error'=>'server error, Try again']);
        	       } 
    	    }
    	    catch(\Illuminate\Database\QueryException $ex){ 		
	            return response()->json(['status'=>'failed','error'=>'SQL Error.']);
    	    }
    	}
    	else{
    	    return response()->json(['status'=>'failed','error'=>'Incomplete Parameters']);
    	}
    }
    
     public function UpdateImage (Request $request){ 
	 if(isset($request['user_id']) && !empty($request['user_id']) 
		AND isset($request['image']) && !empty($request['image'])){
    	    try{
    	       $user_id =$request['user_id'];
    	       $image = $request['image'];
                $destinationPath='public/pictures';
                $destination = "pictures/";
                $request->file('image');  
                $file1=$request->image;
                $extention1=$request->image->extension();
                $fileName1=round(microtime(true)).".".$extention1;
                $file1->move($destinationPath,$fileName1);
                $add_image = $destination. $fileName1;
                if(!File::exists(public_path($add_image))){
                    return response()->json(['status'=>'failed','error'=>'Upload image again']);
                }
                $inserted = DB::update("update  user_detail SET  image='$add_image' where user_id='$user_id'");
    			   
                if($inserted){
                    $users = DB::select("select * from user_detail  where user_id ='$user_id'");
                    return response()->json(['status'=>'success','data'=>$users]); 
                }
                else{
                    return response()->json(['status'=>'failed','error'=>'server error, Try again']);
                } 
    	    }
    	    catch(\Illuminate\Database\QueryException $ex){ 		
	            return response()->json(['status'=>'failed','error'=>'SQL Error.']);
    	    }
    	}
    	else{
    	    return response()->json(['status'=>'failed','error'=>'Incomplete Parameters']);
    	}
    }
    public function ResetPassword (Request $request){ 	
		if(isset($request['email_id']) && !empty($request['email_id']) 
			AND isset($request['password']) && !empty($request['password'])
			AND	isset($request['new_password']) && !empty($request['new_password']) ){
    	    try{
    	       $email_id =$request['email_id'];
			   $password =$request['password']; 
    	       $new_password =$request['new_password']; 
    	       
    	        $updated = DB::update("update  user_detail SET  password='$new_password' where email_id='$email_id' AND password='$password'");
    	       if($updated){
    	           return response()->json(['status'=>'success']); 
    	       }
    	       else{
    	           return response()->json(['status'=>'failed','error'=>'server error, Try again']);
    	       } 
    	    }
    	    catch(\Illuminate\Database\QueryException $ex){ 		
	            return response()->json(['status'=>'failed','error'=>'SQL Error']);
    	    }
    	}
    	else{
    	    return response()->json(['status'=>'failed','error'=>'Incomplete Parameters']);
    	}
    }
    public function Add_achievement (Request $request){ 
	 if(isset($request['user_id']) && !empty($request['user_id']) 
		AND isset($request['achievement']) && !empty($request['achievement']) 	AND isset($request['category']) && !empty($request['category']) ){
    	    try{
    	       $user_id =$request['user_id'];
    	       $achievement =$request['achievement'];
    	       $category =$request['category'];
			   
    	       $inserted = DB::insert('insert into achievement_details (user_id,achievement,category) 
			   values (?,?,?)',[$user_id,$achievement,$category]);
    	       if($inserted){
    	           return response()->json(['status'=>'success']); 
    	       }
    	       else{
    	           return response()->json(['status'=>'failed','error'=>'server error, Try again']);
    	       } 
    	    }
    	    catch(\Illuminate\Database\QueryException $ex){ 		
	            return response()->json(['status'=>'failed','error'=>'User already exist.']);
    	    }
    	}
    	else{
    	    return response()->json(['status'=>'failed','error'=>'Incomplete Parameters']);
    	}
    }
	public function Update_achievement (Request $request){ 
	 if(isset($request['user_id']) && !empty($request['user_id']) 
		AND isset($request['achievement_id']) && !empty($request['achievement_id'])
		AND isset($request['achievement']) && !empty($request['achievement'])
		){
    	    try{
    	       $user_id =$request['user_id'];
    	       $achievement_id =$request['achievement_id'];
			   $achievement =$request['achievement'];
			   
			   $updated = DB::update ("update achievement_details SET achievement='$achievement' where user_id='$user_id' AND achievement_id='$achievement_id'");
			   if($updated){
				   return response()->json(['status'=>'success']); 
			   }
			   else{
				   return response()->json(['status'=>'failed','error'=>'Update again.']);
				}
    	    }
    	    catch(\Illuminate\Database\QueryException $ex){ 		
	            return response()->json(['status'=>'failed','error'=>'SQL Error.']);
    	    }
    	}
    	else{
    	    return response()->json(['status'=>'failed','error'=>'Incomplete Parameters']);
    	}
    }
	
	public function view_achievement (Request $request){ 
	 if(isset($request['user_id']) && !empty($request['user_id']) ){
    	    try{
    	       $user_id =$request['user_id'];
    	       $data = DB::select("select * from achievement_details where user_id='$user_id' ");	   
			   if($data){
				   return response()->json(['status'=>'success','data'=>$data]); 
			   }
			   else{
				   return response()->json(['status'=>'failed','error'=>'Update again.']);
				}
    	    }
    	    catch(\Illuminate\Database\QueryException $ex){ 		
	            return response()->json(['status'=>'failed','error'=>'SQL Error.']);
    	    }
    	}
    	else{
    	    return response()->json(['status'=>'failed','error'=>'Incomplete Parameters']);
    	}
    }
    
    public function DeleteAchievement (Request $request){ 
	 if(isset($request['achievement_id']) && !empty($request['achievement_id']) ){
    	    try{
    	       $achievement_id =$request['achievement_id'];
    	       
    	       $data = DB::delete("DELETE from achievement_details WHERE achievement_id='$achievement_id' ");	   
			   if($data)
			   {
				   return response()->json(['status'=>'success']); 
			   }
			   else
			   {
				   return response()->json(['status'=>'failed','error'=>'Delete again.']);
				}
    	    }
    	    catch(\Illuminate\Database\QueryException $ex){ 		
	            return response()->json(['status'=>'failed','error'=>'SQL Error.']);
    	    }
    	}
    	else{
    	    return response()->json(['status'=>'failed','error'=>'Incomplete Parameters']);
    	}
    }
	
    
}
