<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of file_upload
 *
 * @author zmq163
 */
class File_upload extends CI_Controller {

    //put your code here
    // var $mdb;

    public function __construct() {
        parent::__construct();
        $this->load->model('file_upload_model');
        $this->load->library('date_conversion');
    }

    /**
     * 
     * 
     * 
     * 
     */
    public function dateConversionToEnglishDate() {

        print_r(date('Y-m-d', strtotime($this->date_conversion->jalali_to_gregorian(1396, 3, 8, 'Y-m-d'))));
    }

    public function test() {
        $tempData = array();
        $endDate = strtotime('+ 6 month');
        for ($i = strtotime('Wednesday', time()); $i <= $endDate; $i = strtotime('+1 week', $i)) {

            $tempData[] = array('week_number' => date('W', $i), 'start_date' => date('Y-m-d', $i), 'end_date' => date('Y-m-d', strtotime(date('Y-m-d', $i) . '+ 6 day')));
        }
        print_r($tempData);
        // echo $this->file_upload_model->daycount("monday", strtotime("01.01.2009"), 0);
//        $data = array(272, 781, 776, 494, 495, 532, 559, 173, 174, 509,
//            511, 544, 545, 547, 548, 549, 550, 614, 615, 642, 740, 586
//            , 587, 588, 590, 592, 535, 539, 639, 640, 457, 458, 460, 464,
//            465, 636, 661, 662, 429, 466, 467, 468, 805, 816, 868, 870,
//            251, 255, 422, 552, 878, 275, 447, 483, 486, 487, 488, 479,
//            504, 523, 569, 603, 604, 644, 647, 677, 679, 810, 812, 846,
//            847, 187, 278, 284, 578, 634, 114, 324, 525, 577, 621, 848,
//            850, 369, 514, 556, 623, 748, 766, 767, 768, 802, 754, 366,
//            431, 482, 491, 534, 542, 754, 111, 582, 30, 518, 558, 823,
//            171, 172, 420, 432, 443, 445, 461, 472, 474, 475, 476, 477,
//            480, 555, 583, 808, 414, 416, 418, 596, 597, 602, 825, 826,
//            865, 434, 433, 839, 838, 260, 268, 269, 745, 756, 575, 611,
//            612, 613, 617, 627, 630, 632, 633, 649, 651, 652, 653, 655, 537, 773);
//        print_r(count($data));
        //$this->file_upload_model->testModel($data);
    }

//    /**
//     * 
//     * 
//     * 
//     * 
//     */
//    public function getId() {
//        $mdb = $this->load->database('mdb', TRUE);
//        $mdb->insert('iot_test', array('name' => 'abdul muttalib'));
//        $insert_id = $mdb->insert_id();
//        $numlength = strlen((string) $insert_id);
//        $unique_id = NULL;
//        switch ($numlength) {
//            case 1:
//                $unique_id = 'ZMQ000' . $insert_id;
//                break;
//            case 2:
//                $unique_id = 'ZMQ00' . $insert_id;
//                break;
//            case 3:
//                $unique_id = 'ZMQ0' . $insert_id;
//                break;
//            case 3:
//                $unique_id = 'ZMQ' . $insert_id;
//                break;
//        }
//        $mdb->where('auto_id', $insert_id);
//        $mdb->update('iot_test', array('uniqueid' => $unique_id));
//        print_r($unique_id);
//    }

    /**
     * 
     * 
     * 
     * 
     */
//    public function arduinoGSMTest() {
//        $mdb = $this->load->database('mdb', TRUE);
//        $mdb->insert('iot_test', array('name' => 10000));
//        print_r(date('Y-m-d H:i:s'));
//        print_r($_GET);
//        //echo 'Thanks for contact me.';
//    }

    /**
     * 
     * 
     * 
     * 
     * 
     * 
     */
    /**
     * Date 18-10-2016
     * This serviced used as test purpose for Matloof.
     * 
     * 
     * 
     */
//    public function login() {
//        if (isset($_POST['username']) and isset($_POST['password'])) {
//            $mdb = $this->load->database('mdb', TRUE);
//            $mdb->select('*');
//            $mdb->from('user_details_testing_android');
//            $mdb->where(array('username' => $_POST['username'], 'password' => $_POST['password']));
//            $query = $mdb->get();
//            if ($query->num_rows() > 0) {
//                print_r(json_encode(array('status' => TRUE, 'message' => 'Your login and password is correct', 'user_details' => $query->row_array())));
//            } else {
//                print_r(json_encode(array('status' => FALSE, 'message' => 'Your logId or password incorrect')));
//            }
//        } else {
//            print_r(json_encode(array('status' => FALSE, 'message' => 'Your login or password incorrect')));
//        }
//    }

    /**
     * 
     * 
     * 
     * 
     */
//    public function submitUserData() {
//        $mdb = $this->load->database('mdb', TRUE);
//        if (isset($_POST['username']) and isset($_POST['password']) and isset($_POST['address']) and isset($_POST['age'])) {
//            $data = array('username' => $_POST['username'], 'password' => $_POST['password'], 'address' => $_POST['address'], 'age' => $_POST['age']);
//            $mdb->insert('user_details_testing_android', $data);
//            print_r(json_encode(array('status' => TRUE, 'message' => 'Your data is submited')));
//        } else {
//            print_r(json_encode(array('status' => FALSE, 'message' => 'Your data submission is fail')));
//        }
//    }

    /**
     * 
     * 
     * 
     */
    private function removeStringFromEncodedChunk($_chunk_encoded_base64_data) {
        if ($_chunk_encoded_base64_data != NULL) {

            $_chunk_encoded_base64_data_remove_additional_cha = NULL;
            if (substr($_chunk_encoded_base64_data, (strlen($_chunk_encoded_base64_data) - 2), 2) == '==') {
                return $_chunk_encoded_base64_data;
                //return substr($_chunk_encoded_base64_data, 0, (strlen($_chunk_encoded_base64_data) - 2));
            } else {
                return $_chunk_encoded_base64_data;
            }
        }
    }

    public function writeVideo() {

        
            $mdb = $this->load->database('mdb', TRUE);
            $mdb->select('*');
            $mdb->from('encoded_base64_data');
            $mdb->where(array('file_name' => '1500705341_5CCF7FC10A6B20170722g.mp4'));

            $query = $mdb->get();
            $temp = NULL;
            foreach ($query->result_array() as $value) {

                $temp = $temp . base64_decode(str_replace(' ', '+', $this->removeStringFromEncodedChunk($value['encode_base64_data'])));
            }
            //$mdb->insert('encoded_base64_data',array('encode_base64_data' => base64_encode($temp), 'file_name' =>'abc.mp4', 'file_type' => 'video'));
            //print_r($temp);
            file_put_contents('./IOT/image/abc.mp4', $temp);
       
    }

    /**
     * 
     * 
     * 
     * 
     * 
     */
    public function uploadFileInChunkIOT() {
        $responseData = $this->file_upload_model->uploadFileInChunkIOTModel($this->set_request_data($_SERVER));
        print_r(json_encode($responseData));
    }
    /**
     * 
     * 
     * 
     * 
     */
    public function uploadFileInChunkIOT1(){
        print_r(
                json_encode(
                        array('message'=>'success','data'=> json_decode(file_get_contents('php://input'),TRUE))));
    }

    /**
     * 
     * 
     * 
     * 
     * 
     * 
     */
//    public function uploadFileMultipleChunkIOT1() {
//        print_r($_SERVER);
//        echo '</br>';
//        print_r($_POST);
//        echo '</br>';
//        print_r($_GET);
//        //$request_data_from_iot_device = $this->set_request_data($_SERVER);
//        //file_put_contents('./IOT/text/test.txt', $request_data_from_iot_device, FILE_APPEND);
//        //print_r($request_data_from_iot_device);
//    }
//    public function uploadFileMultipleChunkIOT() {
//        $request_data_from_iot_device = $this->set_request_data($_SERVER);
//        $_chunk_data_array = NULL;
//        if (isset($request_data_from_iot_device['total_chunk_in_array'])) {
//            for ($i = 0; $i < $request_data_from_iot_device['total_chunk_in_array']; $i++) {
//                if (isset($request_data_from_iot_device['chunk_data' . $i])) {
//                    $temp_chunk_data = explode('###', $request_data_from_iot_device['chunk_data' . $i]);
//                    $_chunk_data_array[] = array('file_name' => $request_data_from_iot_device['file_name'], 'file_type' => $request_data_from_iot_device['file_type'], 'encode_string' => $temp_chunk_data[1], 'chunk_id' => $temp_chunk_data[0]);
//                }
//            }
//            if ($this->file_upload_model->uploadFileMultipleChunkIOTModel($_chunk_data_array, $request_data_from_iot_device['file_name'], $request_data_from_iot_device['file_type'], $request_data_from_iot_device['chunk_id'], $request_data_from_iot_device['total_chunk'])) {
//                print_r(json_encode(array('status' => '1')));
//            } else {
//                print_r(json_encode(array('status' => '2')));
//            }
//        } else {
//            print_r(json_encode(array('status' => 0)));
//        }
//    }

    /**
     * 
     * @param type $server
     * @return type
     * 
     * 
     * 
     * 
     */
    private function set_request_data($server) {
        $_request_data = NULL;
        switch ($server['REQUEST_METHOD']) {
            case "POST":
                if (isset($server['HTTP_JSON'])) {
                    $_request_data = json_decode($server['HTTP_JSON'], TRUE);
                } else {
                    $_request_data = $_POST;
                }
                break;
            case "GET":
                if (isset($server['HTTP_JSON'])) {
                    $_request_data = json_decode($server['HTTP_JSON'], TRUE);
                } else {
                    $_request_data = $_GET;
                }
                break;
        }

        return $_request_data;
    }

}
