<?php

namespace App\Http\Controllers\v2019;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Controllers\Auth\User ;
use App\Http\Controllers\Auth\authentication;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\JsonResponse;
use App\Http\Controllers\Action\Common;
//stepLogic
use App\Http\Controllers\Action\StplCommonController;
use Validator;


class StatisticController extends Controller
{
    /**
     * Show the profile for the given user.
     *
     * @param  int  $id
     * @return Response
     */
   /* public function index(){
     //echo "here";
      $obj = new authentication;
      echo $obj->authUserId(12);
    }
*/
    

    public function getStatistics(Request $request){
          $rule = [
                'resourceType' => 'required|in:Web,web',
                'requireData' => 'required|in:datatype',
                'requiredFor' => 'required|in:statistics,price'
              ];
            $check = $this->checkInputisValid($rule,$request->all());
           if($check){
            return response()->json($check);
           }else{

      		$requiredFor = trim($request->requiredFor);
      		$objCommon = new StplCommonController;
      		$data = $objCommon->getDetaTypeName($requiredFor);

      		$datatypes = array();
      		$datatypes['datatypes'] = $data;
      		$status = 1001;
      		$message= "success";            
         $getresponce = $this->innerResponceHander($datatypes,$status,$message);
         if($request->debugMode=='true'){
         	$table = array('tbl_stats_datatype_header_list');
         	$requreParam = array(
                   'resourceType' => 'required|string|web',
                   'requireData' => 'required|string|datatype',
                   'requiredFor' => 'required|string|statistics,price',
         		);
		    $getresponce['debugMode']['usedtable'] = $table;
		    $getresponce['debugMode']['argumentDetail'] = $requreParam;
         }			 
		  return response()->json($getresponce);
     }
    }

    public function getFilterLeftMain(Request $request){
      $rule = [
          'resourceType' => 'required|in:Web,web',
          'requireData' => 'required|in:regionCommodity',
          'requiredFor' => 'required|in:statistics,price',
          'source' => 'required|in:stats_graphs',
          'dataType' => 'required'
        ];
        $check = $this->checkInputisValid($rule,$request->all());
         if($check){
          return response()->json($check);
         }else{               
        	
    		$datatype = trim($request->dataType);

	    	$objCommon = new StplCommonController;
        $checkRecordExist = $objCommon->checkDatatypeExist($datatype);

        if($checkRecordExist!='true'){
          $status = array(
              'status' => 2001,
              'message' => 'dataType not Found!'
          	);
           return response()->json($status);
        }

		 $data = $objCommon->getCommAndRegion($datatype);
	     $status = 1001;
	     $message= "success";
		  $getresponce = $this->innerResponceHander($data,$status,$message);
	     if($request->debugMode=='true'){
	     	 $table = array('tbl_stats_datatype_header_list','tbl_stats_left_toolbox_landing');
	     	 $requreParam = array(
                   'resourceType' => 'required|string|web',
                   'requireData' => 'required|string|regionCommodity',
                   'requiredFor' => 'required|string|statistics,price',
                   'source' => 'required|string|stats_graphs',
                   'dataType' => 'required|string',
         		);
		    $getresponce['debugMode']['usedtable'] = $table;
		    $getresponce['debugMode']['argumentDetail'] = $requreParam;
	     }			 
		   return response()->json($getresponce); 
     }	
    }

    public function getGraphs(Request $request){
    	  $rule = [
	            'resourceType' => 'required|in:Web,web',
	            'dataType' => 'required|string|max:20',
	            'commodity' => 'required|string|max:20',
	            'region' => 'required|string|max:20',
	            'fromDate' => 'required|string|max:20',
	            'toDate' => 'required|string|max:20',
	            'periodFormat' => 'required|string|max:20',
	            'memid' => 'required|string|max:20',
	            'columnNameOfRegion' => 'required|string|max:20',
	            'columnNameOfCommodity' => 'required|string|max:20',
	        ];
          $check = $this->checkInputisValid($rule,$request->all());
        if($check){
          return response()->json($check);
         }else{
          $objCommon = new StplCommonController;
          $title = trim(ucfirst($request->dataType)).'-'.trim(ucfirst($request->commodity)).'|'.trim(ucfirst($request->region));
          $data['title'] = $title;
          $data['graphValues'] = $objCommon->getGraphData($request->all());
          $data['nextChildren'] = $objCommon->getHeaderNameAndColumns($request->all());
          $check_portfolio = $objCommon->checkPortfolio($request->all(),"parent");
          if($check_portfolio == "false"){
            $data['isAddedToPortfolio'] = "N";
          } else {
            $data['isAddedToPortfolio'] = 'Y';
            $data['addedPortfolioId'] = $check_portfolio;
          }
           if($data['graphValues']=='false' || $data['graphValues']==null ||  $data['nextChildren']=='false' ){
	          $status = array(
	              'status' => 2001,
	              'message' => 'Invalid input.'
	          	);
	           return response()->json($status);
	        }


           $status = 1001;
           $message = 'success';
           
                      
           $getresponce = $this->innerResponceHander($data,$status,$message);
           if($request->debugMode=='true'){
               $table = array('dataType'=> $request->dataType,'tableUsedForHeader' => 'tbl_stats_datatype_header_list');

               $requreParam = array(
                    'resourceType' => 'required|string|web',
                    'dataType' => 'required|string|max:20',
                    'commodity' => 'required|string|max:20',
                    'region' => 'required|string|max:20',
                    'fromDate' => 'required|string|max:20',
                    'toDate' => 'required|string|max:20',
                    'periodFormat' => 'required|string|max:20',
                    'memid' => 'required|string|max:20',
                    'columnNameOfRegion' => 'required|string|max:20',
                    'columnNameOfCommodity' => 'required|string|max:20'
                  );
              $getresponce['debugMode']['usedtable'] = $table;
              $getresponce['debugMode']['argumentDetail'] = $requreParam;
           }       
            return response()->json($getresponce);       
         }

}


	public function getGraphs2ndLevel(Request $request){
		 $rule = [
	            'resourceType' => 'required|in:Web,web',
	            'dataType' => 'required|string|max:20',
	            'commodity' => 'required|string|max:20',
	            'region' => 'required|string|max:20',
	            'fromDate' => 'required|string|max:20',
	            'toDate' => 'required|string|max:20',
	            'periodFormat' => 'required|string|max:20',
	            'memid' => 'required|string|max:20',
	            'columnNameOfRegion' => 'required|string|max:20',
	            'columnNameOfCommodity' => 'required|string|max:20',
	            'headerColumn' => 'required|string|max:20',
	        ];
          $check = $this->checkInputisValid($rule,$request->all());
        if($check){
          return response()->json($check);
       }else{
             $objCommon = new StplCommonController;
	           $title = trim(ucfirst($request->dataType)).'-'.trim(ucfirst($request->commodity)).'|'.trim(ucfirst($request->region));
		         $data['childrenTitle'] = $title;
             $data['childrenGraphPoints'] = $objCommon->getChildrenGraphPoint($request->all());
             $check_portfolio = $objCommon->checkPortfolio($request->all(),"child");
            if($check_portfolio == "false"){
            $data['isChildAddedToPortfolio'] = "N";
            } else {
            $data['isChildAddedToPortfolio'] = 'Y';
            $data['addedChildPortfolioId'] = $check_portfolio;
            }


		       if($data['childrenGraphPoints']=='false' || $data['childrenGraphPoints']==null){
		          $status = array(
		              'status' => 2001,
		              'message' => 'Invalid input.'
		          	);
		           return response()->json($status);
		        }


           $status = 1001;
           $message = 'success';
           $getresponce = $this->innerResponceHander($data,$status,$message);
           if($request->debugMode=='true'){
               $table = array('dataType'=> $request->dataType);

               $requreParam = array(
                    'resourceType' => 'required|string|web',
                    'dataType' => 'required|string|max:20',
                    'commodity' => 'required|string|max:20',
                    'region' => 'required|string|max:20',
                    'fromDate' => 'required|string|max:20',
                    'toDate' => 'required|string|max:20',
                    'periodFormat' => 'required|string|max:20',
                    'memid' => 'required|string|max:20',
                    'columnNameOfRegion' => 'required|string|max:20',
                    'columnNameOfCommodity' => 'required|string|max:20',
                    'headerColumn' => 'required|string|max:20',
                  );
              $getresponce['debugMode']['usedtable'] = $table;
              $getresponce['debugMode']['argumentDetail'] = $requreParam;
           }       
            return response()->json($getresponce);
       } 
	}

  public function addPortfolio(Request $request){
     $rule = [
              'resourceType' => 'required|in:Web,web',
              'dataType' => 'required|string|max:20',
              'commodity' => 'required|string|max:20',
              'region' => 'required|string|max:20',
              'fromDate' => 'required|string|max:20',
              'toDate' => 'required|string|max:20',
              'periodFormat' => 'required|string|max:20',
              'memid' => 'required|string|max:20',
              'columnNameOfRegion' => 'required|string|max:20',
              'columnNameOfCommodity' => 'required|string|max:20',
              'graphType' => 'required|string|max:20',
          ];
          $check = $this->checkInputisValid($rule,$request->all());
        if($check){
          return response()->json($check);
       }else{
         $objCommon = new StplCommonController;
         $getid = $objCommon->addtoPortfolio($request->all());
         if($getid=='false'){
           $status = array(
              'status' => 2001,
              'message' => 'Invalid input.'
            );
           return response()->json($status);
         }
         $data['addedPortFolioId'] = $getid;

          $status = 1001;
           $message = 'success';
           $getresponce = $this->innerResponceHander($data,$status,$message);
           if($request->debugMode=='true'){
               $table = array('portfolio');
               $requreParam = array(
                    'resourceType' => 'required|string|web',
                    'dataType' => 'required|string|max:20',
                    'commodity' => 'required|string|max:20',
                    'region' => 'required|string|max:20',
                    'fromDate' => 'required|string|max:20',
                    'toDate' => 'required|string|max:20',
                    'periodFormat' => 'required|string|max:20',
                    'memid' => 'required|string|max:20',
                    'columnNameOfRegion' => 'required|string|max:20',
                    'columnNameOfCommodity' => 'required|string|max:20',
                    'graphType' => 'required|string|max:20',
                  );
              $getresponce['debugMode']['usedtable'] = $table;
              $getresponce['debugMode']['argumentDetail'] = $requreParam;
           }       
            return response()->json($getresponce);

       }
  }

   public function removePortfolio(Request $request){
       $rule = [
              'resourceType' => 'required|in:Web,web',
              'portFolioId' => 'required|numeric'
          ];
          $check = $this->checkInputisValid($rule,$request->all());
        if($check){
          return response()->json($check);
       }else{
          $objCommon = new StplCommonController;
         $getid = $objCommon->removeFromPortfolio($request->all());
         if($getid=='false'){
           $status = array(
              'status' => 2001,
              'message' => 'Invalid input.'
            );
           return response()->json($status);
         }
         $data['removedPortFolioId'] = $getid;
          $status = 1001;
           $message = 'success';
           $getresponce = $this->innerResponceHander($data,$status,$message);
           if($request->debugMode=='true'){
               $table = array('portfolio');
               $requreParam = array(
                   'resourceType' => 'required|in:Web,web',
                   'portFolioId' => 'required|numeric'
                  );
              $getresponce['debugMode']['usedtable'] = $table;
              $getresponce['debugMode']['argumentDetail'] = $requreParam;
           }       
            return response()->json($getresponce);
       }
    }

    public function getRecentView(Request $request){
    $trimmed = array_map('trim', $request->all());
       $validate = Validator::make($trimmed, [
             'resourceType' => 'required|in:Web,web,App,app',
             'memid' => 'required',
             'type' => 'required|in:stats_graphs,tabular',
         ]);
       if ($validate->fails()) {
           return response()->json($validate->messages(), 200);
       }
        $objCommon = new StplCommonController;
        $result = $objCommon->getRecentView($request->all());
       $status = 1001;
       $message= "success";
       $getresponce = $this->innerResponceHander($result,$status,$message);
          if($request->debugMode=='true'){
               $table = array('recent_activities');
               $requreParam = array(
                  'resourceType' => 'required|in:Web,web,App,app',
                   'memid' => 'required',
                   'type' => 'required|in:stats_graphs,tabular',
                  );
              $getresponce['debugMode']['usedtable'] = $table;
              $getresponce['debugMode']['argumentDetail'] = $requreParam;
           }       
            return response()->json($getresponce);
    }


    public function innerResponceHander($data,$status,$message){
    	 $status = array(
               'status' => $status,
               'message' => $message 
    	 	);
    	   	 $responce = array(
             'data' => $data,
             'status' => $status
    	 	);
    	 return $responce;
    }

    public function checkInputisValid($rule,$reqx){
       $trimmed = array_map('trim', $reqx);
       $messages = [
           'resourceType.required' => 'resourceType required!',
        ];  
        $validate = Validator::make($trimmed, $rule,$messages);
        if ($validate->fails()) {  
            $validationFailedArray = array();
            $validationFailedArray['status'] = array(
               'code' => 2001, 
                'messages' => 'Invalid input.'
               //'message' => $validate->errors()->first()
              );
          return $validationFailedArray;
      }
    }

}