<?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 UploadImages extends BaseController
{
    // Target Language is Esan Here
    public function upload_eng_lesson (Request $request){
        
        $destinationPath='public/images_learning_app_Eng';
        $soundDestinationPath='public/sounds_learning_app_Eng';
        $destination = "images_learning_app_Eng/";
        $soundDestination = "sounds_learning_app_Eng/";
        
        $request->file('photo1');
        $file1=$request->photo1;
        $extention1=$request->photo1->extension();
        $fileName1=round(microtime(true)).".".$extention1;
        $file1->move($destinationPath,$fileName1);
      //  $photo1=$fileName1;
        $choice1_image = $destination. $fileName1;
        
        
        $request->file('photo2');
        $file2=$request->photo2;
        $extention2=$request->photo2->extension();
        $fileName2=round(microtime(true)).".".$extention2;
        $file2->move($destinationPath,$fileName2);
        $photo2=$fileName2;
        $choice2_image = $destination. $photo2;
        
        
        $request->file('photo3');
        $file3=$request->photo3;
        $extention3=$request->photo3->extension();
        $fileName3=round(microtime(true)).".".$extention3;
        $file3->move($destinationPath,$fileName3);
        $photo3=$fileName3;
        $choice3_image = $destination. $photo3;
        
        $request->file('photo4');
        $file4=$request->photo1;
        $extention4=$request->photo4->extension();
        $fileName4=round(microtime(true)).".".$extention4;
        $file4->move($destinationPath,$fileName4);
        $photo4=$fileName4;
        $choice4_image = $destination. $photo4;
        
        
        $request->file('sound1');
        $file5=$request->sound1;
        $extention5=$request->sound1->extension();
        $fileName5=round(microtime(true)).".".$extention5;
        $file5->move($soundDestinationPath,$fileName5);
        $sound1=$fileName5;
      	$choice1_sound = $soundDestination. $sound1;

   
   
        $request->file('sound2');
        $file6=$request->sound2;
        $extention6=$request->sound2->extension();
        $fileName6=round(microtime(true)).".".$extention6;
        $file6->move($soundDestinationPath,$fileName6);
        $sound2=$fileName6; 
        $choice2_sound = $soundDestination. $sound2;
        
        
        $request->file('sound3');
        $file7=$request->sound3;
        $extention7=$request->sound3->extension();
        $fileName7=round(microtime(true)).".".$extention7;
        $file7->move($soundDestinationPath,$fileName7);
        $sound3=$fileName7;
        $choice3_sound = $soundDestination. $sound3;
        
        
        $request->file('sound4');
        $file8=$request->sound4;
        $extention8=$request->sound4->extension();
        $fileName8=round(microtime(true)).".".$extention8;
        $file8->move($soundDestinationPath,$fileName8);
        $sound4=$fileName8;
        $choice4_sound = $soundDestination. $sound4;
    
    	$chapter_no = $request['chapter'];
        $question_no = $request['questionNo'];
        $question = $request['question'];
        $choice1 = $request['choice1'];
        $choice2 = $request['choice2'];
        $choice3 = $request['choice3'];
        $choice4 = $request['choice4'];
        $answer= $request['answer'];
        $question_type=$request['question_type'];
        $t_lang = $request['t_lang'];
        
	
	try{
	$scores=DB::insert ('insert into eng_lessons ( 	choice1_image, 	choice2_image, 	choice3_image, 	choice4_image, choice1_sound, choice2_sound, choice3_sound, choice4_sound,
	                                                chapter_no, question_no, question, choice1, choice2 ,   choice3, choice4, answer,question_type) 
	                                                values (?, ?, ?, ?,?, ?, ?, ?,?, ?, ?, ?,?, ?, ?, ?,?)',[$choice1_image ,$choice2_image ,$choice3_image,$choice4_image,
	                                                $choice1_sound ,$choice2_sound ,$choice3_sound,$choice4_sound,	$chapter_no,  $question_no, $question, 
	                                                $choice1,  $choice2 , $choice3, $choice4,  $answer,$question_type] );
	return response()->json(['status'=>'success']);
	}catch(\Illuminate\Database\QueryException $ex){ 		
		return response()->json(['status'=>'failed','error'=>'Sql Error']);
		}
		
		
		
    }
    
     // Target Language is English Here

    public function upload_esan_lesson(Request $request){
    $request->file('photo1');
    $request->file('photo2');
    $request->file('photo3');
    $request->file('photo4');
    $request->file('sound1');
    
    
       
	$destinationPath='public/images_learning_app_Eng';
	$soundDestinationPath='public/sounds_learning_app_Eng';
	
	$file1=$request->photo1;
    $file2=$request->photo2;
    $file3=$request->photo3;
    $file4=$request->photo4;
    $file5=$request->sound1;
   
	$extention1=$request->photo1->extension();
	$extention2=$request->photo2->extension();
	$extention3=$request->photo3->extension();
	$extention4=$request->photo4->extension();
	$extention5=$request->sound1->extension();

	
	$fileName1=round(microtime(true)).".".$extention1;
	$fileName2=round(microtime(true)).".".$extention2;
	$fileName3=round(microtime(true)).".".$extention3;
	$fileName4=round(microtime(true)).".".$extention4;
	$fileName5=round(microtime(true)).".".$extention5;


	$file1->move($destinationPath,$fileName1);
	$file2->move($destinationPath,$fileName2);
	$file3->move($destinationPath,$fileName3);
	$file4->move($destinationPath,$fileName4);
	$file5->move($soundDestinationPath,$fileName5);

	$photo1=$fileName1;
	$photo2=$fileName2;
	$photo3=$fileName3;
	$photo4=$fileName4;
	$sound1=$fileName5;
	
	$chapter_no = $request['chapter'];
    $question_no = $request['questionNo'];
    $question = $request['question'];
    $choice1 = $request['choice1'];
    $choice2 = $request['choice2'];
    $choice3 = $request['choice3'];
    $choice4 = $request['choice4'];
    $answer= $request['answer'];
    $question_type=$request['question_type'];
	
	
	$destination = "images_learning_app_Eng/";
	$soundDestination = "sounds_learning_app_Eng/";
	
    $choice1_image = $destination. $photo1;
    $choice2_image = $destination. $photo2;
	$choice3_image = $destination. $photo3;
	$choice4_image = $destination. $photo4;
	$choice1_sound = $soundDestination. $sound1;
	
	try{
	$scores=DB::insert ('insert into esan_lessons ( choice1_image, 	choice2_image, 	choice3_image, 	choice4_image, question_sound, 
	                                                chapter_no, question_no, question, choice1, choice2 ,   choice3, choice4, answer,question_type) 
	                                                values (?, ?, ?, ?,?, ?, ?, ?,?, ?, ?, ?,?,?)',[$choice1_image ,$choice2_image ,$choice3_image,$choice4_image,
	                                                $choice1_sound, $chapter_no,  $question_no, $question, 
	                                                $choice1,  $choice2 , $choice3, $choice4,  $answer,  $question_type] );
	  $data=DB::select("select * from  esan_lessons where chapter_no ='$chapter_no'");
	return response()->json(['status'=>'success','data'=>$data]);
	}catch(\Illuminate\Database\QueryException $ex){ 		
		return response()->json(['status'=>'failed','error'=>'Sql Error']);
		}


    }
    
    function set_target_language(Request $request){
        	if(isset($request['email_id']) && !empty($request['email_id']) AND 
        	isset($request['current_target_lang']) && !empty($request['current_target_lang'])){
        	    try{
        	        $current_target_lang =$request['current_target_lang'];
        	       $email_id =$request['email_id'];
        	        
        	   	$inserted = DB::update('update user_profie_info SET current_target_lang = ?  where  email_id = ?', [$current_target_lang, $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 Parameter']);   
			}
    }
}