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

class Api extends CI_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
	 */

	public function __construct(){
		parent::__construct();
        $this->load->model("Tracking_model", "tracking");
        $this->load->model("Pins_model", "pins");
        $this->load->model("Activity_model", "activity");
        $this->load->model("Sync_model", "sync");
        $this->load->model("User_model", "user");
        $this->load->model('Uuid_model', 'uuid');
        $this->load->model('Frequency_model', 'freq');
	}

    public function get_last_dates(){
        $uuid = $this->input->post('uuid');
        $user_id = $this->user->get_user_id($uuid);
        $activity = $this->activity->get_last_date($user_id);
        $pins = $this->pins->get_last_date($user_id);
        $tracking = $this->tracking->get_last_date($user_id);

        echo json_encode(array(
            "user_id" => $user_id,
            "activity" => $activity,
            "pins" => $pins,
            "tracking" => $tracking));
    }

    public function save_data(){
        $user_id = $this->input->post('user_id');
        $actjson= $this->input->post('activity');
        $pinsjson = $this->input->post('pins');
        $trackingjson = $this->input->post('tracking');

        $decode_data = json_decode($actjson, true);
        $insert_data = array();
        foreach($decode_data as $row){
            if(isset($row["id"])){
                unset($row["id"]);
            }
            $row["user_id"] = $user_id;
            array_push($insert_data, $row);
        }

        if(count($insert_data) > 0 ){
            $act_recs = $this->activity->insert_batch($insert_data);
        }else{
            $act_recs = 0;
        }


        $decode_data = json_decode($pinsjson, true);
        $insert_data = array();
        foreach($decode_data as $row){
            if(isset($row["id"])){
                unset($row["id"]);
            }
            $row["user_id"] = $user_id;
            array_push($insert_data, $row);
        }
        if(count($insert_data) > 0 ){
            $pins_recs = $this->pins->insert_batch($insert_data);
        }else{
            $pins_recs = 0;
        }

        $decode_data = json_decode($trackingjson, true);
        $insert_data = array();
        foreach($decode_data as $row){
            if(isset($row["id"])){
                unset($row["id"]);
            }
            $row["user_id"] = $user_id;
            array_push($insert_data, $row);
        }
        if(count($insert_data) > 0 ){
            $tracking_recs = $this->tracking->insert_batch($insert_data);
        }else{
            $tracking_recs = 0;
        }
         
        date_default_timezone_set("Asia/Calcutta");
        $last_sync = array("user_id" => $user_id, "last_synch"=> date('Y-m-d H:i'));
        $this->sync->save($last_sync);

        echo "Activity {$act_recs} records, Tracking {$tracking_recs} records, Pins {$pins_recs} records saved";
    }

    public function save_uuid() {
        $uuid = $this->input->post('uuid');
        $id = $this->uuid->save($uuid);
        if($id > 0) {
            echo json_encode(array("success"=>1));
        }else{
            echo json_encode(array("success"=>0));
        }
    }
    
    public function get_duration() {
        $duration = $this->freq->get_lastest();
        echo $duration;
    }
}
