local ChatMomentManager = BaseClass("ChatMomentManager")
local getMomentCd = 10000
local ChatMomentData = require("Chat.Model.ChatMomentData")

function ChatMomentManager:__init()
  self.momentFollowDic = {}
  self.momentFollowList = {}
  self.allianceMomentDataList = {}
  self.followMomentDataList = {}
  self.momentRoom = {}
  self.followList = {}
  self.allianceMomentTime = 0
  self.followMomentTime = 0
  self.redDotDic = {}
  self.msgRedDot = 0
  self.momentIsOpen = false
end

function ChatMomentManager:GetMomentIsOpen()
  return self.momentIsOpen
end

function ChatMomentManager:SetMomentIsOpen(isOpen)
  self.momentIsOpen = isOpen
end

function ChatMomentManager:InitMomentRedDot()
  ChatManager2:GetInstance().Net:SendMessage(ChatMsgDefines.MomentUnreadMsg, MomentType.Alliance)
  ChatManager2:GetInstance().Net:SendMessage(ChatMsgDefines.MomentUnreadMsg, MomentType.Follow)
  ChatManager2:GetInstance().Net:SendMessage(ChatMsgDefines.MomentUnReadTipsCount)
  UIUtil.GetPlayerInfoShowByUid(LuaEntry.Player.uid)
end

function ChatMomentManager:AddFollow(info)
  if not info then
    return
  end
  self.momentFollowDic[info.followeeId] = {
    isFollow = info.isFollow
  }
  if self.firstFoolwDic and self.firstFoolwDic[info.followeeId] and info.isFollow then
    self.firstFoolwDic[info.followeeId].isFirst = false
  end
  EventManager:GetInstance():Broadcast(ChatEventEnum.CHAT_MOMENT_RESF_FOLLOW_STATE, {
    uid = info.followeeId,
    isFollow = info.isFollow
  })
end

function ChatMomentManager:RemoveFollow(uid)
  if not uid then
    return
  end
  if self.momentFollowDic[uid] then
    self.momentFollowDic[uid] = nil
    EventManager:GetInstance():Broadcast(ChatEventEnum.CHAT_MOMENT_RESF_FOLLOW_STATE, {uid = uid, isFollow = false})
  end
end

function ChatMomentManager:GetMomentFollowInfo(uid)
  return self.momentFollowDic[uid]
end

function ChatMomentManager:AddFollowList(infoList)
  for i = #infoList, 1, -1 do
    self:AddFollow(infoList[i])
  end
end

function ChatMomentManager:RemoveFollowList(infoList)
  if not infoList then
    return
  end
  local isOne = #infoList == 1
  for i = #infoList, 1, -1 do
    self:RemoveFollow(infoList[i], isOne)
  end
  if not isOne then
    EventManager:GetInstance():Broadcast(ChatEventEnum.CHAT_MOMENT_RESF_FOLLOW_STATE_LIST)
  end
end

function ChatMomentManager:GetNewMoments(momentType)
  local lastTime = 0
  if momentType == MomentType.Alliance then
    lastTime = self.allianceMomentTime
  elseif momentType == MomentType.Follow then
    lastTime = self.followMomentTime
  end
  local curTime = UITimeManager:GetInstance():GetServerTime()
  if curTime - lastTime > getMomentCd then
    if momentType == MomentType.Alliance then
      self.allianceMomentTime = curTime
    elseif momentType == MomentType.Follow then
      self.followMomentTime = curTime
    end
  end
end

function ChatMomentManager:OnPushMsg(redType, senderUid)
  if redType == "ALLIANCE_MOMENTS" then
    if senderUid == LuaEntry.Player.uid then
      return
    end
    redType = MomentType.Alliance
  elseif redType == "FOLLOW_MOMENTS" then
    redType = MomentType.Follow
  elseif redType == "WRITE_TIMELINE_FINISHED" then
    if senderUid == LuaEntry.Player.uid then
      EventManager:GetInstance():Broadcast(ChatEventEnum.CHAT_MOMENT_MYSEND)
      return
    end
  else
    redType = MomentType.Notice
  end
  self:SetMomentMsgRedDot(redType, 1)
end

function ChatMomentManager:SetMomentMsgRedDot(redType, number)
  local group
  if redType == MomentType.Alliance then
    group = ChatGroupType.GROUP_ALLIANCE_FRIENDS_CIRCLE
  elseif redType == MomentType.Follow then
    group = ChatGroupType.GROUP_FOLLOWEE_CIRCLE_COMMENT_ROOM
  end
  local oldNumber
  if group then
    oldNumber = self.redDotDic[group]
    self.redDotDic[group] = number
    local moment = self:GetMomentData(group)
    if moment then
      moment.redDotCount = self.redDotDic[group]
    end
  else
    oldNumber = self.redDotDic[redType]
    self.redDotDic[redType] = number
  end
  if oldNumber ~= number then
    EventManager:GetInstance():Broadcast(ChatEventEnum.CHAT_MOMENT_NOTICE_REDDOT, redType)
  end
end

function ChatMomentManager:GetRoomId(group)
  return group .. "_" .. LuaEntry.Player.uid .. group
end

function ChatMomentManager:CreateMomentRoom(roomId, group)
  return ChatMomentData.New(roomId, group)
end

function ChatMomentManager:ClearMomentRoom(group)
  if self.momentRoom[group] then
    self.momentRoom[group].msgs = {}
    self.momentRoom[group].groupParams = nil
  end
end

function ChatMomentManager:GetMomentData(group)
  if group ~= ChatGroupType.GROUP_ALLIANCE_FRIENDS_CIRCLE and group ~= ChatGroupType.GROUP_FOLLOWEE_CIRCLE_COMMENT_ROOM and group ~= ChatGroupType.GROUP_ALL_MOMENT then
    return
  end
  if not self.momentRoom[group] then
    local room = self:CreateMomentRoom(self:GetRoomId(group), group)
    self.momentRoom[group] = room
    if self.redDotDic[group] then
      room.redDotCount = self.redDotDic[group]
    end
  end
  return self.momentRoom[group]
end

function ChatMomentManager:AddMomentDatas(group, table)
  local chatData
  local data = self:GetMomentData(group)
  local msgs = table.all_moment
  data.groupParams = table.groupParams
  for i, v in pairs(msgs) do
    chatData = ChatInterface.getRoomMgr():CreateChatMessage()
    chatData:onParseServerData(msgs[i])
    data:__addChatData(chatData)
  end
  EventManager:GetInstance():Broadcast(ChatEventEnum.CHAT_MOMENT_MSG_UPDATE, group)
end

function ChatMomentManager:GetMomentFollowList()
  return self.followList
end

function ChatMomentManager:AddMomentFollowList(list)
  if list then
    for i = 1, #list do
      table.insert(self.followList, #self.followList + 1, list[i])
    end
  end
  EventManager:GetInstance():Broadcast(ChatEventEnum.CHAT_MOMENT_FOLLW_LIST)
end

function ChatMomentManager:ClearMomentList()
  self.followList = {}
  EventManager:GetInstance():Broadcast(ChatEventEnum.CHAT_MOMENT_FOLLW_LIST)
end

function ChatMomentManager:GetMomentChatData(roomId, seqId)
  for i, room in pairs(self.momentRoom) do
    for i, chatData in pairs(room.msgs) do
      if chatData.roomId == roomId and chatData.seqId == seqId then
        return chatData
      end
    end
  end
end

function ChatMomentManager:DeleteMomentMessage(roomId, seqId)
  local chatData
  for k, room in pairs(self.momentRoom) do
    for i = #room.msgs, 1, -1 do
      chatData = room.msgs[i]
      if chatData.roomId == roomId and chatData.seqId == seqId then
        table.remove(room.msgs, i)
        EventManager:GetInstance():Broadcast(EventId.ChatDeleteMessage, room.group)
        return
      end
    end
  end
end

function ChatMomentManager:GetMomentRoomData(roomId)
  for i, room in pairs(self.momentRoom) do
    for i, chatData in pairs(room.msgs) do
      if chatData.roomId == roomId then
        return ChatInterface.getRoomMgr():CreateChatRoomData(roomId, ChatGroupType.GROUP_FRIENDS_CIRCLE_ROOM)
      end
    end
  end
end

function ChatMomentManager:GetRedDot(type)
  return self.redDotDic[type] or 0
end

function ChatMomentManager:SetFirstFollow(uid, isFirst)
  if not self.firstFoolwDic then
    self.firstFoolwDic = {}
  end
  self.firstFoolwDic[uid] = {
    isFirst = not isFirst
  }
end

function ChatMomentManager:GetMomentServerByGroup(chatGroup)
  local roomData = self:GetMomentData(chatGroup)
  local param = {
    groupParams = roomData and roomData.groupParams,
    idDesc = true,
    groupType = chatGroup
  }
  if chatGroup == ChatGroupType.GROUP_FOLLOWEE_CIRCLE_COMMENT_ROOM then
    ChatManager2:GetInstance().Net:SendMessage(ChatMsgDefines.MomentGetFollowCircle, param)
  else
    param.roomId = ChatInterface.getAllianceRoomId()
    ChatManager2:GetInstance().Net:SendMessage(ChatMsgDefines.MomentGetFollowCircle, param)
  end
end

function ChatMomentManager:GetFirstFollow(uid)
  if not self.firstFoolwDic then
    return
  end
  return self.firstFoolwDic[uid]
end

function ChatMomentManager:GetIsMomentBody(post)
  if post == PostType.FriendsCirleBody or post == PostType.FriendsCirleBodyHasIcon then
    return true
  end
end

function ChatMomentManager:GetIsMomentData(post)
  if self:GetIsMomentBody(post) or post == PostType.Chat_Moment then
    return true
  end
end

return ChatMomentManager
