/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  */
/* COBAN GPS_103/GPS303 communication tools                   (c) Pavitra Rastogi 23rd July,2018  */
/*                                                                                   MIT Licence  */
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  */

'use strict';
//if (typeof module!='undefined' && module.exports) var Dms = require('./dms'); // ≡ import Dms from 'dms.js'

/**
 * Creates a header object from device data packet.
 *
 * @constructor
 * @param {string} raw - utf8 encoded packet.
 * @param {obj} socket - socket from tcp connection between server and device.
 *
 * @example
 *     var parsedHead = new GPS_x03(raw);
 */
function GPS_x03(raw, socket) {
    // do not allow instantiation without 'new'
    if (!(this instanceof GPS_x03)) throw new TypeError('Object  is not GPS_x03 type');

    this.imei = raw.substr(raw.indexOf('imei:') + 5, 15);
    this.socket = socket;
    this.raw = raw;
    if (raw.search(/^##,imei:/) != -1 && raw.search(/,A;$/) !=-1){
        this.command = 'login';
    }
    else {
        this.command = 'tracker';
    }
}


/**
 * Logs in device in our server.
 *
 * 
 * @returns {boolean} Returns 'true' if login was successful.
 *
 * @example
 *     var gps_x03  = new GPS_x03(raw);
 *     if(gps_x03.doLogin()){
 *          //perform some action
 *     }
 *     
 */
GPS_x03.prototype.doLogin = function() {
    if (!(this instanceof GPS_x03)) throw new TypeError('Object is not GPS_x03 type');

    this.socket.write('LOAD');

    return true;
};



/**
 * Returns the parsed data for device protocol command 'tracker'.
 *
 * @returns {obj} Parsed ping data from device.
 *
 * @example
 *     var gps_x03p1 = new GPS_x03(raw, socket);
 *     if(gps_x03.doLogin()){
 *          var parsedData = gps_x03.getPingData();
 *     }
 */
GPS_x03.prototype.getPingData = function() {
    if (!(this instanceof GPS_x03)) throw new TypeError('Object not of type GPS_x03');
    if ((this.command != 'tracker')) throw new TypeError('packet is not of type tracker');


    
    var msg_parts = this.raw.split(',');
    
    var date = new Date(
        parseInt('20' + msg_parts[2].substr(0, 2)),
        parseInt(msg_parts[2].substr(2, 2))-1,
        parseInt(msg_parts[2].substr(4, 2)),
        parseInt(msg_parts[2].substr(6, 2)),
        parseInt(msg_parts[2].substr(8, 2)),
        parseInt(msg_parts[2].substr(10, 2))
    );

    var latitude = msg_parts[8] == 'N' ?
    parseFloat(msg_parts[7].substr(0, 2)) + (parseFloat(msg_parts[7].substr(2)) / 60) :
    parseFloat(msg_parts[7].substr(0, 2)) + (parseFloat(msg_parts[7].substr(2)) / 60) * -1;
    var longitude = msg_parts[10] == 'E' ?
    parseFloat(msg_parts[9].substr(0, 3)) + (parseFloat(msg_parts[9].substr(3)) / 60) :
    parseFloat(msg_parts[9].substr(0, 3)) + (parseFloat(msg_parts[9].substr(3)) / 60) * -1;

    return {
        "imei": this.imei,
        "command": msg_parts[1],
        "date": date,
        "dateString": msg_parts[2],
        "sim" : msg_parts[3],
        "latDecimal": latitude,
        "longDecimal": longitude,
        "insertionTime": new Date(),
        "raw": this.raw,
        "speed": parseInt(msg_parts[11]).toString(),
        "timeString": msg_parts[5],
        "heading": (msg_parts[12]).toString(),
        "altitude": (msg_parts[13]).toString(),
        "ignition": (msg_parts[14]).toString(),
        "door" : (msg_parts[15]).toString(),
        "geoJSON": {
            "type": "Point",
            "coordinates": [longitude, latitude ]
        },
        'GPS positioned': msg_parts[4] == 'F' ? '1' : '0'
    };
};





/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  */
if (typeof module != 'undefined' && module.exports) module.exports = GPS_x03; // ≡ export default GPS_x03