<?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 mapController extends Controller
{
    
   public function getMapCommoAndRegion(Request $request){
      $rule = [
          'resourceType' => 'required|in:Web,web',
          'dataType' => 'required|string'
        ];
      $check = $this->checkInputisValid($rule,$request->all());
     if($check){
      return response()->json($check);
     }else{
          $objCommon = new StplCommonController;
          $data = $objCommon->getComandRegionList($request->all());
          if($data=='false'){
             $status = array(
              'status' => 10005,
              'message' => 'No Record Found!'
            );
            return response()->json($status);
          }
          $status = 1001;
          $message= "success";
          $getresponce = $this->innerResponceHander($data,$status,$message);
          if($request->debugMode=='true'){
          $table = array('tbl_map_left_toolbox');
          $requreParam = array(
                  'resourceType' => 'required|in:Web,web',
                  'dataType' => 'required|string'
            );
          $getresponce['debugMode']['usedtable'] = $table;
          $getresponce['debugMode']['argumentDetail'] = $requreParam;
          }       
          return response()->json($getresponce);
     }
   }
   public function getMapSelection(Request $request){
      $rule = [
          'resourceType' => 'required|in:Web,web',
          'dataType' => 'required|string',
          'commodity' => 'required|string',
          'columnNameOfCommodity' => 'required|string',
          'memid' => 'required|string'
        ];
      $check = $this->checkInputisValid($rule,$request->all());
     if($check){
      return response()->json($check);
     }else{ 
          if($request->region){
            $responseArray = array(
              'memid' => trim($request->memid),
              'commodity' => trim($request->commodity),
              'columnNameOfCommodity' => trim($request->columnNameOfCommodity),
              'region' => trim($request->region)
            ); 
          }else{
          $responseArray = array(
              'memid' => trim($request->memid),
              'commodity' => trim($request->commodity),
              'columnNameOfCommodity' => trim($request->columnNameOfCommodity),
            ); 
          }
          $status = 1001;
          $message= "success";
          $getresponce = $this->innerResponceHander($responseArray,$status,$message);
          if($request->debugMode=='true'){
          $table = array('no table Used');
          $requreParam = array(
                  'resourceType' => 'required|in:Web,web',
                  'dataType' => 'required|string',
                  'commodity' => 'required|string',
                  'columnNameOfCommodity' => 'required|string',
                  'memid' => 'required|string'
            );
          $getresponce['debugMode']['usedtable'] = $table;
          $getresponce['debugMode']['argumentDetail'] = $requreParam;
          }       
          return response()->json($getresponce);

     }
   }

   public function saveMapSelection(Request $request){
      $rule = [
          'resourceType' => 'required|in:Web,web',
          'dataType' => 'required|string',
          'commodity' => 'required|string',
          'columnNameOfCommodity' => 'required|string',
          'memid' => 'required|string',
          'name' => 'required|string'
        ];
      $check = $this->checkInputisValid($rule,$request->all());
     if($check){
      return response()->json($check);
     }else{ 
        
          $objCommon = new StplCommonController;
          
          $checkExist = $objCommon->checkSelectionNameexist($request->all());
          // die('here');
          if($checkExist!='true'){
             $status = array(
              'status' => 10003,
              'message' => 'Selection Name Already Exist!'
            );
            return response()->json($status);
          }
          $data['lastInsertedId'] = $objCommon->saveMapSelectionList($request->all());
          $status = 1001;
          $message= "Record Has been Saved!";
          $getresponce = $this->innerResponceHander($data,$status,$message);
          if($request->debugMode=='true'){
            $table = array('tbl_map_selection_saved_list');
            $requreParam = array(
                'resourceType' => 'required|in:Web,web',
                'dataType' => 'required|string',
                'commodity' => 'required|string',
                'columnNameOfCommodity' => 'required|string',
                'memid' => 'required|string',
                'name' => 'required|string'
              );
            $getresponce['debugMode']['usedtable'] = $table;
            $getresponce['debugMode']['argumentDetail'] = $requreParam;
          }       
          return response()->json($getresponce);
     }
   }

   public function getSavedSelection(Request $request){
      $rule = [
          'resourceType' => 'required|in:Web,web',
          'memid' => 'required|string'
        ];
      $check = $this->checkInputisValid($rule,$request->all());
     if($check){
      return response()->json($check);
     }else{
          $objCommon = new StplCommonController;
          $data = $objCommon->getSelectionById($request->all());
          if($data=='false'){
             $status = array(
              'status' => 10005,
              'message' => 'No record found!'
            );
            return response()->json($status);
          }
          $status = 1001;
          $message= "success";
          $getresponce = $this->innerResponceHander($data,$status,$message);
          if($request->debugMode=='true'){
            $table = array('tbl_map_selection_saved_list');
            $requreParam = array(
                'resourceType' => 'required|in:Web,web',
                'memid' => 'required|string'
              );
            $getresponce['debugMode']['usedtable'] = $table;
            $getresponce['debugMode']['argumentDetail'] = $requreParam;
          }       
          return response()->json($getresponce);
      }
   }
   public function removeSavedSelection(Request $request){
      $rule = [
          'resourceType' => 'required|in:Web,web',
          'memid' => 'required|string',
          'selectionId' => 'required|string'
        ];
      $check = $this->checkInputisValid($rule,$request->all());
     if($check){
      return response()->json($check);
     }else{
         $objCommon = new StplCommonController;
          $data['removedId'] = $objCommon->removeSelectionById($request->all());
          if($data=='false'){
             $status = array(
              'status' => 10005,
              'message' => 'No record found!'
            );
            return response()->json($status);
          }
          $status = 1001;
          $message= "Selection Has been Removed!";
          $getresponce = $this->innerResponceHander($data,$status,$message);
          if($request->debugMode=='true'){
            $table = array('tbl_map_selection_saved_list');
            $requreParam = array(
                'resourceType' => 'required|in:Web,web',
                'memid' => 'required|string',
                'selectionId' => 'required|string'
              );
            $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;
      }
    }  

}