local this = class("moduleSortFilter", G_UIModuleBase)
local _systemUnlockTpl = L_GameTpl:getSystemUnlockTpl()
local _systemFilterTpl = L_GameTpl:getSystemFilterTpl()
local _systemFiltertypeTpl = L_GameTpl:getSystemFiltertypeTpl()
local _systemSorttypeTpl = L_GameTpl:getSystemSorttypeTpl()
local PathSort = "UI/Atlas/SortFilterPage/tex_common_icon_px.png"
local PathFilter = "UI/Atlas/SortFilterPage/tex_common_icon_sx.png"

function this.bind()
  return {
    go_button = false,
    img_button = nil,
    go_bar = false,
    img_bar = nil,
    txt_bar = nil,
    go_dropdown = false,
    img_dropdown = nil,
    txt_dropdown = nil,
    go_dropdownContent = false,
    isOn_arrow = false,
    list_dropdown = {
      moduleName = "modulePages/sortFilter/cellSortFilterDropdown"
    },
    go_sort_reverse_btn = false
  }
end

function this.methods()
  return {
    onClick_button = function(self)
      self:openMultiplyPage(L_SortFilterConst.SortFilterStyle.Button)
    end,
    onClick_bar = function(self)
      self:openMultiplyPage(L_SortFilterConst.SortFilterStyle.Bar)
    end,
    onClick_dropdown = function(self)
      if self._dropDownShow then
        C_AudioManager.Play("Play_SFX_System_UI_General_Drop_Close")
      else
        C_AudioManager.Play("Play_SFX_System_UI_General_Drop_Open")
      end
      self:checkDropdownShow()
    end,
    onClick_reverse = function(self)
      self._isSortReverse = not self._isSortReverse
      self._sortCallback(self._sortKeyList, self._isSortReverse)
    end,
    list_dropdown = {
      onClick_select = function(self, bind)
        if not self._dropDownShow then
          return
        end
        self:checkDropdownShow()
        if self._sortStyle == L_SortFilterConst.SortFilterStyle.Dropdown then
          local sortType = _systemFilterTpl:getSortType(self._sortFilterTpl)[1]
          self._sortKeyList = {}
          local solidSortKey = _systemFilterTpl:getSolidSortKey(self._sortFilterTpl)
          table.fill(self._sortKeyList, solidSortKey)
          table.insert(self._sortKeyList, bind.type)
          for index, type in ipairs(sortType) do
            if type ~= bind.type then
              table.insert(self._sortKeyList, type)
            end
          end
          local subSortKey = _systemFilterTpl:getSubSortKey(self._sortFilterTpl)
          table.fill(self._sortKeyList, subSortKey)
          self._sortCallback(self._sortKeyList, self._isSortReverse)
        elseif self._filterStyle == L_SortFilterConst.SortFilterStyle.Dropdown then
          self._filterKeyDic = {
            [bind.type] = {
              bind.id
            }
          }
          self._filterCallback(self._filterKeyDic)
          self.bindComponents.ani_btn_filter:SetBool("On", not table.isEmpty(self._filterKeyDic))
        end
        self:setDropdownListActive()
      end
    }
  }
end

function this:close()
  L_TimerManager:clearTimer(self)
end

function this:setData(param)
  self._systemId = param.systemId
  self._filterCallback = param.filterCallback
  self._filterKeyDic = param.filterKeyDic
  self._sortCallback = param.sortCallback
  self._sortKeyList = param.sortKeyList
  self._isSortReverse = param.isSortReverse
  local tpl_sys = _systemUnlockTpl:getTplById(self._systemId)
  self._sortFilterTpl = _systemFilterTpl:getTplById(_systemUnlockTpl:getFilterConfig(tpl_sys))
  self._filterStyle = _systemFilterTpl:getFilterComponents(self._sortFilterTpl)
  self._sortStyle = _systemFilterTpl:getSortComponents(self._sortFilterTpl)
  self.bind.go_button = self._filterStyle == L_SortFilterConst.SortFilterStyle.Button or self._sortStyle == L_SortFilterConst.SortFilterStyle.Button
  self.bind.go_bar = self._filterStyle == L_SortFilterConst.SortFilterStyle.Bar or self._sortStyle == L_SortFilterConst.SortFilterStyle.Bar
  self.bind.go_dropdown = self._filterStyle == L_SortFilterConst.SortFilterStyle.Dropdown or self._sortStyle == L_SortFilterConst.SortFilterStyle.Dropdown
  self.bind.go_sort_reverse_btn = _systemFilterTpl:getIsReverseOpen(self._sortFilterTpl)
  if self.bind.go_button then
    self:setButtonData()
  end
  if self.bind.go_bar then
    self:setBarData()
  end
  if self.bind.go_dropdown then
    self:setDropdownData()
  end
  self.bindComponents.ani_btn_filter:SetBool("On", not table.isEmpty(self._filterKeyDic))
end

function this:setButtonData()
  if self._filterStyle == L_SortFilterConst.SortFilterStyle.Button then
    self.bind.img_button = PathFilter
  elseif self._sortStyle == L_SortFilterConst.SortFilterStyle.Button then
    self.bind.img_button = PathSort
  end
end

function this:setBarData()
  if self._filterStyle == L_SortFilterConst.SortFilterStyle.Bar then
    self.bind.img_bar = PathFilter
    self.bind.txt_bar = L_WordsTpl:getValue("notice_pageMultiplySortFilter_01")
  elseif self._sortStyle == L_SortFilterConst.SortFilterStyle.Bar then
    self.bind.img_bar = PathSort
    self.bind.txt_bar = L_WordsTpl:getValue("notice_pageMultiplySortFilter_02")
  end
end

function this:setDropdownData()
  self._dropDownShow = false
  self.bind.go_dropdownContent = self._dropDownShow
  self.bind.isOn_arrow = self._dropDownShow
  local tmp = {}
  if self._filterStyle == L_SortFilterConst.SortFilterStyle.Dropdown then
    self.bind.img_dropdown = PathFilter
    local filterConfig = _systemFilterTpl:getFilterConfig(self._sortFilterTpl)
    for _, typeAndLimit in pairs(filterConfig) do
      local typeId, limit = table.unpack(typeAndLimit)
      local tpl_filterType = _systemFiltertypeTpl:getTplById(typeId)
      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(typeId)
      for __, v in ipairs(allContentTpl) do
        table.insert(tmp, {
          type = typeId,
          id = v[idKey],
          txt_name = L_Config:provider(v[nameKey]),
          go_icon = needIcon,
          img_icon = needIcon and v[iconKey] or ""
        })
      end
    end
  elseif self._sortStyle == L_SortFilterConst.SortFilterStyle.Dropdown then
    self.bind.img_dropdown = PathSort
    local sortType = _systemFilterTpl:getSortType(self._sortFilterTpl)[1]
    for index, type in ipairs(sortType) do
      local tpl_sortType = _systemSorttypeTpl:getTplById(type)
      table.insert(tmp, {
        index = index,
        type = type,
        txt_name = _systemSorttypeTpl:getSortTypeName(tpl_sortType),
        go_icon = true,
        img_icon = _systemSorttypeTpl:getIcon(tpl_sortType)
      })
    end
  end
  self.bind.list_dropdown:clear()
  self.bind.list_dropdown:insert_array(tmp)
  self:setDropdownListActive()
end

function this:setDropdownListActive()
  if self._filterStyle == L_SortFilterConst.SortFilterStyle.Dropdown then
    for i, v in pairs(self.bind.list_dropdown) do
      local active = false
      if self._filterKeyDic[v.bind.type] and self._filterKeyDic[v.bind.type][1] == v.bind.id then
        active = true
      end
      v:setActive(active)
      if active then
        self.bind.txt_dropdown = v.bind.txt_name
      end
    end
  elseif self._sortStyle == L_SortFilterConst.SortFilterStyle.Dropdown then
    local firstKeyType = L_SortFilterManager:getNotDefaultFirstSortKey(self._systemId, self._sortKeyList)
    for i, v in pairs(self.modules.list_dropdown) do
      local active = v.bind.type == firstKeyType
      v:setActive(active)
      if active then
        self.bind.txt_dropdown = v.bind.txt_name
      end
    end
  end
end

function this:openMultiplyPage(sortFilterStyle)
  local param
  if self._filterStyle == sortFilterStyle then
    local callback = function(filterKeyDic)
      self._filterKeyDic = filterKeyDic
      self._filterCallback(filterKeyDic)
      self.bindComponents.ani_btn_filter:SetBool("On", not table.isEmpty(self._filterKeyDic))
    end
    param = {
      type = L_SortFilterConst.SortOrFilter.Filter,
      systemId = self._systemId,
      callback = callback,
      selectValue = self._filterKeyDic
    }
  elseif self._sortStyle == sortFilterStyle then
    local callback = function(sortKeyList)
      self._sortKeyList = sortKeyList
      self._sortCallback(sortKeyList, self._isSortReverse)
    end
    param = {
      type = L_SortFilterConst.SortOrFilter.Sort,
      systemId = self._systemId,
      callback = callback,
      selectValue = self._sortKeyList
    }
  end
  L_UI:open("pageMultiplySortFilter", param)
end

function this:checkDropdownShow()
  self._dropDownShow = not self._dropDownShow
  self.bind.isOn_arrow = self._dropDownShow
  if self._dropDownShow then
    self.bind.go_dropdownContent = true
    self.bindComponents.ani_content:Stop()
    self.bindComponents.ani_content:Play("anim_filter_dropdown_menu_show")
    L_GameUtil.forceRebuildLayout(self.bindComponents.rect_content)
  else
    self.bindComponents.ani_content:Stop()
    self.bindComponents.ani_content:Play("anim_filter_dropdown_menu_hide")
    L_TimerManager:newOrResetTimer(self, "hideContent", function()
      self.bind.go_dropdownContent = false
    end, 0.133)
  end
end

return this
