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

class Pins 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("Pins_model", "pins");
        $this->load->model("User_model", "user");
        $this->load->library('googlemaps');
    }
    public function index()
	{
        //get markers
        $level = $this->session->userdata('level');
        $user_id = $this->session->userdata('user_id');
        $res = $this->pins->get_pins_of_user($user_id);
        $markers = $res["data"];
        $data["report_type"] = "map";
        $data['date_type'] = "day";
        $data["user_id"] = $user_id;
		$this->render($markers, $data);
    }

    public function get_pins_of_user($user_id, $report_type="map"){
        $res = $this->pins->get_pins_of_user($user_id);
        $markers = $res["data"];

        $data["user_id"] = $user_id;
        $data["report_type"] = $report_type;
        $this->render($markers, $data);
    }

    public function get_pins_of_day($user_id, $report_type, $date){
        $start = $date." 00:00:00";
        $end = $date." 23:59:59";

        $res = $this->pins->get_pins_of_period($user_id, $start, $end);
        $markers = $res["data"];
        $data["user_id"] = $user_id;
        $data["report_type"] = $report_type;
        $data["date_type"] = "day";
        $data["date"] = $date;
        $this->render($markers, $data);
    }

    public function get_pins_of_week($user_id, $report_type, $year, $month, $week){
        $s_month = $month > 9 ? "".$month : "0".$month;
        $first_day_of_month = $year."-".$s_month."-01";
        $first_week = date("W", strtotime($first_day_of_month));

        $cur_week = $first_week + $week - 1;

        $dto = new DateTime();
        $dto->setISODate($year, $cur_week);
        $start = $dto->format('Y-m-d');
        $dto->modify('+6 days');
        $end = $dto->format('Y-m-d');

        $start = $start." 00:00:00";
        $end = $end." 23:59:59";

        $res = $this->pins->get_pins_of_period($user_id, $start, $end);
        $markers = $res["data"];
        $data["user_id"] = $user_id;
        $data["report_type"] = $report_type;
        $data["date_type"] = "week";
        $data["year"] = $year;
        $data["month"] = $month;
        $data["week"] = $week;
        $this->render($markers, $data);

    }

    public function get_pins_of_month($user_id, $report_type, $year, $month){
        $s_month = $month > 9 ? "".$month : "0".$month;
        $start = $year."-".$s_month."-01 00:00:00";
        $end = $year."-".$s_month."-31 23:59:59";

        $res = $this->pins->get_pins_of_period($user_id, $start, $end);
        $markers = $res["data"];
        $data["user_id"] = $user_id;
        $data["report_type"] = $report_type;
        $data["date_type"] = "month";

        $data["year"] = $year;
        $data["month"] = $month;
        $this->render($markers, $data);
    }

    public function get_pins_of_period($user_id, $report_type, $start, $end){
        if(!isset($start)){
            $start = date('Y-m-d');
        }

        if(!isset($end)) {
            $end = date('Y-m-d');
        }

        $start_t = $start." 00:00:00";
        $end_t = $end." 23:59:59";
        $res = $this->pins->get_pins_of_period($user_id, $start_t, $end_t);
        $markers = $res["data"];
        $data["user_id"] = $user_id;
        $data["report_type"] = $report_type;
        $data["date_type"] = "period";

        $data["start"] = $start;
        $data["end"] = $end;
        $this->render($markers, $data);
    }


    private function render(array $markers, array $res){
        if(count($markers) > 0) {
            $first = $markers[0];
            $config['center'] = "{$first["lat"]}, {$first["lng"]}";
        }else{
            $config['center'] = '24.566368, 80.809889';
        }
        $config['zoom'] = '14';
        $config['apiKey'] = 'AIzaSyA5os-lLvgVneN1434lMNcupgoI8vhA9Gw';
        $config['map_height'] = '100%';
        $this->googlemaps->initialize($config);
        $points = array();
        foreach($markers as $item){
            $marker = array();
            $marker['position'] = "{$item["lat"]}, {$item["lng"]}";
            $marker['infowindow_content'] = "<b>Comment:</b> {$item["description"]} <br/><br/><b>Date Time:</b> {$item["created_date"]}";
            $marker['onmouseover'] = "iw_map.setContent(this.get('content'));iw_map.open(map, this);";
            $marker['onmouseout'] = "iw_map.close();";
            $this->googlemaps->add_marker($marker);

            $loc = "{$item["lat"]}, {$item["lng"]}";
            array_push($points, $loc);
        }

        $polyline = array();
        $polyline['points'] = $points;
        $this->googlemaps->add_polyline($polyline);

        $data['map'] = $this->googlemaps->create_map();
        $res_user = $this->user->get_all();
        $data["users"] = $res_user["data"];
        $data["list"] = $markers;
		$this->load->view('layout', array("view"=>'pages/pins', "data"=>$data + $res));
    }
}
