local this = class("pageMultiplySortFilter", G_UIPageBase)
local _systemUnlockTpl = L_GameTpl:getSystemUnlockTpl()
local _systemFilterTpl = L_GameTpl:getSystemFilterTpl()
local _systemFiltertypeTpl = L_GameTpl:getSystemFiltertypeTpl()

function this.bind()
  return {
    txt_title = "",
    list_optionGroup = {
      moduleName = "pages/sortFilter/cellSortFilterGroup"
    },
    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, v in pairs(self.modules.list_optionGroup) do
        local cell = v
        cell:setOptionListActive()
      end
    end,
    onClick_confirm = function(self)
      local valueList = {}
      if self._type == L_SortFilterConst.SortOrFilter.Sort then
        local tpl_sortFilter = _systemFilterTpl:getTplById(self._sortFilterId)
        local solidSortKey = _systemFilterTpl:getSolidSortKey(tpl_sortFilter)
        table.fill(valueList, solidSortKey)
      end
      for i, v in ipairs(self.modules.list_optionGroup) do
        if self._type == L_SortFilterConst.SortOrFilter.Sort then
          table.fill(valueList, v:getSelectValueList())
        else
          valueList[v.bind.type] = v:getSelectValueList()
          if table.isEmpty(valueList[v.bind.type]) then
            valueList[v.bind.type] = nil
          end
        end
      end
      if self._type == L_SortFilterConst.SortOrFilter.Sort then
        local tpl_sortFilter = _systemFilterTpl:getTplById(self._sortFilterId)
        local subSortKey = _systemFilterTpl:getSubSortKey(tpl_sortFilter)
        table.fill(valueList, subSortKey)
      end
      self._callback(valueList)
      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:preOpen(options)
  this.super.preOpen(self, options)
  self._type = options.type
  self._callback = options.callback
  local selectValue = options.selectValue or {}
  local tpl_sys = _systemUnlockTpl:getTplById(options.systemId)
  self._sortFilterId = _systemUnlockTpl:getFilterConfig(tpl_sys)
  local tpl_sortFilter = _systemFilterTpl:getTplById(self._sortFilterId)
  local tmp = {}
  if self._type == L_SortFilterConst.SortOrFilter.Sort then
    self.bind.txt_title = L_WordsTpl:getValue("notice_pageMultiplySortFilter_02")
    local sortType = _systemFilterTpl:getSortType(tpl_sortFilter)
    for index, typeData in pairs(sortType) do
      local selectType = {}
      for _, v in pairs(selectValue) do
        local node, __index = table.ipairsFind(typeData, function(value)
          return value == v
        end)
        if __index then
          table.insert(selectType, v)
        end
      end
      local sortLimit, isSelectFirst = _systemFilterTpl:getSortPicks(tpl_sortFilter, index)
      table.insert(tmp, {
        sortOrFilter = L_SortFilterConst.SortOrFilter.Sort,
        selectValue = selectType,
        selectLimit = sortLimit,
        sortTypeData = typeData,
        defaultValue = isSelectFirst and typeData[1] or nil,
        txt_title = _systemFilterTpl:getGroupName(tpl_sortFilter, index)
      })
    end
  else
    self.bind.txt_title = L_WordsTpl:getValue("notice_pageMultiplySortFilter_01")
    local filterConfig = _systemFilterTpl:getFilterConfig(tpl_sortFilter)
    for _, typeAndLimit in pairs(filterConfig) do
      local typeId, limit = table.unpack(typeAndLimit)
      local tpl_filterType = _systemFiltertypeTpl:getTplById(typeId)
      table.insert(tmp, {
        sortOrFilter = L_SortFilterConst.SortOrFilter.Filter,
        selectValue = table.clone(selectValue[typeId]) or {},
        selectLimit = limit,
        type = typeId,
        txt_title = _systemFiltertypeTpl:getFilterTypeName(tpl_filterType)
      })
    end
  end
  self.bind.list_optionGroup:clear()
  self.bind.list_optionGroup:insert_array(tmp)
  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
