local this = {}

function this:req_modifyName(name, callback)
  local data = {name = name}
  L_Net:sendMessage(MsgGenCode.CSProtoChangeName, data, function(rspData, errorCode)
    if errorCode == L_Const.errorCode.ErrCodeSucc then
      self.data.baseInfo.name = name
      self:call(self.event.refreshBaseInfo)
      if callback then
        callback()
      end
    elseif errorCode == L_Const.errorCode.ErrCodeInvalidNick then
      L_FlyMsgManager:showNormalMsg(L_WordsTpl:getValue("notice_idcard_4"))
    end
  end)
end

function this:req_modifySign(sign, callback)
  local data = {sign = sign}
  L_Net:sendMessage(MsgGenCode.CSProtoChangeSign, data, function(rspData, errorCode)
    if errorCode == L_Const.errorCode.ErrCodeSucc then
      self.data.baseInfo.sign = sign
      self:call(self.event.refreshBaseInfo)
      if callback then
        callback()
      end
    elseif errorCode == L_Const.errorCode.ErrCodeInvalidNick then
      L_FlyMsgManager:showNormalMsg(L_WordsTpl:getValue("notice_idcard_4"))
    end
  end)
end

function this:req_modifyDisplayHero(hero, callback)
  local data = {hero = hero}
  L_Net:sendMessage(MsgGenCode.CSProtoChangeShowHero, data, function(rspData, errorCode)
    if errorCode == L_Const.errorCode.ErrCodeSucc then
      self:modifyDisplayHero(hero)
      self:call(self.event.refreshBaseInfo)
      if callback then
        callback(errorCode)
      end
    end
  end)
end

function this:req_modifyDisplayPet(pets, callback)
  local data = {pet = pets}
  L_Net:sendMessage(MsgGenCode.CSProtoChangeShowPet, data, function(rspData, errorCode)
    if errorCode == L_Const.errorCode.ErrCodeSucc then
      self:modifyDisplayPet(pets)
      self:call(self.event.refreshBaseInfo)
      if callback then
        callback(errorCode)
      end
    end
  end)
end

function this:req_CSProtoChangeBirthday(month, day, callback)
  local data = {month = month, day = day}
  L_Net:sendMessage(MsgGenCode.CSProtoChangeBirthday, data, function(rspData, errorCode)
    if errorCode == L_Const.errorCode.ErrCodeSucc then
      self.data.baseInfo.birthday = data
      self:call(self.event.refreshBaseInfo)
      if callback then
        callback(errorCode)
      end
    end
  end)
end

function this:req_buyStamina(data, callback)
  L_Net:sendMessage(MsgGenCode.CSProtoBuyStamina, data, function(rspData, errorCode)
  end)
end

function this:req_getPlayerTitle(callback)
  L_Net:sendMessage(MsgGenCode.CSProtoTitleGetList, nil, function(rspData, errorCode)
    if errorCode == L_Const.errorCode.ErrCodeSucc and callback then
      callback(rspData, errorCode)
    end
  end)
end

function this:req_changePlayerTitle(data, callback)
  L_Net:sendMessage(MsgGenCode.CSProtoTitleChange, data, function(rspData, errorCode)
    if errorCode == L_Const.errorCode.ErrCodeSucc and callback then
      callback(rspData, errorCode)
    end
  end)
end

function this:req_syncMove(data, callback)
  if data.pos then
    data.pos.x = data.pos.x * 100
    data.pos.y = data.pos.y * 100
    data.pos.z = data.pos.z * 100
  end
  if data.angle then
    data.angle = data.angle * 100
  end
  local params = {}
  params.isRequest = false
  params.isMask = false
end

function this:req_CSProtoWorldMapPlayerStatus(status, arg, callback)
  if status == L_Const.WorldMapPlayerStatusType.WMPST_MOUNT and not arg then
    arg = L_MountStore:getEquipMountId()
  end
  local data = {status = status, arg = arg}
  L_Net:sendMessage(MsgGenCode.CSProtoWorldMapPlayerStatus, data, function(rspData, errorCode)
    if callback then
      callback(rspData, errorCode)
    end
  end)
end

function this:req_syncPose(poseData, isSingle, callback)
  local data = {}
  if isSingle then
    data = {
      pose_id = poseData.poseId
    }
  else
    data = poseData
  end
end

function this:req_replyPose(isAccept, callback)
  local data = {
    u32 = isAccept == true and 1 or 0
  }
  L_Net:sendMessage(MsgGenCode.CSProtoWorldMapPoseReply, data, function(rspData, errorCode)
    if errorCode == L_Const.errorCode.ErrCodeSucc and callback then
      callback()
    end
  end)
end

function this:req_cancelPose(callback)
  L_Net:sendMessage(MsgGenCode.CSProtoWorldMapPoseCancel, nil, function(rspData, errorCode)
    if errorCode == L_Const.errorCode.ErrCodeSucc and callback then
      callback()
    end
  end)
end

function this:req_playerCustomDataUp(data, callback)
  L_Net:sendMessage(MsgGenCode.CSProtoPinchFaceDataUp, data, function(rsp, errcode)
    if callback ~= nil then
      callback(rsp, errcode)
    end
  end)
end

function this:req_playerRevive(callback)
  L_Net:sendMessage(MsgGenCode.CSProtoWorldMapPlayerRevive, nil, function(rspData, errorCode)
    if errorCode == 0 and callback then
      callback()
    end
  end)
end

function this:rsp_syncPlayerInfo(data)
  local baseInfo = data.basic_info
  if not table.isEmpty(baseInfo) then
    self:syncBaseInfo(baseInfo)
  end
  local modInfo = data.basic_info and data.basic_info.wardrobe
  if not table.isEmpty(modInfo) then
    self:syncModInfo(modInfo)
  end
  local attrInfo = data.attr_infos and data.attr_infos.attrs
  if attrInfo then
    self:syncAttrInfo(attrInfo)
  end
end

function this:rsp_syncPlayerMapInfo(sMapInfo, isSend, stopTransfer)
  local mapInfo = self:getPlayerMapInfo()
  local move
  self:syncMountStatus(sMapInfo)
  self:syncSelectMountId(sMapInfo.mount)
  self:syncPlayerStatus(sMapInfo, isSend)
  if sMapInfo.move then
    move = sMapInfo.move[#sMapInfo.move]
    if move.pos then
      mapInfo.position.x = move.pos.x and move.pos.x * 0.01 or 0
      mapInfo.position.y = move.pos.y and move.pos.y * 0.01 or 0
      mapInfo.position.z = move.pos.z and move.pos.z * 0.01 or 0
      mapInfo.rotation.y = move.angle and move.angle * 0.01 or 0
    end
  end
  if sMapInfo.pose_id then
    mapInfo.pose_id = sMapInfo.pose_id
    mapInfo.poseInfo = sMapInfo.poseInfo
  end
end

function this:syncSelectMountId(mountId)
  local curMountId = self:getPlayerMount()
  local mapInfo = self:getPlayerMapInfo()
  mapInfo.mount = mountId
  if not math.isEmpty(mountId) and not math.isEmpty(curMountId) and mountId ~= curMountId then
    self:call(self.event.refreshPlayerMount, mountId)
  end
end

function this:syncMountStatus(nextMapInfo)
  local mapInfo = self:getPlayerMapInfo()
  local curMountStatus = mapInfo.mount_status
  local nextMountStatus = nextMapInfo.mount_status
  local curMountId = mapInfo.mount
  local nextMountId = nextMapInfo.mount
  if nextMountStatus ~= curMountStatus then
    mapInfo.mount_status = nextMountStatus
    if not math.isEmpty(curMountStatus) and curMountId == nextMountId then
      self:call(self.event.refreshMountStatus, nextMountStatus)
    end
    L_PlayerManager:refreshMountData()
  end
end

function this:syncPlayerStatus(nextMapInfo, isSend)
  local curMapInfo = self:getPlayerMapInfo()
  local curPlayerStatus = curMapInfo.status
  local nextPlayerStatus = nextMapInfo.status
  if nextPlayerStatus ~= curPlayerStatus then
    local mapMove
    if nextMapInfo.move then
      mapMove = nextMapInfo.move[#nextMapInfo.move]
    end
    local state = {
      cur = curPlayerStatus,
      next = nextPlayerStatus,
      move = mapMove,
      reason = nextMapInfo.reason
    }
    curMapInfo.status = nextPlayerStatus
    self:call(self.event.refreshPlayerStatus, state)
  end
end

function this:syncBaseInfo(baseInfo)
  self:refreshBaseInfo(baseInfo)
end

function this:syncModInfo(serverData)
  self:refreshModInfo(L_ModStore:serverDataToModAvatar(serverData))
end

function this:syncAttrInfo(attrInfo)
  local needRefreshStaminaTime = false
  for _, v in ipairs(attrInfo) do
    if v.attr_id == L_Const.currencyType.staminaRecover then
      needRefreshStaminaTime = true
    end
    self:refreshAttr(v)
  end
  if needRefreshStaminaTime then
    self:refreshStamina()
  end
  self:call(self.event.syncAttrInfoComplete)
end

function this:syncPlayerPos(data)
  if data == nil then
    return
  end
  local mainControl = L_FormationStore:getControl(L_FormationManager:getCurFormationType())
  for i, v in pairs(data.move or {}) do
    if v.uuid == mainControl then
      local mapInfo = self:getPlayerMapInfo()
      local move = v.info
      if move.pos then
        mapInfo.position.x = move.pos.x and move.pos.x * 0.01 or 0
        mapInfo.position.y = move.pos.y and move.pos.y * 0.01 or 0
        mapInfo.position.z = move.pos.z and move.pos.z * 0.01 or 0
        mapInfo.rotation.y = move.angle and move.angle * 0.01 or 0
      end
    end
  end
end

return this
