local this = class("moduleChatEmotion", G_UIModuleBase)
local emojiGroupTpl = L_GameTpl:getChatEmojiGroupTpl()
local emojiTpl = L_GameTpl:getChatEmojiTpl()
local EMOTION_COLLECTIED_GROUP_ID = 1
local COLOR_GREY = C_LuaUtility.ParseHtmlStringColor("#8b8c8f")

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

function this.bind()
  return {
    go_buttonEdit = false,
    scrollList_emotionType = {
      moduleName = "pages/chat/cellEmotionType"
    },
    scrollList_emotion = {
      moduleName = "pages/chat/cellEmotion"
    }
  }
end

function this.methods()
  return {
    scrollList_emotionType = {
      onClick_select = function(self, bind)
        L_ReddotManager:markNew(L_ReddotManager.DotDef.NewEmoji, bind.type)
        if not bind.go_active then
          self.data.selectType = bind.type
          self:initEmotionList()
          self:setEmotionTypeListActive()
        end
      end
    },
    scrollList_emotion = {
      onClick_select = function(self, bind)
        local callback = function()
          self.parent.bind.go_moduleEmoticon = false
          self.parent.bind.go_chatMask = false
          self.parent.bind.color_btnEmotion = COLOR_GREY
        end
        if bind.isEditButton == true then
          L_UI:open("pageEmotionCollectEdit")
        else
          if self.parent.data.selectPlayerUid ~= nil then
            L_ChatStore:req_sendChatInfo(self.parent.data.selectPlayerUid, L_Const.ChatInfoType.CHAT_EMOJI, tostring(bind.id), callback)
          end
          if self.parent.data.selectMeetingChat == true then
            L_ChatStore:req_meetingChat(L_Const.ChatInfoType.CHAT_EMOJI, tostring(bind.id), callback)
          end
        end
      end
    }
  }
end

function this:open(options)
  L_ChatStore:listenCallFunc(L_ChatStore.event.refreshEmojiInfo, self.onEvent_refreshEmojiInfo, self)
  this.super.open(self, options)
  self:initModule()
end

function this:close(options)
  L_ChatStore:unListenCallFunc(L_ChatStore.event.refreshEmojiInfo, self.onEvent_refreshEmojiInfo)
  this.super.close(self, options)
end

function this:initModule()
  self:initEmotionTypeList()
  self.data.selectType = self.data.selectType or self.bind.scrollList_emotionType:getValue(1, "type")
  self:initEmotionList()
  self:setEmotionTypeListActive()
end

function this:initEmotionTypeList()
  local tmp = {}
  table.insert(tmp, {
    type = EMOTION_COLLECTIED_GROUP_ID,
    img_icon = emojiGroupTpl:getIcon(emojiGroupTpl:getTplById(EMOTION_COLLECTIED_GROUP_ID)),
    txt_name_normal = emojiGroupTpl:getNameById(EMOTION_COLLECTIED_GROUP_ID)
  })
  for groupId, _ in pairs(L_ChatStore.data.emojiOwnedDic) do
    local tpl = emojiGroupTpl:getTplById(groupId)
    table.insert(tmp, {
      type = groupId,
      img_icon = emojiGroupTpl:getIcon(tpl),
      txt_name_normal = emojiGroupTpl:getName(tpl)
    })
  end
  table.sort(tmp, function(a, b)
    return a.type < b.type
  end)
  self.bind.scrollList_emotionType:clear()
  self.bind.scrollList_emotionType:insert_array(tmp)
end

function this:initEmotionList()
  if self.data.selectType == EMOTION_COLLECTIED_GROUP_ID then
    self:initEmotionCollectedList()
  else
    self:initEmotionGroupList()
  end
end

function this:initEmotionCollectedList()
  local emojiIdList = L_ChatStore:getEmojiCollectedList()
  local tmp = {}
  table.insert(tmp, {
    isEditButton = true,
    txt_name = "",
    img_emotion = "UI/Texture/Emoji/tex_chat_bth_edit.png",
    frameCount = 1,
    frameCountPerRow = 1,
    frameCountPerCol = 1,
    frameAnimParamsComplete = true
  })
  for _, emojiId in pairs(emojiIdList) do
    local tpl = emojiTpl:getTplById(emojiId)
    table.insert(tmp, {
      isEditButton = false,
      id = emojiTpl:getId(tpl),
      txt_name = emojiTpl:getName(tpl),
      img_emotion = emojiTpl:getIcon(tpl),
      frameCount = emojiTpl:getMaxFrame(tpl),
      frameCountPerRow = emojiTpl:getFramePerRow(tpl),
      frameCountPerCol = emojiTpl:getFramePerCol(tpl),
      frameAnimParamsComplete = true
    })
  end
  self.bind.scrollList_emotion:clear()
  self.bind.scrollList_emotion:insert_array(tmp)
end

function this:initEmotionGroupList()
  local data = emojiTpl:getDataByGroup(self.data.selectType)
  local tmp = {}
  for i, v in pairs(data) do
    table.insert(tmp, {
      isEditButton = false,
      id = emojiTpl:getId(v),
      txt_name = emojiTpl:getName(v),
      img_emotion = emojiTpl:getIcon(v),
      frameCount = emojiTpl:getMaxFrame(v),
      frameCountPerRow = emojiTpl:getFramePerRow(v),
      frameCountPerCol = emojiTpl:getFramePerCol(v),
      frameAnimParamsComplete = true
    })
  end
  self.bind.scrollList_emotion:clear()
  self.bind.scrollList_emotion:insert_array(tmp)
end

function this:setEmotionTypeListActive()
  for i = 1, #self.bind.scrollList_emotionType do
    local active = false
    if self.bind.scrollList_emotionType:getValue(i, "type") == self.data.selectType then
      active = true
    end
    self.bind.scrollList_emotionType:change(i, {go_select = active})
  end
end

function this:onEvent_refreshEmojiInfo()
  self:initEmotionTypeList()
  self:initEmotionList()
  self:setEmotionTypeListActive()
end

return this
