local this = class("cellSortFilterGroup", G_UIModuleBase)
local _systemFiltertypeTpl = L_GameTpl:getSystemFiltertypeTpl()
local _systemSorttypeTpl = L_GameTpl:getSystemSorttypeTpl()

function this.bind()
  return {
    txt_title = nil,
    go_content = true,
    isOn_arrow = true,
    list_option = {
      moduleName = "pages/sortFilter/cellSortFilterOption"
    }
  }
end

function this.methods()
  return {
    onClick_title = function(self)
      self.bind.go_content = not self.bind.go_content
      self.bind.isOn_arrow = self.bind.go_content
      L_GameUtil.forceRebuildLayout(self.bindComponents.rect_cellSortFilterGroup)
      self:emit("onClick_title", self.uiBinding, self.bind.go_content)
    end,
    list_option = {
      onClick_select = function(self, bind)
        local limitReached = #self._selectValueList >= self.bind.selectLimit
        local hasGoActive = bind.go_active
        if limitReached and self.bind.selectLimit > 1 and not hasGoActive then
          return
        end
        if hasGoActive then
          if self.bind.sortOrFilter == L_SortFilterConst.SortOrFilter.Sort and self.bind.selectLimit == 1 and self.bind.defaultValue ~= nil then
            return
          end
          local node, index = table.ipairsFind(self._selectValueList, function(v)
            return v == bind.id
          end)
          table.remove(self._selectValueList, index)
        else
          if limitReached and self.bind.selectLimit == 1 then
            self._selectValueList = {}
          end
          table.insert(self._selectValueList, bind.id)
        end
        self:setOptionListActive(self._selectValueList)
      end
    }
  }
end

function this:open()
  self:initOptionList()
  self:setOptionListActive(self.bind.selectValue)
end

function this:initOptionList()
  local tmp = {}
  if self.bind.sortOrFilter == L_SortFilterConst.SortOrFilter.Filter then
    local tpl_filterType = _systemFiltertypeTpl:getTplById(self.bind.type)
    local idKey = _systemFiltertypeTpl:getFilterKey(tpl_filterType)
    local nameKey = _systemFiltertypeTpl:getFilterName(tpl_filterType)
    local iconKey = _systemFiltertypeTpl:getIconDisplay(tpl_filterType)
    local needIcon = not string.isEmpty(iconKey)
    local allContentTpl = L_SortFilterManager:getAllFilterContentTplByType(self.bind.type)
    for i, v in pairs(allContentTpl) do
      local name = L_Config:provider(v[nameKey])
      local imgPath
      if needIcon then
        imgPath = v[iconKey]
      end
      table.insert(tmp, {
        id = v[idKey],
        name = name,
        img_icon = imgPath,
        txt_name = name
      })
    end
  else
    for i, type in ipairs(self.bind.sortTypeData) do
      local tpl_filterType = _systemSorttypeTpl:getTplById(type)
      local name = _systemSorttypeTpl:getSortTypeName(tpl_filterType)
      table.insert(tmp, {
        id = type,
        name = name,
        img_icon = _systemSorttypeTpl:getIcon(tpl_filterType),
        txt_name = name
      })
    end
  end
  self.bind.list_option:clear()
  self.bind.list_option:insert_array(tmp)
  L_GameUtil.forceRebuildLayout(self.bindComponents.rect_list_option)
  L_GameUtil.forceRebuildLayout(self.bindComponents.rect_cellSortFilterGroup)
end

function this:setOptionListActive(selectList)
  local defaultList = self.bind.defaultValue ~= nil and {
    self.bind.defaultValue
  } or {}
  self._selectValueList = selectList or defaultList
  for i, v in ipairs(self.modules.list_option) do
    local node, index = table.ipairsFind(self._selectValueList, function(value)
      return value == v.bind.id
    end)
    v:setActive(index ~= nil)
    v:setSortIndex(self.bind.sortOrFilter == L_SortFilterConst.SortOrFilter.Sort and self.bind.selectLimit ~= 1 and index ~= nil, index)
  end
end

function this:getSelectValueList()
  if not table.isEmpty(self._selectValueList) then
    return self._selectValueList
  end
end

return this
