import ConstMsgId from './ConstMsgId';
import Packet from '../net/Packet';
import Common from '../common/Common'
import Define from '../common/Define';
import DataModelManager from '../framework/DataModelManager';

let MsgTransfer = (
  function () {
    let encodeLoginMsg = function () {
      var playerInfoModel = DataModelManager.getModel('PlayerInfoModel');
      var selfUserInfo = playerInfoModel.getSelfWXPlayerInfo();
      if (arguments.length <= 2) {
        return;
      }
      var main = arguments[0];
      var sub = arguments[1];
      var len = arguments[2];
      var index = 2;
      if (!(main === ConstMsgId.MDM_KN_COMMAND && sub === ConstMsgId.SUB_KN_DETECT_SOCKET)) {
        console.log("encodemsg main:" + main + " sub:" + sub + " len:" + len);
      }
      var cmd_data = new ArrayBuffer(len);
      var cmd_view = new DataView(cmd_data);
      cmd_view.setUint16(index, len, Common.littleEndian);
      index += 2;
      cmd_view.setUint16(index, main, Common.littleEndian);
      index += 2;
      cmd_view.setUint16(index, sub, Common.littleEndian);
      index += 2;
      if (ConstMsgId.MDM_MB_LOGON === main) {
        if (ConstMsgId.SUB_MB_LOGON_WX === sub) {//微信code换openid
          var code = arguments[3];
          index = Common.putString(index, cmd_view, code);
        } else if (ConstMsgId.SUB_MB_LOGON_OTHERPLATFORM === sub) {

          if(selfUserInfo == null)
          {
            console.log("selfUserInfo is null:" );
            return;
          }

          console.log("selfUserInfo Len:" + JSON.stringify(selfUserInfo).length + " " + JSON.stringify(selfUserInfo));

          cmd_view.setUint16(index, Define.INVALID_WORD, Common.littleEndian);
          index += 2;
          cmd_view.setUint32(index, Common.version(7, 2, 0, 1), Common.littleEndian);
          index += 4;
          cmd_view.setUint8(index, Define.DEVICE_TYPE, Common.littleEndian);
          index += 1;
          if (CC_WECHATGAME) {
            cmd_view.setUint8(index, selfUserInfo.gender, Common.littleEndian);
          }
          else if(CC_JSB && cc.sys.OS_ANDROID === cc.sys.os)
          {
            cmd_view.setUint8(index, selfUserInfo.gender, Common.littleEndian);  
          }
           else {
            cmd_view.setUint8(index, Define.MYGENDER, Common.littleEndian);
          }
          index += 1;
          cmd_view.setUint8(index, Define.LogonMode.WeChatGame, Common.littleEndian);
          index += 1;

          index = Common.putString(index, cmd_view, playerInfoModel.getSelfOpenId(), Define.LEN_USER_UIN);
          if (CC_WECHATGAME) {
            index = Common.putString(index, cmd_view, selfUserInfo.nickName, Define.LEN_NICKNAME);
            index = Common.putString(index, cmd_view, selfUserInfo.nickName, Define.LEN_COMPELLATION);
          } else if(CC_JSB && cc.sys.OS_ANDROID === cc.sys.os)
          {

            index = Common.putString(index, cmd_view, selfUserInfo.nickName, Define.LEN_NICKNAME);
            index = Common.putString(index, cmd_view, selfUserInfo.nickName, Define.LEN_COMPELLATION);

          } else {
            index = Common.putString(index, cmd_view, Define.NICKNAME, Define.LEN_NICKNAME);
            index = Common.putString(index, cmd_view, Define.NICKNAME, Define.LEN_COMPELLATION);
          }


          index = Common.putString(index, cmd_view, JSON.stringify(selfUserInfo), Define.LEN_WX_USERINFO);
          index = Common.putString(index, cmd_view, Define.MachineId, Define.LEN_MACHINE_ID);
          index = Common.putString(index, cmd_view, Define.PhoneNumber, Define.LEN_MOBILE_PHONE);

        } else if (ConstMsgId.SUB_MB_LOGON_ACCOUNTS === sub) {
          cmd_view.setUint16(index, Define.INVALID_WORD, Common.littleEndian);
          index += 2;
          cmd_view.setUint32(index, Common.version(6, 7, 0, 1), Common.littleEndian);
          index += 4;
          cmd_view.setUint8(index, Define.DEVICE_TYPE, Common.littleEndian);
          index += 1;
          index = Common.putString(index, cmd_view, Define.MYPASSWORD, Define.LEN_MD5);
          index = Common.putString(index, cmd_view, Define.MYACCOUNT, Define.LEN_ACCOUNTS);
          index = Common.putString(index, cmd_view, Define.MachineId, Define.LEN_MACHINE_ID);
          index = Common.putString(index, cmd_view, Define.PhoneNumber, Define.LEN_MOBILE_PHONE);
        }
      }else if (ConstMsgId.MDM_GP_LOGON === main) { //封装新的登录结构体

        if (ConstMsgId.SUB_GR_LOGON_USERID === sub) {
          cc.log("封装新的登录消息结构体")

          var id=arguments[3];
          id = parseInt(id)

          var password=arguments[4];
          cc.log("id,password",id,password);
          cmd_view.setUint32(index, Common.version(6, 7, 0, 1), Common.littleEndian);
          index += 4;
          cmd_view.setUint32(index, Common.version(6, 7, 0, 1), Common.littleEndian);
          index += 4;
          cmd_view.setUint32(index, Common.version(6, 7, 0, 1), Common.littleEndian);
          index += 4;
          cmd_view.setUint32(index, id, Common.littleEndian);
          index += 4;
          index = Common.putString(index, cmd_view,password,Define.LEN_MD5);
          index = Common.putString(index, cmd_view, Define.MYACCOUNT, Define.LEN_MACHINE_ID);
          index = Common.putString(index, cmd_view, Define.MachineId, Define.LEN_MACHINE_ID);
          cmd_view.setUint16(index,Define.GAMEID, Common.littleEndian);
          index += 2;
        }

      } 
      else if (ConstMsgId.MDM_GP_USER_SERVICE === main) {
        if (ConstMsgId.SUB_GP_GROWLEVEL_QUERY === sub) {
          var playerInfo = playerInfoModel.getSelfPlayerInfo();
          cmd_view.setUint32(index, playerInfo.dwUserID, Common.littleEndian);
          index += 4;
          index = Common.putString(index, cmd_view, Define.MYPASSWORD, Define.LEN_MD5);
          index = Common.putString(index, cmd_view, Define.MachineId, Define.LEN_MACHINE_ID);
        }
      }
      return cmd_data;
    };

    let encodemsg = function () {
      var playerInfoModel = DataModelManager.getModel('PlayerInfoModel');
      var configData = DataModelManager.getModel('ConfigData');
      var gameList = configData.getGameList();
      var player = playerInfoModel.getSelfPlayerInfo()
      if (arguments.length <= 2) {
        return
      }
      var main = arguments[0]
      var sub = arguments[1]
      var len = arguments[2]
      var index = 2
      if (!(main === ConstMsgId.MDM_KN_COMMAND && sub === ConstMsgId.SUB_KN_DETECT_SOCKET)) {
        console.log("encodemsg main:" + main + " sub:" + sub + " len:" + len)
      }

      var cmd_data = new ArrayBuffer(len)
      var cmd_view = new DataView(cmd_data)
      cmd_view.setUint16(index, len, Common.littleEndian)
      index += 2
      cmd_view.setUint16(index, main, Common.littleEndian)
      index += 2
      cmd_view.setUint16(index, sub, Common.littleEndian)
      index += 2
      if (ConstMsgId.MDM_GR_LOGON === main) {
        if (ConstMsgId.SUB_GR_LOGON_MOBILE === sub) {
          cmd_view.setUint16(index, Define.GAMEID, Common.littleEndian)
          index += 2
          cmd_view.setUint32(index,  Common.version(6, 7, 0, 1), Common.littleEndian)
          index += 4
          cmd_view.setUint8(index, Define.DEVICE_TYPE, Common.littleEndian)
          index += 1
          cmd_view.setUint16(index, 0x0011, Common.littleEndian)
          index += 2
          cmd_view.setUint16(index, 255, Common.littleEndian)
          index += 2
          cmd_view.setUint32(index, player.dwUserID, Common.littleEndian)
          index += 4
          index = Common.putString(index, cmd_view, player.szDynamicPass, 33)
          index = Common.putString(index, cmd_view, player.szRoomPasswd, 33)
          //index = Common.putString(index,cmd_view,"A501164B366ECFC9E249163873094D50",33)
          index = Common.putString(index, cmd_view, "", 33)
        }
        else if (ConstMsgId.SUB_GR_LOGON_USERID === sub) {
          cc.log("封装新的登录消息结构体")
          var id = arguments[3];
          id = parseInt(id)
          var password=arguments[4];
          cc.log("id,password",id,password);
          cmd_view.setUint32(index, Common.version(6, 7, 0, 1), Common.littleEndian);
          index += 4;
          cmd_view.setUint32(index, Common.version(6, 7, 0, 1), Common.littleEndian);
          index += 4;
          cmd_view.setUint32(index, Common.version(6, 7, 0, 1), Common.littleEndian);
          index += 4;
          cmd_view.setUint32(index, id, Common.littleEndian);
          index += 4;
          index = Common.putString(index, cmd_view,password,Define.LEN_MD5);
          index = Common.putString(index, cmd_view, Define.MYACCOUNT, Define.LEN_MACHINE_ID);
          index = Common.putString(index, cmd_view, Define.MachineId, Define.LEN_MACHINE_ID);
          cmd_view.setUint16(index,Define.GAMEID, Common.littleEndian);
          index += 2;
        }
        
      } else if (ConstMsgId.MDM_GR_USER === main) {
        if (ConstMsgId.SUB_GR_USER_SITDOWN === sub) {
          var table = arguments[3]
          var chair = arguments[4]
          var password = arguments[5]
          cmd_view.setUint16(index, table, Common.littleEndian)
          index += 2
          cmd_view.setUint16(index, chair, Common.littleEndian)
          index += 2
          if (password) {
            index = Common.putString(index, cmd_view, password, Define.LEN_PASSWORD)
          }
        } else if (ConstMsgId.SUB_GR_USER_STANDUP === sub) {
          var table = arguments[3]
          var chair = arguments[4]
          var bForce = arguments[5]
          cmd_view.setUint16(index, table, Common.littleEndian)
          index += 2
          cmd_view.setUint16(index, chair, Common.littleEndian)
          index += 2
          if (bForce === true) {
            cmd_view.setUint8(index, 1, Common.littleEndian)
            index += 1
          } else {
            cmd_view.setUint8(index, 0, Common.littleEndian)
            index += 1
          }
        } else if (ConstMsgId.SUB_GR_USER_CHAIR_INFO_REQ === sub) {
          var table = arguments[3]
          var chair = arguments[4]
          cmd_view.setUint16(index, table, Common.littleEndian)
          index += 2
          cmd_view.setUint16(index, chair, Common.littleEndian)
          index += 2
        }
      } else if (ConstMsgId.MDM_GF_FRAME === main) {
        //--场景规则
        if (ConstMsgId.SUB_GF_GAME_OPTION === sub) {
          cmd_view.setUint8(index, 0, Common.littleEndian)
          index += 1
          // dataBuffer:pushbyte(0)
          // dataBuffer:pushdword(appdf.VersionValue(6,7,0,1))
          // dataBuffer:pushdword(self._kindVersion)
          cmd_view.setUint32(index, Common.version(7, 2, 0, 1), Common.littleEndian)
          index += 4
          cmd_view.setUint32(index,  Common.version(7, 2, 0, 1), Common.littleEndian)
          index += 4
        }
      } else if (ConstMsgId.MDM_GF_GAME === main) {// game
        if (ConstMsgId.SUB_C_CALL_SCORE === sub) {// call score
          var score = arguments[3]
          cmd_view.setUint8(index, score, Common.littleEndian)
          index += 1
        } else if (ConstMsgId.SUB_C_OUT_CARD === sub) {
          var cards = arguments[3]
          var cardcount = cards.length
          cmd_view.setUint8(index, cardcount, Common.littleEndian)
          index += 1
          for (var i = 0; i < cardcount; i++) {
            var card = cards[i]
            cmd_view.setUint8(index, card, Common.littleEndian)
            index += 1
          }
        }
      }
      return cmd_data;
    };

    let decodemsg = function (wMainCmdID, wSubCmdID, buf) {
      if (ConstMsgId.MDM_MB_LOGON === wMainCmdID) {
        if (ConstMsgId.SUB_MB_LOGON_WX === wSubCmdID) {

        }
        else if (ConstMsgId.SUB_MB_LOGON_SUCCESS === wSubCmdID) {

        }
      }
      else if (ConstMsgId.MDM_MB_LOGON === wMainCmdID) {

      }
    };

    return {
      encodeLoginMsg: encodeLoginMsg,
      encodemsg: encodemsg,
      decodemsg: decodemsg,
    };
  }
)();

export default MsgTransfer;
