local module = class("modulePageProductTabList", require("ui.modulePages.tabList.moduleCommonTabList"))
local _productsTpl = L_GameTpl:getProductsTpl()
local tabParams = {
  building = {
    iconColorKey_normal = "#806e5bcc",
    nameColorKey_normal = "#806e5bcc",
    iconColorKey_select = "#fbffb8",
    nameColorKey_select = "#fbffb8"
  },
  product = {
    iconColorKey_normal = "#806e5bcc",
    nameColorKey_normal = "#806e5bcc",
    iconColorKey_select = "#E0923E",
    nameColorKey_select = "#E0923E"
  }
}

function module.bind()
  return {
    selectTab = -1,
    list_tab = {
      moduleName = "modulePages/tabList/cellPageProductTabItem"
    }
  }
end

function module.methods()
  return {
    onTabId = function(self, id)
      if self._callback then
        self._callback(id)
        for i = 1, #self.bind.list_tab do
          local cellPageProductTabItem = self.bind.list_tab:getItemCls(i)
          cellPageProductTabItem:onSelect(cellPageProductTabItem:getTabId() == id, self.tabParam)
        end
      end
      Unity.UI.LayoutRebuilder.ForceRebuildLayoutImmediate(self.bindComponents.root)
    end
  }
end

function module:refreshTabItemTraceNode(traceTaskIdAndTraceFormulaIdListInfo)
  for i = 1, #self.bind.list_tab do
    local cellPageProductTabItem = self.bind.list_tab:getItemCls(i)
    cellPageProductTabItem:hideTraceTaskNode()
  end
  local traceTaskId = traceTaskIdAndTraceFormulaIdListInfo.traceTaskId
  local traceFormulaIdList = traceTaskIdAndTraceFormulaIdListInfo.traceFormulaIdList
  if traceTaskId == nil or traceFormulaIdList == nil then
    return
  end
  for _, traceFormulaId in pairs(traceFormulaIdList) do
    local traceProductId = traceFormulaId
    local productsTplOfTraceProductId = _productsTpl:getTplById(traceProductId)
    local tabId = _productsTpl:getTabId(productsTplOfTraceProductId)
    if tabId and 0 < tabId then
      local cellPageProductTabItem = self:getCellPageProductTabItemByTabId(tabId)
      if cellPageProductTabItem then
        cellPageProductTabItem:refreshTraceTaskNode(traceTaskId)
      end
    end
  end
end

function module:getCellPageProductTabItemByTabId(tabId)
  for i = 1, #self.bind.list_tab do
    local cellPageProductTabItem = self.bind.list_tab:getItemCls(i)
    local tabIdOfTabItem = cellPageProductTabItem:getTabId()
    if tabIdOfTabItem == tabId then
      return cellPageProductTabItem
    end
  end
  errorf(string.format("modulePageProductTabList can not found cellPageProductTabItem by tabId: %d", tabId))
  return nil
end

function module:setData(param)
  self.tabParam = tabParams[param.tabType or "product"]
  self.super.setData(self, param.idList, param.callback, param.selectId, param.redKeys)
end

return module
