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

class Tracking_model extends MY_Model
{

    /**
     * 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
     */
    public function __construct()
    {
        parent::__construct();
        $this->tb_name = "tbl_tracking";
    }

    public function get_data_between($user_id, $start, $end, $passive = false)
    {
        $this->db->select("*")
            ->from($this->tb_name)
            ->where('user_id', $user_id)
            ->where("created_date between '{$start}' and '{$end}'")
            ->order_by('created_date', 'asc');
        if (!$passive) {
            $this->db->where("status", 1);
        }
        $query = $this->db->get();

        if ($query->num_rows() > 0) {
            return array("success" => true, "data" => $query->result_array());
        } else {
            return array("success" => false, "message" => "No data to display!");
        }
    }

    public function get_last_date($user_id)
    {
        $query = $this->db->select("*")
            ->from($this->tb_name)
            ->where('user_id', $user_id)
            ->order_by("created_date", "desc")
            ->limit(1)
            ->get();
        if ($query->num_rows() > 0) {
            $row = $query->row_array();
            return $row["created_date"];
        } else {
            return "2018-01-01 00:00:00";
        }
    }

    public function get_distance_of_day($day, $user_id)
    {
        $start_time = $day . " 00:00:00";
        $end_time = $day . " 23:59:59";
        $query = $this->db->select("*")
            ->from($this->tb_name)
            ->where("created_date between '{$start_time}' and '{$end_time}'")
            ->where('user_id', $user_id)
            ->order_by('created_date', 'asc')
            ->get();
        if ($query->num_rows() > 0) {
            $first = $query->first_row("array");
            $rows = $query->result_array();
            $distance = 0;
            foreach($rows as $row) {
                $distance += $this->distance($first["lat"], $first["lng"], $row["lat"], $row["lng"], "K");
                $first = $row;
            }
            return $distance;
        } else {
            return 0;
        }
    }

    public function distance($lat1, $lon1, $lat2, $lon2, $unit)
    {

        $theta = $lon1 - $lon2;
        $dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
        $dist = acos($dist);
        $dist = rad2deg($dist);
        $miles = $dist * 60 * 1.1515;
        $unit = strtoupper($unit);

        if ($unit == "K") {
            return ($miles * 1.609344);
        } else if ($unit == "N") {
            return ($miles * 0.8684);
        } else {
            return $miles;
        }
    }

    public function delete_data_between($start, $end)
    {
        $start_time = $start . " 00:00:00";
        $end_time = $end . " 23:59:59";
        $result = $this->db->where("created_date between '{$start}' and '{$end}'")
                           ->delete($this->tb_name);
        return $this->db->affected_rows();
    }

    public function get_user_last_location(){
        $this->db->select("*")
                 ->from($this->tb_name." as a")
                 ->join("tbl_user as b", "a.user_id=b.id")
                 ->group_by('a.user_id')
                 ->order_by('created_date', 'desc');
        $query = $this->db->get();
        if ($query->num_rows() > 0) {
            return array("success" => true, "data" => $query->result_array());
        } else {
            return array("success" => false, "message" => "No data to display!", "data"=>array());
        }
    }
}
