<?php
namespace App\Http\Controllers\v2019;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\JsonResponse;
use App\Http\Controllers\Auth\authentication;
use App\Http\Controllers\Auth\User;

class HomeController extends Controller
{ 
	public $appType = 'SM' ;
    public function __construct(Request $request) {
        if($request->has('debug') && $request->input('debug') == true) {
            $this->debug = true;			
        }
		$this->appType = 'SM';
		if($request->has('appType') && $request->input('appType') == true) {
            $this->appType = $request->input('appType');		
        } elseif($request->has('filters')){
			 $filter = $request->input('filters');
			 if(!empty($filter['app_type'])){
					$this->appType = 'CM';
			 }
		} elseif($request->has('app_type') && $request->input('app_type') == true) {
            $this->appType = $request->input('app_type');		
        }
    }

	public function getPromotion(Request $request)
    {   
		if($this->appType == 'CM'){
			$code = 1001;
			$message = "Success";
			return new JsonResponse(array("status"=>array("code" => $code, "message" => $message), "data"=> [],"user_type"=>"AD"));
		}
		
		$resourceType = "app";$multiple="N";
		if($request->has('send_all'))
			$multiple = $request->input('send_all');
			
		$result = array();	
		if($request->has('resourceType'))
			$resourceType = strtolower($request->input('resourceType'));
		
		$showAtPlace='home';
		if($request->has('showAtPlace'))
			$showAtPlace = strtolower($request->input('showAtPlace'));
				
		$dt = date("Y-m-d");
		if($request->has('debug') && $request->input('debug') == 1){
			DB::enableQueryLog(); 			
		}
		if($multiple == "N"){
			
					
    		$dbresult = DB::table('tbl_web_app_ads')
    			//->where('f_platform', '=', $resourceType ) 
    			->where('showAtPlace', '=', $showAtPlace ) 
    			->where('end_date', '>=', $dt ) 
    			->orderBy('id', 'desc')
    			->first();
				
			
				
		} else {
		    $dbresult = DB::table('tbl_web_app_ads')
    			->where('f_platform', '=', $resourceType ) 
    			->where('showAtPlace', '=', $showAtPlace ) 
    			->where('end_date', '>=', $dt ) 
    			->orderBy('id', 'desc')
    			->get();
		}
			
		if($request->has('debug') && $request->input('debug') == 1){				
				dd(DB::getQueryLog());
		}
		//special case - to show manage pref screen to post login users
		if (count($dbresult) > 0){
		    if($multiple == "N"){
    			if( $dbresult['id'] == 1 ){
    				if ($resourceType=='app' && $request->has('accessToken') && $request->input('accessToken')!='') {
    					$result[] = array(
    						'type'	=> $dbresult['type'],
    						'adImageVideoUrl'	=> ($dbresult['type'] == "I" || $dbresult['type'] == "G") ? "https://www.steelmint.com/images/web_app_ads/" . $dbresult['f_image']:$dbresult['f_video'],
    						'ad_action'	=> $dbresult['ad_action'],
    						'url_screen_name' => $dbresult['ad_action']=='open_screen'?$dbresult['f_screen']:$dbresult['f_url'],
    						'thumbnailImage'=> "https://www.steelmint.com/images/web_app_ads/" . $dbresult['f_image']
    					);
    					$code = 1001;
    					$message = "Success";
    					return new JsonResponse(array("status"=>array("code" => $code, "message" => $message), "data"=> $result,"user_type"=>"AD"));
    				}
    			}
		    }else{
		        if ($resourceType=='app' && $request->has('accessToken') && $request->input('accessToken')!='') {
		            $user_id=0;$user_type = "login";
		            $token = $request->input('accessToken');
			    	$auth = new authentication();
			    	$user_id = $auth->authToken($token,$this->appType);
			    	if ($user_id > 0) {
        			   $user = new User($user_id, $this->appType);
        			   $isAccessible = $user->isAccessible($resourceType);
        			   if ($isAccessible==-1) {
        				$user_type = "paid";
        			   }else{
        				 $user_type = "upgrade";
        			   }
        			} else{
        			    $user_type = "login";
        			}
        			if(count($dbresult)>0){
        			    $user_type = "AD";
        			}
		            foreach($dbresult as $dbr){
    					$result[] = array(
    						'type'	=> $dbr['type'],
    						'adImageVideoUrl'	=> ($dbr['type'] == "I" || $dbr['type'] == "G") ? "https://www.steelmint.com/images/web_app_ads/" . $dbr['f_image']:$dbr['f_video'],
    						'ad_action'	=> $dbr['ad_action'],
    						'url_screen_name' => $dbr['ad_action']=='open_screen'?$dbr['f_screen']:$dbr['f_url'],
    						'thumbnailImage'=> "https://www.steelmint.com/images/web_app_ads/" . $dbr['f_image']
    					);
		            }
		            
    					$code = 1001;
    					$message = "Success";
    					return new JsonResponse(array("status"=>array("code" => $code, "message" => $message), "data"=> $result,"user_type"=>$user_type));
    				} else {
    				    $user_type = "login";
    				    if(count($dbresult)>0){
        			    $user_type = "AD";
        			}
		            foreach($dbresult as $dbr){
    					$result[] = array(
    						'type'	=> $dbr['type'],
    						'adImageVideoUrl'	=> ($dbr['type'] == "I" || $dbr['type'] == "G") ? "https://www.steelmint.com/images/web_app_ads/" . $dbr['f_image']:$dbr['f_video'],
    						'ad_action'	=> $dbr['ad_action'],
    						'url_screen_name' => $dbr['ad_action']=='open_screen'?$dbr['f_screen']:$dbr['f_url'],
    						'thumbnailImage'=> "https://www.steelmint.com/images/web_app_ads/" . $dbr['f_image']
    					);
		            }
		            
    					$code = 1001;
    					$message = "Success";
    					return new JsonResponse(array("status"=>array("code" => $code, "message" => $message), "data"=> $result,"user_type"=>$user_type));
    				}
		    }
		} else {
			$code = 1001;
			$message = "Success";
			return new JsonResponse(array("status"=>array("code" => $code, "message" => $message), "data"=> $result,"user_type"=>"login"));
		}

		/*if (count($dbresult) > 0 && $dbresult['id'] > 1){
			$result = array(
				'type'	=> 'AD',
				'ad_image'	=> "https://www.steelmint.com/images/web_app_ads/" . $dbresult['f_image'],
				'ad_action'	=> $dbresult['ad_action'],
				'url_screen_name' => $dbresult['ad_action']=='open_screen'?$dbresult['f_screen']:$dbresult['f_url']
			);
			$code = 1001;
			$message = "Success";
			return new JsonResponse(array("status"=>array("code" => $code, "message" => $message), "data"=> $result));
		} else {
			$user_id = 0;
			$userType = 'login';
			
			if ($resourceType=='app' && $request->has('accessToken') && $request->input('accessToken')!='') {
				$token = $request->input('accessToken');
				$auth = new authentication();
				$user_id = $auth->authToken($token,$this->appType);
			}elseif ($resourceType=='web' && $request->has('memid') && $request->input('memid')!=''){
				$memid = $request->input('memid');
				$auth = new authentication();
				$user_id = $auth->authUserId($memid);//for web user
			} 
			
			if ($user_id > 0) {
			   $user = new User($user_id, $this->appType);
			   $isAccessible = $user->isAccessible($resourceType);
			   if ($isAccessible==-1) {
				$result['type'] = "paid";
			   }else{
				 $result['type'] = "upgrade";
			   }
			}else{
				$result['type'] = $userType;
				//$result['type'] = 'default';
			}
			//print_r($dbresult);
			$code = 1001;
			$message = "Success";
			return new JsonResponse(array("status"=>array("code" => $code, "message" => $message), "data"=> $result));
		}*/
    }
	
	public function getPromotion_5_12_2020(Request $request)
    {      
		$resourceType = "app";
		
		$result = array();	
		if($request->has('resourceType'))
			$resourceType = strtolower($request->input('resourceType'));
		
		$showAtPlace='home';
		if($request->has('showAtPlace'))
			$showAtPlace = strtolower($request->input('showAtPlace'));
				
		$dt = date("Y-m-d");
		$dbresult = DB::table('tbl_web_app_ads')
			->where('f_platform', '=', $resourceType ) 
			->where('showAtPlace', '=', $showAtPlace ) 
			->where('end_date', '>=', $dt ) 
			->orderBy('id', 'desc')
			->first();

		//special case - to show manage pref screen to post login users
		if (count($dbresult) > 0){
			if( $dbresult['id'] == 1 ){
				if ($resourceType=='app' && $request->has('accessToken') && $request->input('accessToken')!='') {
					$result = array(
						'type'	=> 'AD',
						'ad_image'	=> "https://www.steelmint.com/images/web_app_ads/" . $dbresult['f_image'],
						'ad_action'	=> $dbresult['ad_action'],
						'url_screen_name' => $dbresult['ad_action']=='open_screen'?$dbresult['f_screen']:$dbresult['f_url']
					);
					$code = 1001;
					$message = "Success";
					return new JsonResponse(array("status"=>array("code" => $code, "message" => $message), "data"=> $result));
				}
			}
		}

		if (count($dbresult) > 0 && $dbresult['id'] > 1){
			$result = array(
				'type'	=> 'AD',
				'ad_image'	=> "https://www.steelmint.com/images/web_app_ads/" . $dbresult['f_image'],
				'ad_action'	=> $dbresult['ad_action'],
				'url_screen_name' => $dbresult['ad_action']=='open_screen'?$dbresult['f_screen']:$dbresult['f_url']
			);
			$code = 1001;
			$message = "Success";
			return new JsonResponse(array("status"=>array("code" => $code, "message" => $message), "data"=> $result));
		} else {
			$user_id = 0;
			$userType = 'login';
			
			if ($resourceType=='app' && $request->has('accessToken') && $request->input('accessToken')!='') {
				$token = $request->input('accessToken');
				$auth = new authentication();
				$user_id = $auth->authToken($token,$this->appType);
			}elseif ($resourceType=='web' && $request->has('memid') && $request->input('memid')!=''){
				$memid = $request->input('memid');
				$auth = new authentication();
				$user_id = $auth->authUserId($memid);//for web user
			} 
			
			if ($user_id > 0) {
			   $user = new User($user_id, $this->appType);
			   $isAccessible = $user->isAccessible($resourceType);
			   if ($isAccessible==-1) {
				$result['type'] = "paid";
			   }else{
				 $result['type'] = "upgrade";
			   }
			}else{
				$result['type'] = $userType;
				//$result['type'] = 'default';
			}
			//print_r($dbresult);
			$code = 1001;
			$message = "Success";
			return new JsonResponse(array("status"=>array("code" => $code, "message" => $message), "data"=> $result));
		}	
    }  
	
	public function getPhotoStories(Request $request)
    {      
		$result = array();$offset=0;
		    if($request->has('resourceType'))
		       $resourceType = strtolower($request->input('resourceType'));
		    if($request->has('pageValue')){
			   $pageValue = $request->input('pageValue');
			   $offset = ($pageValue-1)* DEFAULT_LIMIT;
			}
			
			$result = \apiQuery::getPhotoRowSet('','post',15, $offset, $this->appType);
			$code = 1001;
			$message = "Success";

	   return new JsonResponse(array("status"=>array("code" => $code, "message" => $message), "data"=> $result));
	} 
	
	public function updatePhotoStoryViews(Request $request){
		if($request->has('views')){
			$views = $request->input("views");
			foreach($views as $k=>$v){
				$result = \apiQuery::updateViews($v);
			}
			if($result){
				$code = 1001;
				$message = "Success";
			} else {
				$code = 9999;
				$message = "Something went wrong.";
			}
		} else {
			$code = 2001;
			$message = "Invalid input";
		}
		return new JsonResponse(array("status"=>array("code" => $code, "message" => $message)));
	}
	
	public function upcomingEvents(Request $request)
    {      
	   $result = array();
	   include_once "/home/steelmin/public_html/api/storage/v2019/event_pre.php";
	   if (! empty($result) && is_array($result)) {
		   $result = \apiQuery::applyPublicEventImagesToNestedData($result);
	   }
		if ($request->has('accessToken') && $request->input('accessToken')!='') {
           // $auth = new authentication();
           $token = $request->input('accessToken');
           if(empty($token)){
				$code = 2013;
				$message = "User not found.";
			}else{
				$code = 1001;
				$message = "Success";
			}
        }else{
			$code = 1001;
			$message = "Success";
		}
	   return new JsonResponse(array("status"=>array("code" => $code, "message" => $message), "data"=> $result));
    } 
	
}
