local this = class("pageEmotionCollectEdit", G_UIPageBase)
local _gameConstTpl = L_GameTpl:getGameConstTpl()
local _emojiTpl = L_GameTpl:getChatEmojiTpl()
local CHAT_EMOJI_COLLECT_NUM_MAX = _gameConstTpl:getValue("CHAT_EMOJI_COLLECT_NUM_MAX")

function this:ctor(...)
  this.super.ctor(self, ...)
end

function this.bind()
  return {
    scrollList_emotion = {
      moduleName = "pages/chat/cellEmotion"
    },
    txt_myCollect = "",
    txt_maxNum = tostring(CHAT_EMOJI_COLLECT_NUM_MAX),
    module_commonTipBoard = L_Const.ModuleInfo.CommonTipBoard
  }
end

function this.methods()
  return {
    onClick_close = function(self)
      L_UI:close(self.pageName)
    end,
    onClick_reset = function(self)
      for i = 1, CHAT_EMOJI_COLLECT_NUM_MAX do
        self.data.selectOrder2ShowIndexDic[i] = -1
      end
      self:initPage()
    end,
    onClick_save = function(self)
      local emojiIdList = {}
      for _, showIndex in pairs(self.data.selectOrder2ShowIndexDic) do
        if showIndex ~= -1 then
          local emojiId = self.data.emojiIdList[showIndex]
          table.insert(emojiIdList, emojiId)
        end
      end
      L_ChatStore:req_collectChatEmojiChange(emojiIdList)
      L_UI:close(self.pageName)
    end,
    scrollList_emotion = {
      onClick_select = function(self, bind)
        local showIndex = bind.showIndex
        local ifActive = not bind.go_selectFrame
        local insertLocation = self:getInsertLocationOfSelectDic()
        local selectOrder = insertLocation
        if ifActive then
          if selectOrder == false then
            return
          else
            self.data.selectOrder2ShowIndexDic[selectOrder] = showIndex
          end
        else
          for selectOrder, emojiShowIndex in pairs(self.data.selectOrder2ShowIndexDic) do
            if emojiShowIndex == showIndex then
              self.data.selectOrder2ShowIndexDic[selectOrder] = -1
            end
          end
        end
        self.bind.scrollList_emotion:change(showIndex, {
          go_selectFrame = ifActive,
          go_selectBg = ifActive,
          go_numBg = ifActive,
          txt_selectId = tostring(selectOrder)
        })
        self.bind.txt_myCollect = tostring(self:getSelectEmotionNum())
      end
    }
  }
end

function this:preOpen(options)
  this.super.preOpen(self, options)
  self:initData()
  self:initPage()
end

function this:initData()
  self.data = {
    emojiIdList = {},
    selectOrder2ShowIndexDic = {}
  }
  for i = 1, CHAT_EMOJI_COLLECT_NUM_MAX do
    self.data.selectOrder2ShowIndexDic[i] = -1
  end
  for index, emojiId in ipairs(L_ChatStore:getEmojiCollectedList()) do
    table.insert(self.data.emojiIdList, emojiId)
    self.data.selectOrder2ShowIndexDic[index] = index
  end
  for _, EmojiIdOwnedList in pairs(L_ChatStore:getEmojiOwnedDic()) do
    for _, emojiId in ipairs(EmojiIdOwnedList) do
      if table.indexOf(self.data.emojiIdList, emojiId) == false then
        table.insert(self.data.emojiIdList, emojiId)
      end
    end
  end
end

function this:initPage()
  local tmp = {}
  for showIndex, emojiId in pairs(self.data.emojiIdList) do
    local tpl = _emojiTpl:getTplById(emojiId)
    local selectOrder = self:getSelectOrderOfTargetShowIndex(showIndex)
    table.insert(tmp, {
      isInPageEdit = true,
      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,
      go_selectFrame = selectOrder ~= false,
      go_selectBg = selectOrder ~= false,
      go_numBg = selectOrder ~= false,
      txt_selectId = tostring(selectOrder),
      showIndex = showIndex
    })
  end
  self.bind.scrollList_emotion:clear()
  self.bind.scrollList_emotion:insert_array(tmp)
  self.bind.txt_myCollect = tostring(self:getSelectEmotionNum())
end

function this:getInsertLocationOfSelectDic()
  for location, selectEmojiIndex in pairs(self.data.selectOrder2ShowIndexDic) do
    if selectEmojiIndex == -1 then
      return location
    end
  end
  return false
end

function this:getSelectOrderOfTargetShowIndex(targetShowIndex)
  for selectOrder, showIndex in pairs(self.data.selectOrder2ShowIndexDic) do
    if showIndex == targetShowIndex then
      return selectOrder
    end
  end
  return false
end

function this:getSelectEmotionNum()
  local selectEmotionNum = 0
  for _, showIndex in pairs(self.data.selectOrder2ShowIndexDic) do
    if showIndex ~= -1 then
      selectEmotionNum = selectEmotionNum + 1
    end
  end
  return selectEmotionNum
end

return this
