local this = class("chatManager")
local _worldCityTpl = L_GameTpl:getWorldCityTpl()
local CELL_TYPE = {
  text = 0,
  commonWords = 1,
  emotion = 2,
  time = 3,
  meeting = 4
}
local CELL_OWNER = {me = 1, you = 2}
local CELL_PARAM = {
  [CELL_TYPE.text] = {
    moduleName = "pages/Chat/cellChatMessage",
    identify = {
      [CELL_OWNER.me] = "cellMessage",
      [CELL_OWNER.you] = "cellMessageOpposite"
    }
  },
  [CELL_TYPE.emotion] = {
    moduleName = "pages/Chat/cellChatEmotion",
    identify = {
      [CELL_OWNER.me] = "cellMessageEmotion",
      [CELL_OWNER.you] = "cellMessageEmotionOpposite"
    }
  },
  [CELL_TYPE.commonWords] = {
    moduleName = "pages/Chat/cellChatMessage",
    identify = {
      [CELL_OWNER.me] = "cellMessage",
      [CELL_OWNER.you] = "cellMessageOpposite"
    }
  },
  [CELL_TYPE.time] = {
    moduleName = "pages/Chat/cellChatTime",
    identify = "cellMessageTime"
  },
  [CELL_TYPE.meeting] = {
    moduleName = "pages/Chat/cellChatTime",
    identify = "cellMessageMeeting"
  }
}

function this:getChatPlayerInfo(callback)
  local chatType = self:getCurChatType()
  local players = {}
  local conversationList = L_ChatStore:getConversationList()
  for i, v in pairs(conversationList) do
    table.insert(players, L_ChatStore:getPlayerUid(v))
  end
  local types = {
    L_Const.PlayerBasicQueryType.PBQT_NAME,
    L_Const.PlayerBasicQueryType.PBQT_FACE_MINI
  }
  L_FriendManager:requestBaseInfo(types, players, function()
    callback()
  end)
end

function this:getCurChatType()
  local sceneModule = L_WorldManager:getCurModule()
  if sceneModule == L_Const.worldModule.meeting then
    return L_Const.chatType.meeting
  else
    return L_Const.chatType.personal
  end
end

function this:getConversationList()
  local chatType = self:getCurChatType()
  local res = {}
  if chatType == L_Const.chatType.meeting then
    table.insert(res, {
      conversationType = L_Const.chatType.meeting,
      serverData = L_ChatStore:getMeetingConversation()
    })
  end
  local tmp = {}
  local conversationList = L_ChatStore:getConversationList()
  for i, v in pairs(conversationList) do
    table.insert(tmp, {
      conversationType = L_Const.chatType.personal,
      serverData = v
    })
  end
  table.sort(tmp, function(a, b)
    return a.serverData.upgrade > b.serverData.upgrade
  end)
  return table.fill(res, tmp)
end

function this:parseMsg(message)
  local isSender = L_ChatStore:getIsMessageSendByMyself(message)
  local msg = {
    identify = CELL_PARAM[L_ChatStore:getMessageType(message)].identify[isSender and 1 or 2],
    moduleName = CELL_PARAM[L_ChatStore:getMessageType(message)].moduleName,
    serverData = message
  }
  if isSender then
    msg.playerUid = L_PlayerStore:getPlayerId()
    msg.txt_name = L_PlayerStore:getPlayerName()
    msg.url_head = L_PlayerStore:getPlayerModHeadImgName()
  else
    local oppositeBaseInfo = L_FriendStore:getPlayerBaseInfo(L_ChatStore:getMessageSender(message))
    msg.playerUid = L_ChatStore:getMessageSender(message)
    msg.txt_name = L_FriendStore:getPlayerName(oppositeBaseInfo)
    msg.url_head = L_FriendStore:getPlayerAvatarsHead(oppositeBaseInfo)
  end
  if L_ChatStore:getMessageType(message) == L_Const.ChatInfoType.CHAT_EMOJI then
    msg.emotionData = L_ChatStore:getMessageContent(message)
  else
    msg.txt_message = L_ChatStore:getMessageContent(message)
  end
  return msg
end

function this:parseTimeMsg(lastMessage, curMessage)
  local timeCur = L_ChatStore:getMessageTime(curMessage)
  local timeLast = lastMessage and L_ChatStore:getMessageTime(lastMessage) or 0
  if not timeCur or 300 < timeCur - timeLast then
    local msg = {
      identify = CELL_PARAM[CELL_TYPE.time].identify,
      moduleName = CELL_PARAM[CELL_TYPE.time].moduleName,
      txt_time = L_TimeUtil.getChatDisplayTime(timeCur)
    }
    return msg
  end
end

function this:parseSwitchMeetingMsg(lastMessage, curMessage)
  local timeCur = L_ChatStore:getMessageTime(curMessage)
  local timeLast = lastMessage and L_ChatStore:getMessageTime(lastMessage) or 0
  local record = L_ChatStore:getMeetingSwitchLineRecord()
  local res = {}
  for i, v in ipairs(record) do
    if timeLast < v.time and timeCur >= v.time then
      table.insert(res, self:generateSwitchMeetingMsg(v))
    end
  end
  return res
end

function this:generateSwitchMeetingMsg(record)
  local sceneName = _worldCityTpl:getCity(_worldCityTpl:getTplById(record.mapId))
  local data = {
    identify = CELL_PARAM[CELL_TYPE.meeting].identify,
    moduleName = CELL_PARAM[CELL_TYPE.meeting].moduleName,
    txt_time = L_WordsTpl:getValue("ui_chatManager", {
      [0] = sceneName,
      [1] = record.lineId
    })
  }
  return data
end

return this
