local this = class("pagePetReleaseQuickSelect", G_UIPageBase)
local petReleaseTypeTpl = L_GameTpl:getPetReleaseTypeTpl()
local petReleaseSubtypeTpl = L_GameTpl:getPetReleaseSubtypeTpl()

function this.bind()
  return {
    txt_title = "",
    list_optionGroup = {
      moduleName = "pages/pet/cellReleaseFilterGroup"
    },
    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)
      self:initPage()
    end,
    onClick_confirm = function(self)
      self:dealFilter()
      L_UI:close(self.pageName)
    end,
    onValueChanged_scrollView = function(self, _)
      if not self._lockBackVertical then
        self._backScrollNormalize = nil
      end
    end,
    list_optionGroup = {
      onClick_title = function(self, uiBinding, open)
        L_GameUtil.forceRebuildLayout(self.bindComponents.rect_Content)
        self._backScrollNormalize = self.bindComponents.scrollView_group.verticalNormalizedPosition
        FrameScheduler.add(function()
          self:refreshNormalizePos(uiBinding, open)
        end, 1, uiBinding)
      end
    }
  }
end

function this:dealFilter()
  local petItems = table.clone(L_PetStore:getPetList())
  for _, v in ipairs(self.modules.list_optionGroup) do
    local subId = v:getSelectSubId()
    if not math.isEmpty(subId) then
      local tpl = petReleaseSubtypeTpl:getTplById(subId)
      local typeId = petReleaseSubtypeTpl:getTypeId(tpl)
      if self["dealFilter" .. typeId] then
        local param = petReleaseSubtypeTpl:getParam(tpl)
        petItems = self["dealFilter" .. typeId](self, petItems, param)
      end
    end
  end
  if self.callback then
    self.callback(petItems)
  end
end

function this:dealFilter101(petItems)
  local pets = {}
  for _, v in pairs(petItems) do
    if v.box_id // 100 == self.currentBox then
      table.insert(pets, v)
    end
  end
  return pets
end

function this:dealFilter102(petItems)
  return petItems
end

function this:dealFilter201(petItems)
  local pets = {}
  for _, v in pairs(petItems) do
    if not v:getIsSpecialPet() then
      table.insert(pets, v)
    end
  end
  return pets
end

function this:dealFilter202(petItems)
  local pets = {}
  for _, v in pairs(petItems) do
    if v:getIsSpecialPet() then
      table.insert(pets, v)
    end
  end
  return pets
end

function this:dealFilter300(petItems, param)
  local pets = {}
  for _, v in pairs(petItems) do
    if not math.isEmpty(param) then
      local _, grade = L_PetStore:getPetGradeNew(v.guid)
      if param >= grade then
        table.insert(pets, v)
      end
    end
  end
  return pets
end

function this:preOpen(options)
  this.super.preOpen(self, options)
  self.callback = options.callback
  self.currentBox = options.currentBox
  self.bind.txt_title = L_WordsTpl:getValue("notice_soldier_selectFast")
  self:initPage()
end

function this:initPage()
  local data = petReleaseTypeTpl:getSortedData()
  self.bind.list_optionGroup:clear()
  self.bind.list_optionGroup:insert_array(data)
  L_GameUtil.forceRebuildLayout(self.bindComponents.rect_Content)
end

function this:refreshNormalizePos(cellUiBinding, isInfoOpen)
  local scroll = self.bindComponents.scrollView_group
  if not scroll then
    return
  end
  if isInfoOpen then
    local isVisible = L_EffectUtil.isElementVerticalVisible(cellUiBinding, scroll.viewport, scroll.content)
    local jumpY = self:getJumpY(cellUiBinding)
    local contenSize = scroll.content.sizeDelta
    local viewPortRectSize = scroll.viewport.rect.size
    if not isVisible then
      self._lockBackVertical = true
      scroll.verticalNormalizedPosition = math.clamp(1 - jumpY / (contenSize.y - viewPortRectSize.y), 0, 1)
      FrameScheduler.add(function()
        self._lockBackVertical = false
      end, 1, cellUiBinding)
    end
  else
    if self._backScrollNormalize then
      local y = self._backScrollNormalize
      scroll.verticalNormalizedPosition = math.clamp(y, 0, 1)
    end
    self._backScrollNormalize = nil
  end
end

function this:getJumpY(targetElement)
  local elementRect = targetElement:GetComponent(typeof(C_RectTransform))
  local elementAnchoredPos = elementRect.anchoredPosition
  return -elementAnchoredPos.y - 11 - elementRect.rect.height / 2
end

return this
