local this = {}

function this:init()
  this.super.init(self)
  self.data = {
    multiDungeonInfoDic = {},
    curMultiDungeonInfo = {},
    matchTeamSyncReason = 0,
    matchTeamData = {},
    matchTeamInviteData = {},
    matchedReadyData = {},
    inviteWaitDic = {}
  }
end

function this:getMultiDungeonInfo(dungeonId)
  return self.data.multiDungeonInfoDic[dungeonId] or {}
end

function this:getCurMultiDungeonInfo()
  return self.data.curMultiDungeonInfo
end

function this:getUseTime(info)
  return info.end_time - info.start_time
end

function this:getDungeonStar(info)
  if not table.isEmpty(info) then
    return L_CommonUtil.numberToBool(info.star)
  end
  return {}
end

function this:getMatchTeamData()
  return self.data.matchTeamData
end

function this:getMatchTeamDataMembers()
  return self.data.matchTeamData.members or {}
end

function this:getMatchTeamDataMemberByPid(playerUid)
  local members = self:getMatchTeamDataMembers()
  for i, v in ipairs(members) do
    if v.pid == playerUid then
      return v
    end
  end
end

function this:getMatchTeamMemberByPos(pos)
  local members = self:getMatchTeamDataMembers()
  for i, v in ipairs(members) do
    if v.position == pos then
      return v.pid
    end
  end
end

function this:getMatchTeamDataSyncReason()
  return self.data.matchTeamSyncReason
end

function this:getMatchTeamInviteData()
  return self.data.matchTeamInviteData
end

function this:getMatchedReadyData()
  return self.data.matchedReadyData
end

function this:getMatchedReadyDataMembers()
  return self.data.matchedReadyData.members or {}
end

function this:getMatchedReadyMemberByPid(pid)
  local members = self:getMatchedReadyDataMembers()
  for i, v in pairs(members) do
    if v.pid == pid then
      return v
    end
  end
  return {}
end

function this:getInviteWaitDic()
  local toRemove = {}
  for i, v in pairs(self.data.inviteWaitDic) do
    if v.start_time + L_GameConstTpl:getValue("MULIT_DUNGEON_FRIEND_READYTIME") <= L_TimeUtil.getServerTime() then
      table.insert(toRemove, i)
    else
      local node, index = table.ipairsFind(self:getMatchTeamDataMembers(), function(item)
        return item.pid == v.pid
      end)
      if index then
        table.insert(toRemove, i)
      end
    end
  end
  for i, v in ipairs(toRemove) do
    self.data.inviteWaitDic[v] = nil
  end
  return self.data.inviteWaitDic
end

function this:getInviteWaitByPid(playerUid)
  local members = self:getInviteWaitDic()
  for i, v in pairs(members) do
    if v.pid == playerUid then
      return v
    end
  end
end

function this:getMatchStatus()
  local teamData = self:getMatchTeamData()
  local readyData = self:getMatchedReadyData()
  if not math.isEmpty(readyData.matched_time) then
    return L_MultiDungeonConst.MatchStatus.Matched
  elseif math.isEmpty(readyData.matched_time) and not math.isEmpty(teamData.match_start_time) then
    return L_MultiDungeonConst.MatchStatus.Matching
  elseif not table.isEmpty(teamData) then
    return L_MultiDungeonConst.MatchStatus.Prepare
  else
    return L_MultiDungeonConst.MatchStatus.None
  end
end

return this
