local module = class("cellPetBox", G_UIModuleBase)

function module.bind()
  return {
    list_pet = {
      moduleName = "pages/pet/cellPixelPet"
    }
  }
end

function module.methods()
  return {}
end

function module:open()
  if L_DeviceTpl:getIsPc() then
    self.bindComponents.scrollView.enabled = false
  else
    self.bindComponents.scrollView.enabled = true
  end
  L_PetStore:listenCallFunc(L_PetStore.event.refreshNewPet, self.refreshInfo, self)
  L_PetStore:listenCallFunc(L_PetStore.event.refreshPet, self.refreshSinglePet, self)
  L_PetStore:listenCallFunc(L_PetStore.event.setCurPetId, self.onEvent_setCurPetId, self)
  L_PetStore:listenCallFunc(L_PetStore.event.refreshPetBox, self.onEvent_refreshPetBox, self)
  L_PetStore:listenCallFunc(L_PetStore.event.setSelectReleasePet, self.onEvent_setSelectReleasePet, self)
  L_PetStore:listenCallFunc(L_PetStore.event.refreshPetGeneMutation, self.onEvent_refreshPetGeneMutation, self)
end

function module:refresh()
  self:refreshInfo()
end

function module:refreshSinglePet(pet)
  self:refreshBoxList(pet.guid)
end

function module:onEvent_setCurPetId(guid)
  self:refreshBoxList(guid)
end

function module:onEvent_setSelectReleasePet(guid)
  self:refreshBoxList(guid)
end

function module:onEvent_refreshPetBox(boxMap)
  if boxMap[self.bind.boxIndex] then
    self:refreshInfo()
  end
end

function module:onEvent_refreshPetGeneMutation(guid)
  self:refreshBoxList(guid)
end

function module:refreshBoxList(guid)
  for i, v in pairs(self.modules.list_pet) do
    if not math.isEmpty(v.bind.guid) then
      local cell = v
      local select = guid == v.bind.guid
      if L_PetReleaseManager:getIsReleaseMode() then
        select = L_PetReleaseManager:isPetSelected(v.bind.guid)
      end
      cell:setGifPlay(select)
      if select and not L_PetReleaseManager:getIsReleaseMode() then
        L_ReddotManager:clearNew(L_ReddotManager.DotDef.PetNew, guid)
        local petItem = L_PetStore:getPetItem(guid)
        local hasNewGene = false
        if petItem.gene_infos and #petItem.gene_infos > 0 then
          for i, v in ipairs(petItem.gene_infos) do
            if L_PetStore:checkIsNewGene(guid, v.pos) then
              hasNewGene = true
              break
            end
          end
        end
        if not petItem:hasMutationGene() and not hasNewGene then
          local keyNewString = L_PetStore:getPetGeneMutationNewSaveKey(guid)
          C_PlayerPrefsUtility.SetInt(keyNewString, 0)
          L_ReddotManager:clearNew(L_ReddotManager.DotDef.PetGeneMutation, guid)
        end
      end
    end
  end
end

function module:refreshInfo()
  if not self.isBind then
    return
  end
  local onClick_Choose = function(cell)
    if L_PetReleaseManager:getIsReleaseMode() then
      local petId = cell.bind.guid
      L_PetReleaseManager:dealSelectPet(petId)
      return
    end
    if self.isDragging then
      return
    end
    local petId = cell.bind.guid
    if L_PetStore:getCurSelectPetId() ~= petId then
      local lastPetItem = L_PetStore:getPetItem(L_PetStore:getCurSelectPetId())
      if not math.isEmpty(L_PetStore:getCurSelectPetId()) and not lastPetItem:hasMutationGene() then
        local keyNewString = L_PetStore:getPetGeneMutationNewSaveKey(L_PetStore:getCurSelectPetId())
        C_PlayerPrefsUtility.SetInt(keyNewString, 0)
        L_ReddotManager:clearNew(L_ReddotManager.DotDef.PetGeneMutation, L_PetStore:getCurSelectPetId())
      end
      if lastPetItem.gene_infos and 0 < #lastPetItem.gene_infos then
        for i, v in ipairs(lastPetItem.gene_infos) do
          local keyString = L_PetStore:getPetGeneMutationNewSaveKeyByPos(L_PetStore:getCurSelectPetId(), v.pos)
          C_PlayerPrefsUtility.SetInt(keyString, 0)
        end
      end
      L_PetStore:setCurSelectPetId(petId)
      self.petId = petId
      local serverData = L_PetStore:getPetItem(cell.bind.guid)
      L_PetStore:playPetVoiceSound(serverData.id, L_PetConst.PetSystemVoiceType.CaptureSuccess)
    end
  end
  local boxIndex = self.bind.boxIndex
  local countList = L_PetStore:getPetBoxList(boxIndex)
  local data = {}
  if not table.isEmpty(countList) then
    for i, v in ipairs(countList) do
      local pet = L_PetStore:getPetItem(v)
      if not table.isEmpty(pet) then
        table.insert(data, pet)
      else
        table.insert(data, {})
      end
    end
  end
  if math.isEmpty(L_PetStore:getCurSelectPetId()) then
    local petGuid = L_PetStore:getFirstOrderPet()
    L_PetStore:setCurSelectPetId(petGuid)
  end
  local lst = {}
  for i = 1, #data do
    local cell = {}
    if not table.isEmpty(data[i]) then
      cell.choice = L_PetStore:getCurSelectPetId() == countList[i]
      cell.filterType = self.parent:getFilterType()
      cell.guid = countList[i]
      cell.itemType = L_Const.resType.pet
      cell.reddotName = string.format(L_ReddotManager.DotDef.PetNew, countList[i])
      cell.reddotGeneName = string.format(L_ReddotManager.DotDef.PetGeneMutation, countList[i])
      cell.callback = onClick_Choose
    end
    table.insert(lst, cell)
  end
  self.bind.list_pet:clear()
  self.bind.list_pet:insert_array(lst)
end

function module:close()
  if not math.isEmpty(L_PetStore:getCurSelectPetId()) then
    local petItem = L_PetStore:getPetItem(L_PetStore:getCurSelectPetId())
    if not petItem:hasMutationGene() then
      local keyString = L_PetStore:getPetGeneMutationNewSaveKey(L_PetStore:getCurSelectPetId())
      C_PlayerPrefsUtility.SetInt(keyString, 0)
      L_ReddotManager:clearNew(L_ReddotManager.DotDef.PetGeneMutation, L_PetStore:getCurSelectPetId())
    end
    if petItem.gene_infos and 0 < #petItem.gene_infos then
      for i, v in ipairs(petItem.gene_infos) do
        local keyString = L_PetStore:getPetGeneMutationNewSaveKeyByPos(L_PetStore:getCurSelectPetId(), v.pos)
        C_PlayerPrefsUtility.SetInt(keyString, 0)
      end
    end
  end
  L_PetStore:unListenCallFunc(L_PetStore.event.refreshNewPet, self.onEvent_refreshPet, self)
  L_PetStore:unListenCallFunc(L_PetStore.event.setCurPetId, self.onEvent_setCurPetId, self)
  L_PetStore:unListenCallFunc(L_PetStore.event.refreshPet, self.refreshSinglePet, self)
  L_PetStore:unListenCallFunc(L_PetStore.event.refreshPetBox, self.onEvent_refreshPetBox, self)
  L_PetStore:unListenCallFunc(L_PetStore.event.setSelectReleasePet, self.onEvent_setSelectReleasePet, self)
  L_PetStore:unListenCallFunc(L_PetStore.event.refreshPetGeneMutation, self.onEvent_refreshPetGeneMutation, self)
end

function module:setIsDragging(bool)
  self.isDragging = bool
end

return module
