local this = class("moduleProductBag", G_UIPageBase)
local tag = "page:moduleProductBag"
local _productsTpl = L_GameTpl:getProductsTpl()

function this:created(obj, pageConfig)
  this.super.created(self, obj, pageConfig)
end

function this:preBind()
  return {}
end

function this:bind()
  return {
    grl_product = L_Const.ModuleInfo.CellIconBag,
    rl_productQueue = {
      moduleName = "pages/bag/cellFastProductQueue"
    },
    dym_productInfo = {
      type = "toggleModule",
      module_Equip = {
        assetName = "UI/Pages/Product/moduleProductInfoAccessory",
        moduleName = "pages/product/moduleProductInfoAccessory"
      },
      module_Product = {
        assetName = "UI/Pages/Product/moduleProductInfoNormal",
        moduleName = "pages/product/moduleProductInfoNormal"
      }
    },
    dym_productInfoName = ""
  }
end

function this:methods()
  return {}
end

function this:open()
  L_ProductStore:listenCallFunc(L_ProductStore.event.refreshFastProduct, self.onEventMessageRefreshFastProduct, self)
  L_BagStore:listenCallFunc(L_BagStore.event.refreshBag, self.onEventMessageRefreshBag, self)
  L_CollectionToolStore:listenCallFunc(L_CollectionToolStore.event.dataUpdate, self.onEventMessageRefreshBag, self)
end

function this:close()
  L_ProductStore:unListenCallFunc(L_ProductStore.event.refreshFastProduct, self.onEventMessageRefreshFastProduct, self)
  L_BagStore:unListenCallFunc(L_BagStore.event.refreshBag, self.onEventMessageRefreshBag, self)
  L_CollectionToolStore:unListenCallFunc(L_CollectionToolStore.event.dataUpdate, self.onEventMessageRefreshBag, self)
  self.bind.dym_productInfoName = ""
  self._selectProduct = nil
  self._isProductView = nil
end

function this:onSetTabSelect(isProductView)
  self._isProductView = isProductView
  if isProductView then
    self:refreshView()
  end
end

function this:refreshView()
  L_ProductManager:onShowAllFastProductRewards(true)
  self:onRefreshList()
  self:refreshProductQueue()
end

function this:onRefreshList(onlyRefresh)
  if onlyRefresh then
    for _, module in ipairs(self.modules.grl_product) do
      local productId = module.bind.productId
      local hadNum = L_ProductManager:onGetAllFastProductCount(productId, false)
      module:setNumInfo(hadNum)
    end
    return
  end
  local productList = L_ProductManager:getAllCanProductList()
  local dataList = {}
  for key, value in ipairs(productList) do
    local canShow = true
    local hadNum = L_ProductManager:onGetAllFastProductCount(value.productId, false)
    if value.maxCount > 0 then
      canShow = hadNum < value.maxCount
    end
    if canShow then
      local item = {
        index = key,
        productId = value.productId,
        itemNum = hadNum,
        itemId = value.itemId,
        itemType = value.itemType,
        noTip = true,
        callback = function(cell)
          local productId = cell.bind.productId
          self:selectItemOnClick(productId)
        end
      }
      table.insert(dataList, item)
    end
  end
  self.bind.grl_product:clear()
  self.bind.grl_product:insert_array(dataList)
  if self._selectProduct == nil and 0 < #dataList then
    local productId = dataList[1].productId
    self:selectItemOnClick(productId)
  elseif self._selectProduct ~= nil then
    self:selectItemOnClick(self._selectProduct)
  end
end

function this:selectItemOnClick(productId)
  if productId == nil then
    return
  end
  self._selectProduct = productId
  local productTpl = _productsTpl:getTplById(productId)
  if productTpl == nil then
    return
  end
  local groupType = _productsTpl:getGroupId(productTpl)
  local buildFunctionType = L_ProductManager:onGetHomeBuildFunctionType(groupType)
  if buildFunctionType == L_HomeConst.HomeBuildFunctionType.PRODUCT then
    self.bind.dym_productInfoName = "module_Product"
    self.modules.dym_productInfo.module_Product:onSetFastProduct(productId, groupType)
    self.modules.dym_productInfo.module_Product:onSetLocalPos(C_Vector2(290, 20))
  elseif buildFunctionType == L_HomeConst.HomeBuildFunctionType.EQUIP then
    self.bind.dym_productInfoName = "module_Equip"
    self.modules.dym_productInfo.module_Equip:onSetFastProduct(productId)
    self.modules.dym_productInfo.module_Equip:onSetLocalPos(C_Vector2(0, 0))
  end
  for _, v in pairs(self.modules.grl_product) do
    v:setActive(self._selectProduct == v.bind.productId)
  end
end

function this:refreshProductQueue()
  if not self.isBind then
    return
  end
  local hadWorkList = {}
  local productList = L_ProductStore:getFastProductList()
  for _, value in pairs(productList) do
    if not table.isEmpty(value) and value.product_guid ~= nil and value.product_guid > 0 then
      if 0 < value.start_time and 1 <= #hadWorkList then
        table.insert(hadWorkList, 1, value.product_guid)
      else
        table.insert(hadWorkList, value.product_guid)
      end
    end
  end
  local queueData = {}
  for i = 1, 4 do
    local productQueue = hadWorkList[i] or 0
    local item = {index = i, productQueue = productQueue}
    table.insert(queueData, item)
  end
  self.bind.rl_productQueue:clear()
  self.bind.rl_productQueue:insert_array(queueData)
end

function this:onEventMessageRefreshFastProduct()
  if not self._isProductView then
    return
  end
  self:refreshProductQueue()
  L_ProductManager:onShowAllFastProductRewards(true)
end

function this:onEventMessageRefreshBag()
  if not self._isProductView then
    return
  end
  self:onRefreshList(true)
end

function this:destroy()
end

return this
