'use strict';

var mqtt = require('mqtt');
var constants = require("./constants");

var chat = (function() {
  var myInfo;

  function configMyInfo(coid, emplid, loginid, recvCallback) {
    myInfo = {};
    myInfo.coid = coid;
    myInfo.emplid = emplid;
    myInfo.loginid = loginid;
    myInfo.recvCallback = recvCallback;

    console.log('coid:%i, emplid:%i, loginid:%s, recvCallback:%s', coid, emplid, loginid, recvCallback.name);

    if ((myInfo.client = _createMQTTClient()) === null) {
      console.error("Failed to initialize MQTT client");
      return;
    }
  }

  function _getTopic(connType, partid) {
    var myTopic;
    if (connType === constants.DIRECT_CHAT) {
      if (myInfo.emplid < partid) {
        myTopic = myInfo.emplid + "_" + partid;
      } else {
        myTopic = partid + "_" + myInfo.emplid;
      }
    } else if (connType === constants.GROUP_CHAT) {
      myTopic = partid;
    } else {
      return null;
    }

    return myInfo.coid + constants.TOPIC_MSG + "/" + connType + "/" + myTopic;
  }

  function _createMQTTClient() {
    var options = {
      keepalive: 60,
      reconnectPeriod: 3000,
      connectTimeout: 30 * 1000,
    };

    var client = mqtt.connect(constants.MQTT_URL, options);
    client.on('connect', _mqttConnected);
    client.on('message', _mqttReceived);

    return client;
  }

  function _mqttConnected() {
    // topic array: presence, msg, {publisher}
    var topicArray = [myInfo.coid + constants.TOPIC_PRESENCE_ALL,
      myInfo.coid + constants.TOPIC_MSG + "/+/+",
      myInfo.coid + constants.TOPIC_MSG + "/+/+/" + myInfo.emplid
    ];
    console.log('_mqttConnected! topicArray:%s', topicArray.toString());
    myInfo.client.subscribe(topicArray);

    // presence/online
    myInfo.client.publish(myInfo.coid + constants.TOPIC_PRESENCE_ONLINE, myInfo.emplid.toString());
  }

  function _mqttReceived(topic, payload) {
    var payloadStr = payload.toString();
    console.log('_mqttReceived topic:%s, msg:%s, %s', topic, payloadStr, new Date().format("HH:mm:ss"));

    myInfo.recvCallback(myInfo.emplid, topic, payloadStr);
  }

  function _generateMsgPayloadStr(chatType, receiver, msg) {
    /*
      msgPayload = {
        chatType:  // 채팅 타입 (client 담당)
        coid:      // company id (client 담당)
        publisher: // 메시지 발신자 (client 담당)
        receiver:  // 메시지 수신자, direct인 경우 peer id, group인 경우 group chat id (client 담당)
        lastmsgid: // 이전 마지막 msg id. (pubreq 담당)
        msgid:     // DB 저장될 msg id. (pubreq 담당)
        time:      // 메시지 발신 시간 (pubreq 담당)
        msg:       // 발신 메시지 (client 담당)
      }
    */
    var msgPayload = {
      chatType: chatType,
      coid: myInfo.coid,
      publisher: myInfo.emplid,
      receiver: receiver,
      msg: msg
    };

    return JSON.stringify(msgPayload);
  }

  function sendDirectMsg(receiver, msg) {
    var topic = _getTopic(constants.DIRECT_CHAT, receiver) + constants.TOPIC_PUBREQ;
    var msgPayloadStr = _generateMsgPayloadStr(constants.DIRECT_CHAT, receiver, msg);
    myInfo.client.publish(topic, msgPayloadStr);

    console.log('sendDirectMsg topic:%s, msg:%s', topic, msgPayloadStr);
  }

  function getMyInfo() {
    return myInfo;
  }

  return {
    getMyInfo: getMyInfo,

    configMyInfo: configMyInfo,
    // initClient: initClient,
    sendDirectMsg: sendDirectMsg
  };
})();

module.exports = chat;

@@ -1979,20 +1979,16 @@
 %25s, msg:
-%25s, 
 %25s', top
@@ -2005,39 +2005,8 @@
 dStr
-, new Date().format(%22HH:mm:ss%22)
 );%0A%0A
@@ -3116,154 +3116,47 @@
 %0A%0A  
-function getMyInfo() %7B%0A    return myInfo;%0A  %7D%0A%0A  return %7B%0A    getMyInfo: getMyInfo,%0A%0A    configMyInfo: configMyInfo,%0A    // initClient: initClient
+return %7B%0A    configMyInfo: configMyInfo
 ,%0A  

remove unncessary function & test code