local this = class("pageFriends", G_UIPageBase)
local _moduleName = {
  friend = "friendList",
  apply = "friendApplication",
  recent = "recentTeam"
}
local MenuType = {
  friendList = 1,
  friendApply = 2,
  recent = 3
}
local TYPE_ICON = {
  [MenuType.friendList] = "Page/BSFriend/tex_friend_icon_haoyou",
  [MenuType.friendApply] = "Page/BSFriend/tex_friend_icon_shenqing",
  [MenuType.recent] = "Page/BSFriend/tex_friend_icon_lianji"
}

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

function this.bind()
  return {
    moduleMenu = {
      moduleName = "modulePages/moduleMenu"
    },
    moduleName_modulePlayerList = "",
    module_playerList = {
      type = "toggleModule",
      friendList = {
        assetName = "UI/Pages/Friend/moduleFriendList",
        moduleName = "pages/friend/moduleFriendList"
      },
      friendApplication = {
        assetName = "UI/Pages/Friend/moduleFriendApplication",
        moduleName = "pages/friend/moduleFriendApplication"
      },
      recentTeam = {
        assetName = "UI/Pages/Friend/moduleRecentTeam",
        moduleName = "pages/friend/moduleRecentTeam"
      }
    }
  }
end

function this.methods()
  return {
    onClick_back = function(self)
      L_UI:close(self.pageName)
    end,
    onClick_root = function(self)
      L_UI:backToRoot()
    end
  }
end

function this:preOpen(options)
  this.super.preOpen(self, options)
  L_FriendStore:clearPlayerInfoList()
  L_FriendStore:listenCallFunc(L_FriendStore.event.refreshIdList, self.onEvent_refreshIdList, self)
  L_ChatStore:listenCallFunc(L_ChatStore.event.refreshChatShield, self.onEvent_refreshIdList, self)
  self:initPage()
end

function this:close(options)
  this.super.close(self, options)
  L_FriendStore:unListenCallFunc(L_FriendStore.event.refreshIdList, self.onEvent_refreshIdList)
  L_ChatStore:unListenCallFunc(L_ChatStore.event.refreshChatShield, self.onEvent_refreshIdList)
end

function this:initPage()
  self:updateSelectPage(MenuType.friendList)
  self:initModuleMenu()
end

function this:initModuleMenu()
  local typeList = {
    MenuType.friendList,
    MenuType.friendApply,
    MenuType.recent
  }
  local menuList = {}
  for i, v in pairs(typeList) do
    local data = {
      type = v,
      icon = TYPE_ICON[v]
    }
    if v == MenuType.friendApply then
      data.reddotKey = L_ReddotManager.DotDef.Friend
    end
    table.insert(menuList, data)
  end
  local func = function(selectType)
    self:updateSelectPage(selectType)
  end
  self.modules.moduleMenu:initModule(menuList, self.data.selectMenuType, func)
end

function this:updateSelectPage(newSelect)
  if self.data.selectMenuType == newSelect then
    return
  end
  if newSelect == MenuType.friendList then
    self.bind.moduleName_modulePlayerList = _moduleName.friend
    self.modules.module_playerList.friendList:initModule()
  elseif newSelect == MenuType.friendApply then
    self.bind.moduleName_modulePlayerList = _moduleName.apply
    self.modules.module_playerList.friendApplication:initModule(self.data.selectMenuType)
  elseif newSelect == MenuType.recent then
    self.bind.moduleName_modulePlayerList = _moduleName.recent
    self.modules.module_playerList.recentTeam:initModule()
  end
  self.data.selectMenuType = newSelect
end

function this:onEvent_refreshIdList()
  if self.bind.moduleName_modulePlayerList == _moduleName.friend then
    self.modules.module_playerList.friendList:initModule()
  elseif self.bind.moduleName_modulePlayerList == _moduleName.apply then
    self.modules.module_playerList.friendApplication:initModule()
  elseif self.bind.moduleName_modulePlayerList == _moduleName.recent then
    self.modules.module_playerList.recentTeam:initModule()
  end
end

return this
