local this = class("friendManager")

function this:requestBaseInfo(types, players, callback)
  if table.isEmpty(players) then
    if callback then
      callback()
    end
    return
  end
  local data = {types = types, player_ids = players}
  L_FriendStore:req_queryPlayerBaseInfo(data, function(rspData, errCode)
    if table.isEmpty(rspData) then
      L_FlyMsgManager:showNormalMsg(L_WordsTpl:getValue("notice_friend_player_unknown"))
    elseif errCode == L_Const.errorCode.ErrCodeSucc and callback then
      callback()
    end
  end)
end

function this:requestBaseInfoAll(playerId, callback)
  local players = {playerId}
  local types = {
    L_Const.PlayerBasicQueryType.PBQT_ALL
  }
  self:requestBaseInfo(types, players, function()
    if callback then
      callback()
    end
  end)
end

function this:viewPlayerCard(playerId)
  local reqCount = 0
  local endDo = function()
    local baseInfo = L_FriendStore:getPlayerBaseInfo(playerId)
    L_PhotoManager:loadHeadTex(L_FriendStore:getPlayerAvatarsHead(baseInfo), function(tex)
      reqCount = reqCount + 1
      if reqCount == 2 then
        L_UI:open("pagePlayerCard", {uid = playerId})
      end
    end)
    L_PhotoManager:loadHeadTex(L_FriendStore:getPlayerAvatars(baseInfo, L_Const.avatarTextureIndex.painting), function(tex)
      reqCount = reqCount + 1
      if reqCount == 2 then
        L_UI:open("pagePlayerCard", {uid = playerId})
      end
    end)
  end
  if playerId == L_PlayerStore:getPlayerId() then
    endDo()
  else
    self:requestBaseInfoAll(playerId, endDo)
  end
end

function this:operationPlayer(opType, op, playerId, callback)
  local baseInfo = L_FriendStore:getPlayerBaseInfo(playerId)
  local data = {
    optype = opType,
    op = op,
    id = {
      pid = playerId,
      zoneid = L_FriendStore:getPlayerZoneId(baseInfo)
    }
  }
  L_FriendStore:req_operationPlayer(data, function(errorCode)
    if errorCode == L_Const.errorCode.ErrCodeSucc then
      if opType == L_Const.friendOpType.friend and op == L_Const.friendOp.delete or opType == L_Const.friendOpType.blackList and op == L_Const.friendOp.add then
        L_ChatStore:rsp_unmarkChat({del = playerId})
      end
      if callback then
        callback()
      end
    elseif errorCode == L_Const.errorCode.ErrCodeTargetGetYourApply then
      L_FlyMsgManager:showNormalMsg(L_WordsTpl:getValue("notice_friend_repeatapply"))
    elseif errorCode == L_Const.errorCode.ErrCodeYouBanTarget then
      L_FlyMsgManager:showNormalMsg(L_WordsTpl:getValue("notice_friend_blacklist"))
    elseif errorCode == L_Const.errorCode.ErrCodeTargetBanYou then
      L_FlyMsgManager:showNormalMsg(L_WordsTpl:getValue("notice_friend_blacklist_otherside"))
    elseif errorCode == L_Const.errorCode.ErrCodeHadFriend then
      L_FlyMsgManager:showNormalMsg(L_WordsTpl:getValue("notice_friend_alreadybefriends"))
    elseif errorCode == L_Const.errorCode.ErrCodeTargetFriendApplyNumMax then
      L_FlyMsgManager:showNormalMsg(L_WordsTpl:getValue("notice_friend_applylist_limit"))
    elseif errorCode == L_Const.errorCode.ErrCodeTargetFriendNumMax then
      L_FlyMsgManager:showNormalMsg(L_WordsTpl:getValue("notice_friend_list_limit_otherside"))
    elseif errorCode == L_Const.errorCode.ErrCodeFriendNumMax then
      L_FlyMsgManager:showNormalMsg(L_WordsTpl:getValue("notice_friend_list_limit"))
    end
  end)
end

function this:modifyFriendMark(playerUid)
  local systemId = L_SystemConst.enum.renameFriend
  if L_SystemBreakManager:checkBanSystemAndTip(systemId) then
    return
  end
  local data = {
    txtTitle = L_WordsTpl:getValue("ui_modifyRemarks"),
    placeHolder = L_WordsTpl:getValue("ui_importRemarks"),
    limit = 5,
    textBoxType = L_Const.CommonTextBoxType.Rename,
    confirmCallback = function(txt)
      if string.gsub(txt, " ", "") ~= txt then
        L_FlyMsgManager:showNormalMsg(L_WordsTpl:getValue("ui_hintRemark_01"))
        return
      end
      local data = {id = playerUid, remark = txt}
      L_FriendStore:req_modifyFriendMark(data, function(errCode)
        if errCode == L_Const.errorCode.ErrCodeSucc then
          L_FlyMsgManager:showNormalMsg(L_WordsTpl:getValue("ui_modifyRemarksSucceed"))
        elseif errCode == L_Const.errorCode.ErrCodeInvalidNick then
          L_FlyMsgManager:showNormalMsg(L_WordsTpl:getValue("notice_idcard_4"))
        end
      end)
    end
  }
  L_GameUtil.showNormalTextBox(data)
end

function this:getFriendShowName(playerUid)
  local baseInfo = L_FriendStore:getPlayerBaseInfo(playerUid)
  local name = L_FriendStore:getPlayerName(baseInfo)
  local friend = L_FriendStore:getPlayerItem(L_FriendStore:getFriendList(), playerUid)
  if not string.isEmpty(friend:getFriendMark()) then
    name = string.format("%s(%s)", name, friend:getFriendMark())
  end
  return name
end

function this:addFriend(playerUid)
  if table.count(L_FriendStore:getFriendList()) >= L_GameTpl:getGameConstTpl():getFriendLimit() then
    L_FlyMsgManager:showNormalMsg(L_WordsTpl:getValue("notice_friend_list_limit"))
    return
  end
  self:operationPlayer(L_Const.friendOpType.application, L_Const.friendOp.add, playerUid, function()
    L_FlyMsgManager:showNormalMsg(L_WordsTpl:getValue("notice_friend_sending_success"))
  end)
end

return this
