<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Passive extends MY_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see https://codeigniter.com/user_guide/general/urls.html
	 */

	function __construct(){
		parent::__construct();
		$this->load->model('Tracking_model', "track");
		$this->load->library('googlemaps');
	}
	public function index()
	{
        $config['center'] = '37.4419, -122.1419';
        $config['zoom'] = '12';
        $config['apiKey'] = 'AIzaSyA5os-lLvgVneN1434lMNcupgoI8vhA9Gw';
        $config['map_height'] = '100%';
        $this->googlemaps->initialize($config);

        $data['map'] = $this->googlemaps->create_map();
		$this->load->view('passive', $data);
	}

	public function show_track($user_id, $date) {
		$start = $date." 00:00:00";
		$end = $date." 23:59:59";
		$res = $this->track->get_data_between($user_id, $start, $end, true);
		if($res["success"]){
			$data = $res["data"];
			$first = $data[0];
			$config['center'] = "{$first["lat"]}, {$first["lng"]}";
			$config['zoom'] = '12';
			$config['apiKey'] = 'AIzaSyA5os-lLvgVneN1434lMNcupgoI8vhA9Gw';
			$config['map_height'] = '100%';
            $this->googlemaps->initialize($config);

            $points = array();
            $prev_status = $first["status"];
            $previous_row = $first;
            $cnt = 0;
			foreach($data as $row){
                $loc = "{$row["lat"]}, {$row["lng"]}";
                array_push($points, $loc);
                
                if ($row['status'] != $prev_status) {
                    $polyline = array();
                    $polyline['points'] = $points;
                    if($prev_status == 1){
                        $polyline['strokeColor'] = '#FF0000';
                    }else{
                        $polyline['strokeColor'] = '#0000FF';
                    }
                    $this->googlemaps->add_polyline($polyline);
                    $points = array();
                    array_push($points, $loc);
                    $prev_status = $row["status"];
                }
                
                if($cnt % 3 == 0){
                    $marker = array();
                    $marker['position'] = "{$row["lat"]}, {$row["lng"]}";
                
                    if($cnt == 0){
                        $marker['infowindow_content'] = "Start <br/> {$row["created_date"]}";
                        $marker['icon'] = "http://maps.google.com/mapfiles/ms/icons/blue.png";
                    }else{
                        $marker['icon'] = "http://maps.google.com/mapfiles/ms/micons/yellow.png";
                        $marker['infowindow_content'] = "{$row["created_date"]}";
                    }
                    
                    $this->googlemaps->add_marker($marker);
                }
                $cnt++;
			}
	        
	        $polyline = array();
            $polyline['points'] = $points;
            if($prev_status == 1){
                $polyline['strokeColor'] = '#FF0000';
            }else{
                $polyline['strokeColor'] = '#0000FF';
            }
            $this->googlemaps->add_polyline($polyline);
            
	        $marker = array();
	        $last = $data[$cnt - 1];
            $marker['position'] = "{$last["lat"]}, {$last["lng"]}";
            $marker['icon'] = "http://maps.google.com/mapfiles/ms/icons/red.png";
            $marker['infowindow_content'] = "End <br/> {$last["created_date"]}";  
            $this->googlemaps->add_marker($marker);
		
			$data['map'] = $this->googlemaps->create_map();
			$this->load->view('pages/passive', $data);
		}else{
		    echo "No data to display";
		}
	}
}
