/**
  * Display route detail information
  *
  * @param $rute_id
  * @return \Illuminate\Http\JsonResponse
  */
 public function busRouteDetail($rute_id)
 {
     $response = array();
     try {
         $routeModel = new Route();
         $busRouteModel = new BusRoute();
         $busOperationModel = new BusOperation();
         $infoRoute = $routeModel->where('rute_id', '=', $rute_id)->get()->toArray();
         $totalBusStop = $busRouteModel->select(DB::raw('count(*) as total_halte'))->where('rute_id', '=', $rute_id)->get()->toArray();
         $totalBusOperation = $busOperationModel->select(DB::raw('count(*) as total_bus'))->where('rute_id', '=', $rute_id)->get()->toArray();
         if (isset($infoRoute[0]) && isset($totalBusStop[0]) && $totalBusOperation[0]) {
             $container = array();
             $container['rute_id'] = $infoRoute[0]['rute_id'];
             $container['deskripsi'] = $infoRoute[0]['deskripsi'];
             $container['total_halte'] = $totalBusStop[0]['total_halte'];
             $container['total_bus'] = $totalBusOperation[0]['total_bus'];
             $response['code'] = 200;
             $response['data'] = $container;
         } else {
             $response['code'] = 400;
             $response['data']['msg'] = 'no route information can be found, make sure you attach a correct route identifier';
         }
     } catch (\Exception $e) {
         $response['code'] = 500;
         $response['data']['msg'] = "internal server error, please contact administrator";
     }
     header("Access-Control-Allow-Origin: *");
     return response()->json($response);
 }
 public function getBusInRoute($rute_id)
 {
     $busOperationModel = new BusOperation();
     $response = array();
     if ($rute_id != 'all') {
         $busOperation = $busOperationModel->select('plat_nomor', 'rute_id', 'last_latitude', 'last_longitude', 'avg_speed', 'driver_id', 'conductor_id')->where('rute_id', '=', $rute_id)->get()->toArray();
     } elseif ($rute_id == 'all') {
         $busOperation = $busOperationModel->select('plat_nomor', 'rute_id', 'last_latitude', 'last_longitude', 'avg_speed', 'driver_id', 'conductor_id')->get()->toArray();
     }
     if ($busOperation != null) {
         $response['code'] = 200;
         $response['data'] = $busOperation;
     } else {
         $response['code'] = 400;
         $response['data']['msg'] = 'no one bus operate in that route';
     }
     header("Access-Control-Allow-Origin: *");
     return response()->json($response);
 }
 /**
  * get nearest bus heading to certain bus stop
  *
  * @param $halte_id
  * @return \Illuminate\Http\JsonResponse
  */
 public function getNearestArrivalEstimation($halte_id)
 {
     $arrivalEstimationModel = new ArrivalEstimation();
     try {
         $arrivalEstimation = $arrivalEstimationModel->where('halte_id_tujuan', $halte_id)->orderBy('waktu_kedatangan', 'asc')->take(1)->get()->toArray();
         if (isset($arrivalEstimation[0])) {
             $busOperationModel = new BusOperation();
             $lastPosition = $busOperationModel->select('last_latitude', 'last_longitude')->where('plat_nomor', '=', $arrivalEstimation[0]['plat_nomor'])->first();
             $this->nearestArrivalEtimation = $arrivalEstimation[0];
             $response = array();
             $response['code'] = 200;
             $response['data'] = $this->nearestArrivalEtimation;
             $response['data']['bus_latitude'] = $lastPosition['last_latitude'];
             $response['data']['bus_longitude'] = $lastPosition['last_longitude'];
         } else {
             $response['code'] = 400;
             $response['data']['msg'] = 'cannot find nearest bus, please try again later';
         }
     } catch (\Exception $e) {
         $response['code'] = 500;
         $response['data']['msg'] = 'internal error, please try again later or contact administrator';
     }
     header("Access-Control-Allow-Origin: *");
     return response()->json($response);
 }
 /**
  * controller buat handle request post ke URI /report_location
  * attribut post ada rute_id, lat, long, speed, semua dikirim plain form request
  *
  * @param Request $requests
  * @return \Illuminate\Http\JsonResponse
  */
 public function postLocation(Request $requests)
 {
     $routeModel = new route();
     $plat = $requests->input('plat');
     $this->plat_nomor = $plat;
     $input_token = $requests->input('token');
     $bus = new BusOperation();
     $reference_token = $bus->select('token')->where('plat_nomor', '=', $plat)->get()->toArray();
     try {
         if ($input_token == $reference_token[0]['token']) {
             $location = new StoreLocationModel();
             $location->route_id = $requests->input('rute_id');
             $this->rute_id = $requests->input('rute_id');
             $routeModel->where('rute_id', '=', $this->rute_id)->firstOrFail();
             $flagCheck = $this->checkBusIteration();
             $location->latitude = $requests->input('lat');
             $this->busLat = $requests->input('lat');
             $location->longitude = $requests->input('long');
             $this->busLon = $requests->input('long');
             $location->avg_speed = $requests->input('speed');
             $this->avg_speed = $requests->input('speed');
             $location->plat_nomor = $plat;
             $location->save();
             if ($flagCheck) {
                 if ($location->save()) {
                     $this->getLastBusStop();
                     $this->selectBusHistory();
                     try {
                         $this->getAllBusStop();
                     } catch (\Exception $e) {
                         $this->response['data']['msg'] = 'internal error, cannot make request to third pary service or temporary connection down, please try again or report it';
                         $this->response['code'] = 500;
                         header("Access-Control-Allow-Origin: *");
                         return response()->json($this->response);
                     }
                     $this->updateBusOperation();
                     $this->checkBusLocationStatus();
                     $this->checkBusStopHistory();
                     $this->detectSpeedViolation();
                     //all operation successfull
                     header("Access-Control-Allow-Origin: *");
                     return response()->json($this->response);
                 } else {
                     $this->response['data']['msg'] = 'internal error, cannot save data to database, please try again or report it';
                     $this->response['code'] = 500;
                     header("Access-Control-Allow-Origin: *");
                     return response()->json($this->response);
                 }
             } else {
                 $this->updateBusOperation();
                 $this->response['code'] = 200;
                 $this->response['data']['msg'] = 'update bus coordinate location';
                 header("Access-Control-Allow-Origin: *");
                 return response()->json($this->response);
             }
         } else {
             $this->response['data']['msg'] = 'transaction failed, make sure you enter a valid token';
             $this->response['code'] = 400;
             header("Access-Control-Allow-Origin: *");
             return response()->json($this->response);
         }
     } catch (\Exception $e) {
         $this->response['data']['msg'] = 'one or more of your parameter value is invalid, make sure you send correct parameter';
         $this->response['code'] = 400;
         header("Access-Control-Allow-Origin: *");
         return response()->json($this->response);
     }
 }