local this = class("modulePetRelease", G_UIModuleBase)

function this.bind()
  return {
    selectedNum = "",
    list_dropdown = {
      moduleName = "modulePages/sortFilter/cellSortFilterDropdown"
    },
    txt_dropDown = "",
    toggleArrow = false,
    dropDownContent = false,
    curBoxIndex = "",
    curBoxName = "",
    module_pageTabSlider = {
      moduleName = "modulePages/modulePageTabSlider"
    }
  }
end

function this.methods()
  return {
    onClick_filter = function(self)
      L_UI:open("pagePetReleaseQuickSelect", {
        currentBox = self.curBox,
        callback = function(pets)
          self:setSelectedPets(pets)
        end
      })
    end,
    onClick_filterClear = function(self)
      L_PetStore:resetSelectGuids()
    end,
    onClick_releaseConfirm = function(self)
      local pets = L_PetStore:getSelectGuids()
      if #pets == 0 then
        L_FlyMsgManager:showNormalMsg(L_WordsTpl:getValue("ui_pet_release_pet_empty"))
        return
      end
      L_UI:open("pagePetReleaseConfirm", {
        pets = table.clone(pets)
      })
    end,
    onClick_showDropDownContent = function(self)
      self:refreshFilterActive()
      self.bind.dropDownContent = not self.bind.dropDownContent
      self.bind.toggleArrow = not self.bind.toggleArrow
    end,
    list_dropdown = {
      onClick_select = function(self, bind)
        self.parent.filter = bind.index
        self.bind.dropDownContent = false
        self.bind.txt_dropDown = L_WordsTpl:getValue(bind.type)
        self.bind.toggleArrow = false
        self.parent:refreshAllBox()
      end
    },
    dropDownContentBgOnClick = function(self)
      self.bind.dropDownContent = false
    end
  }
end

function this:setSelectedPets(pets)
  if pets == 0 then
    L_FlyMsgManager:showNormalMsg(L_WordsTpl:getValue("ui_pet_release_quick_empty"))
    return
  end
  local guids = {}
  for _, v in pairs(pets) do
    table.insert(guids, v.guid)
  end
  if L_PetReleaseManager:dealQuickSelect(guids) then
    L_FlyMsgManager:showNormalMsg(L_WordsTpl:getValue("ui_pet_release_quick_filter"))
  else
    L_FlyMsgManager:showNormalMsg(L_WordsTpl:getValue("ui_pet_release_quick_empty"))
  end
end

function this:open()
  self:refreshSelectedNum()
  L_PetStore:listenCallFunc(L_PetStore.event.setSelectReleasePet, self.onEvent_setSelectReleasePet, self)
end

function this:close()
  L_PetStore:unListenCallFunc(L_PetStore.event.setSelectReleasePet, self.onEvent_setSelectReleasePet, self)
end

function this:onEvent_setSelectReleasePet()
  self:initDropDown()
  self:refreshSelectedNum()
end

function this:refreshSelectedNum()
  local selectedGuids = L_PetStore:getSelectGuids() or {}
  self.bind.selectedNum = tostring(table.count(selectedGuids))
end

function this:setCurBox(boxId, boxIndexStr, boxName)
  self.curBox = boxId
  self.bind.curBoxIndex = boxIndexStr
  self.bind.curBoxName = boxName
end

function this:setTabSlider(curIndex, loopListLength)
  local modulePageTabSlider = self.modules.module_pageTabSlider
  modulePageTabSlider:setPageIndex(curIndex, loopListLength)
end

function this:initSortFilterModule()
  self.petSortFilter = L_SortFilterManager:createSortFilter(L_SystemConst.enum.petRelease, function()
  end, function()
    self:dealQuickFilter()
  end)
end

function this:dealQuickFilter()
  self:refreshFilterData()
  if #self._petTotalDataList == 0 then
    L_FlyMsgManager:showNormalMsg(L_WordsTpl:getValue("ui_pet_release_quick_empty"))
    return
  end
  local guids = {}
  for _, v in pairs(self._petTotalDataList) do
    table.insert(guids, v.guid)
  end
  if L_PetReleaseManager:dealQuickSelect(guids) then
    L_FlyMsgManager:showNormalMsg(L_WordsTpl:getValue("ui_pet_release_quick_filter"))
  else
    L_FlyMsgManager:showNormalMsg(L_WordsTpl:getValue("ui_pet_release_quick_empty"))
  end
end

function this:refreshFilterData()
  self.petSortFilter:bindModule(self.modules.quickSelectFilter)
  self._isPetBoxMode = self.petSortFilter.filterKeyDic[L_SortFilterConst.FilterType.QiBoBoxMode] ~= nil
  local allPet = L_PetStore:getPetList()
  if self._isPetBoxMode then
    allPet = {}
    local boxPets = L_PetStore:getPetBoxList(self.data.curIndex)
    for _, guid in ipairs(boxPets) do
      table.insert(allPet, L_PetStore:getPetItem(guid))
    end
  end
  self._petTotalDataList = {}
  for i, pet in pairs(allPet) do
    table.insert(self._petTotalDataList, {
      itemType = L_Const.resType.pet,
      itemId = pet.id,
      guid = pet.guid
    })
  end
  self.petSortFilter:sortFilterList(self._petTotalDataList)
end

function this:initDropDown()
  local tmp = {}
  for index, type in ipairs(self.parent.filterType) do
    table.insert(tmp, {
      index = index,
      type = type,
      txt_name = L_WordsTpl:getValue(type),
      go_icon = false
    })
  end
  self.bind.txt_dropDown = tmp[self.parent.filter].txt_name
  self.bind.list_dropdown:clear()
  self.bind.list_dropdown:insert_array(tmp)
end

function this:refreshFilterActive()
  for i = 1, #self.bind.list_dropdown do
    local index = self.bind.list_dropdown:getValue(i, "index")
    if index == self.parent.filter then
      self.bind.list_dropdown:getItemCls(i):setActive(true)
    else
      self.bind.list_dropdown:getItemCls(i):setActive(false)
    end
  end
end

return this
