<?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 PersonSignup (Request $request)
    { 
		if(isset($request['email_id']) && !empty($request['email_id']) 
       AND isset($request['password']) && !empty($request['password'])	) {
			try 
			{
				$email_id=$request['email_id'];
				$password=$request['password'];
				
				//echo "hello";

				$inserted = DB::insert('insert into user_profie_info (email_id,password) values (?,?)',[$email_id,$password]);
				if($inserted){
					return response()->json(['status'=>'success','email_id'=>$email_id]); 
				}
				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'])  ) {
				$email_id=$request['email_id'];
				$password=$request['password'];
				try 
				{ 
                   
					$users = DB::select("select * from user_profie_info  where email_id ='$email_id' AND password='$password'  ");
					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 get_question_from_db(Request $request)
    { 
    	if(isset($request['chapter_no']) && !empty($request['chapter_no']) AND isset($request['target_lang']) && !empty($request['target_lang']) AND
    	isset($request['chapter_type']) && !empty($request['chapter_type'])){
    		$chapter_no=$request['chapter_no'];
    		$target_lang=$request['target_lang'];
    		$chapter_type=$request['chapter_type'];
    		
    		$lang2 = "Esan";
    		$lang1 = "English";
    		
    	//	return "here".$target_lang;
    	 if($chapter_type==1){
    		//$records;
    		$records=[];
			if (strcmp($target_lang, $lang1)==0)
			{
				$records=DB::select("select * from  esan_lessons where chapter_no ='$chapter_no'");
			}	
			else if (strcmp($target_lang, $lang2)==0){
			    $records=DB::select("select * from  eng_lessons where chapter_no ='$chapter_no'");
			}
			else{
			    return response()->json(['status'=>'failed','error'=>'wrong language']);
			}
			if($records){
				$result=[];
			    foreach ($records as $rec)
			    {
                    $record_id=$rec->record_id;
                    $question_no=$rec->question_no;
                    $question=$rec->question;
                    $question_type=$rec->question_type;
                    $question_sound =$rec->question_sound;
                    $question_image =$rec->question_image;
                    $answer =$rec->answer;
                    $chapter_no =$rec->chapter_no;
                    
                    $choice1=$rec-> choice1;
                    $choice2=$rec->choice2;
                    $choice3=$rec->choice3;
                    $choice4=$rec->choice4;
                    
                    $choice1_image=$rec->choice1_image;
                    $choice2_image=$rec->choice2_image;
                    $choice3_image=$rec->choice3_image;
                    $choice4_image=$rec->choice4_image;
                    
                    $choice1_sound=$rec-> choice1_sound;
                    $choice2_sound=$rec->choice2_sound;
                    $choice3_sound=$rec->choice3_sound;
                    $choice4_sound=$rec->choice4_sound;
                        
                $choices = array($choice1, $choice2,$choice3,$choice4);
				$images=array( $choice1_image, $choice2_image, $choice3_image, $choice4_image);
				 $sounds = array($choice1_sound, $choice2_sound,$choice3_sound,$choice4_sound);
				
				$data=["record_id"=>$record_id,"question_no"=> $question_no,"question"=>$question,"question_type"=>$question_type, 
				       "question_sound"=>$question_sound,"question_image"=>$question_image,"answer"=>$answer , "chapter_no"=>$chapter_no,
				       "choices"=>$choices,"images"=>$images, "sounds"=>$sounds ];
			    array_push($result , $data);
			        
			    }
				     return response()->json(['status'=>'success','data'=>$result]);
				}
			else 
			{
			    return response()->json(['status'=>'failed','error'=>'chapter is not available']);
			    
			}
    	 }else{
    	     $records=DB::select("select * from  sentence_lesson where chapter_no ='$chapter_no'");
    	     if($records){
    	         	$result=[];
    	         foreach ($records as $rec){
    	            $record_id=$rec->record_id;
    	            $chapter_no =$rec->chapter_no;
                    $question_no=$rec->question_no;
                    $sound =$rec->sound;
                    $question_type=$rec->question_type;
                    $question_image=$rec->question_image;
                    if (strcmp($target_lang, $lang1)==0){
                        $question =$rec-> esan_sen;
                        $answer =$rec->eng_sen;
                    }else if (strcmp($target_lang, $lang2)==0){
                        $question =$rec-> eng_sen;
                        $answer =$rec->esan_sen;
                    }else{
                         return response()->json(['status'=>'failed','error'=>'wrong language']);
                    }
                    $tokens=explode(" ", $answer);
                    shuffle($tokens);
                    $images=array("NA","NA","NA","NA");
				    $sounds = array( "NA","NA","NA","NA");
                    $data=["record_id"=>$record_id,"question_no"=> $question_no,"question"=>$question,"question_type"=>$question_type, 
				       "question_sound"=>$sound,"question_image"=> $question_image,"answer"=>$answer , "chapter_no"=>$chapter_no,"choices"=>$tokens,"images"=>$images, "sounds"=> $sounds ];
                    array_push($result , $data);
    	         }
    	          return response()->json(['status'=>'success','data'=>$result]);
    	     }else{
    	          return response()->json(['status'=>'failed','error'=>'chapter is not available']);
    	     }
    	 }
    	}
		else 
			{
				return response()->json(['status'=>'failed','error'=>'Incomplete Parameters']);
			}

    }
    public function set_score(Request $request)
    { 
	    if(isset($request['email_id']) && !empty($request['email_id'])
	    AND isset($request['curr_chapter_no']) && !empty($request['curr_chapter_no'])
	    	AND isset($request['current_target_lang']) && !empty($request['current_target_lang'])) {
    	try
    		{    
    		$email_id =	$request['email_id'];
	    	$curr_chapter_no = $request['curr_chapter_no'];
	    	$current_target_lang= $request['current_target_lang'];
	    	$lang1 = "Esan";
    		$lang2 = "English";
    		//$records;
			if (strcmp($current_target_lang, $lang1)==0){
	    	    $inserted=DB::update ('update  user_profie_info SET  eng_cur_chapter_no=? where email_id=?' ,[$curr_chapter_no,$email_id] );
	    	    if($inserted){
					return response()->json(['status'=>'success']); 
				}
				else{
					return response()->json(['status'=>'failed','error'=>'Try again']);
				}
	    	}else if (strcmp($current_target_lang, $lang2)==0){
	    	    $inserted=DB::update ('update  user_profie_info SET  esan_cur_chap_no=? where email_id=?' ,[$curr_chapter_no,$email_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 get_table_of_content(Request $request){
        $current_target_lang= $request['target_lang'];
       if( isset($request['target_lang']) && !empty($request['target_lang'])){
          $lang1 = "Esan";
          $lang2 = "English";
            if (strcmp($current_target_lang, $lang1)==0){
            	$data = DB::select("select * from content_eng ");
				if($data)
				{
            		return response()->json(['status'=>'success','data'=>$data]);
					
				}else{
				    return response()->json(['status'=>'failed','error'=>'No content found']);
				}
            }else if (strcmp($current_target_lang, $lang2)==0){
                $data = DB::select("select * from content_esan ");
				if($data)
				{
            		return response()->json(['status'=>'success','data'=>$data]);
					
				}else{
				    return response()->json(['status'=>'failed','error'=>'No content found']);
				}
            }else{
    			return response()->json(['status'=>'failed','error'=>'Wrong Language']);

    		}
       }
       else{
    		return response()->json(['status'=>'failed','error'=>'Incomplete Parameters']);
       }
       
   }

}
