<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\User;
use DB;

class InterfaceController extends Controller
{


    function isvalid_token($access_token){
        if($access_token=="hk6763829"){
            return true;
        }else{
            echo "Access Token Denied";
            return false;
        }
    }

    function isvalid_login(){
        if(isset($_COOKIE['iflexapps'])){
            return true;
        }else{
            return false;
        }
    }

    function islogin_api($access_token){
        
        if($this->isvalid_login()){

            $user_token=$_COOKIE['iflexapps'];
            $user=User::where([['id_token','=',$user_token]]);
                if($user->count()==1){
                    return $user->first()->toJson();
                }else{
                    return "Access Denied";
                }       
        }else{
            return "Not Login";
        }

    }

    function logout_api($redirect){

        
           setcookie("iflexapps","", time()-10,"/"); 
           if($redirect=="iflexmoney"){
               return redirect("https://testing.iflexmoney.com/");
           }
                    

    }
    
    function getuser_api($access_token){
            
            $message='something went wrong';
            $status='500';
            
            $dataarray=array();
            
             $token_detail=DB::select('select * from tokens where token = :access_token', ['access_token' => $access_token]);
             
            if(count($token_detail)==1){
                    $delete_token=DB::select('delete from tokens where token = :access_token', ['access_token' => $access_token]);
                    $timestamp=$token_detail[0]->timestamp;
                    if($timestamp+300>strtotime(date('Y-m-d h:m:s'))){
                        $user_id=$token_detail[0]->user_iflex_id;
                        $user=User::where([['iflex_ID','=',$user_id]]);
                        $user=$user->first();
                        
                        $dataarray['status']="200";
                        $dataarray['user']=$user;
        
                        
                    }else{
                        $dataarray['status']="300";
                        $dataarray['message']="Token Expired";
                    }
                    
                    
                }else{
                    $dataarray['status']="400";
                    $dataarray['message']="Invalid Token";
                }
                
                
                return json_encode($dataarray);
                             

    }
    
    
    function getuserdata($user_id){
        
        $user=User::where([['iflex_ID','=',$user_id]]);
        
        if($user->count()==1){
            $user=$user->first(); 
            $dataarray['status']="200";
            $dataarray['user']=$user;   
        }else{
            $dataarray['status']="400";
            $dataarray['message']="Invalid iFlex ID";
        }

        return json_encode($dataarray);
    }
    

}
