local this = class("moduleFriendApplication", G_UIModuleBase)

function this:ctor(...)
  this.super.ctor(self, ...)
  self.data = {}
end

function this.bind()
  return {
    txt_applicationNum = "",
    go_none = false,
    go_line = false,
    scrollList_player = {
      moduleName = "pages/Friend/cellFriendPlayer"
    }
  }
end

function this.methods()
  return {
    onClick_search = function(self)
      local searchUid = self.bindComponents.input_search.text
      if string.isEmpty(searchUid) then
        return
      end
      local uid = tonumber(searchUid)
      if uid == L_PlayerStore:getPlayerId() then
        L_FlyMsgManager:showNormalMsg(L_WordsTpl:getValue("ui_moduleFriendApplication_05"))
        return
      end
      L_FriendManager:viewPlayerCard(uid)
    end,
    scrollList_player = {
      onClick_head = function(self, uid, position)
        local dropdowns = {
          L_DropdownsManager.option.viewInfo,
          L_DropdownsManager.option.insertToBlackList,
          L_DropdownsManager.option.report
        }
        L_DropdownsManager:showPlayerDropdowns(position, dropdowns, uid)
      end,
      onClick_agree = function(self, uid)
        if table.count(L_FriendStore:getFriendList()) >= L_GameTpl:getGameConstTpl():getFriendLimit() then
          L_FlyMsgManager:showNormalMsg(L_WordsTpl:getValue("ui_moduleFriendApplication_02"))
          return
        end
        L_FriendManager:operationPlayer(L_Const.friendOpType.friend, L_Const.friendOp.add, uid, function()
          L_FlyMsgManager:showNormalMsg(L_WordsTpl:getValue("ui_moduleFriendApplication_03"))
        end)
      end,
      onClick_refuse = function(self, uid)
        L_FriendManager:operationPlayer(L_Const.friendOpType.application, L_Const.friendOp.delete, uid, function()
          L_FlyMsgManager:showNormalMsg(L_WordsTpl:getValue("ui_moduleFriendApplication_04"))
        end)
      end
    }
  }
end

function this:open(options)
  this.super.open(self, options)
end

function this:close(options)
  this.super.close(self, options)
end

function this:initModule(sou)
  if sou then
    if sou < 2 then
      self.bindComponents.ani_moduleFriendApplication:Play("anim_friend_list_in_up")
    elseif 2 < sou then
      self.bindComponents.ani_moduleFriendApplication:Play("anim_friend_list_in_down")
    end
  end
  local playerList = L_FriendStore:getFriendApplicationList()
  local players = {}
  for _, v in pairs(playerList) do
    table.insert(players, v:getPlayerUid())
  end
  local types = {
    L_Const.PlayerBasicQueryType.PBQT_NAME,
    L_Const.PlayerBasicQueryType.PBQT_FACE_MINI,
    L_Const.PlayerBasicQueryType.PBQT_OFFLINE_TIME,
    L_Const.PlayerBasicQueryType.PBQT_LEVEL,
    L_Const.PlayerBasicQueryType.PBQT_SIGN
  }
  L_FriendManager:requestBaseInfo(types, players, function()
    self:initFriendList()
    self.bind.txt_applicationNum = L_WordsTpl:getValue("ui_moduleFriendApplication_01", {
      [0] = table.count(L_FriendStore:getFriendApplicationList()),
      [1] = L_GameTpl:getGameConstTpl():getFriendApplicationLimit()
    })
    if table.count(L_FriendStore:getFriendApplicationList()) == 0 then
      self.bind.txt_applicationNum = ""
    end
    self.bind.go_none = table.isEmpty(L_FriendStore:getFriendApplicationList())
    self.bind.go_line = not table.isEmpty(L_FriendStore:getFriendApplicationList())
  end)
end

function this:initFriendList()
  local tmp = {}
  local player = L_FriendStore:getFriendApplicationList()
  for i, v in pairs(player) do
    local baseInfo = L_FriendStore:getPlayerBaseInfo(v:getPlayerUid())
    local online = L_FriendStore:getPlayerIsOnline(baseInfo)
    local busy = false
    local txt_state = L_TimeUtil.getOfflineTimeDisplay(L_FriendStore:getPlayerOnLineTime(baseInfo), L_FriendStore:getPlayerOffLineTime(baseInfo))
    if online and L_FriendStore:getPlayerIsBusy(baseInfo) then
      busy = true
      online = false
      txt_state = L_GameUtil.fillColor(L_WordsTpl:getValue("ui_moduleRecentTeam"), L_Const.colorHtml.yellow0)
    end
    local offLine = not online or not busy
    table.insert(tmp, {
      uid = v:getPlayerUid(),
      txt_name = L_FriendStore:getPlayerName(baseInfo),
      txt_level = L_WordsTpl:getValue("ui_moduleFriendList_02", {
        [0] = L_FriendStore:getPlayerLevel(baseInfo)
      }),
      txt_sign = L_FriendStore:getPlayerSign(baseInfo),
      go_state = true,
      txt_state = txt_state,
      state_online = online,
      state_offline = offLine,
      state_busy = busy,
      operate_friend = false,
      operate_apply = true,
      url_defaultHero = L_FriendStore:getPlayerAvatarsHead(baseInfo)
    })
  end
  table.sort(tmp, function(a, b)
    local friendItemA = L_FriendStore:getPlayerItem(L_FriendStore:getFriendApplicationList(), a.uid)
    local friendItemB = L_FriendStore:getPlayerItem(L_FriendStore:getFriendApplicationList(), b.uid)
    local timeA = friendItemA:getInsertTime()
    local timeB = friendItemB:getInsertTime()
    return timeA > timeB
  end)
  self.bind.scrollList_player:clear()
  self.bind.scrollList_player:insert_array(tmp)
end

return this
