local this = class("pageProduct_Quick", G_UIPageBase)
local _productsTpl = L_GameTpl:getProductsTpl()
local columnNum = 5
local minLineNum = 5

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

function this:preBind()
  return {}
end

function this:bind()
  return {
    grl_product = {
      moduleName = "pages/product/cellProduceTarget"
    },
    rl_productQueue = {
      moduleName = "pages/product/cellProductQueue"
    },
    module_Product = {
      moduleName = "pages/product/moduleProductInfoNormal"
    },
    alpha_img_desc_work = 0,
    alpha_img_desc_sleep = 0
  }
end

function this:methods()
  return {
    onClick_close = function(self)
      L_UI:close(self.pageName)
    end,
    grl_product = {
      onClick_selectId = function(self, id)
        self:selectItemOnClick(id)
      end
    }
  }
end

function this:check(options, callback)
  local result = L_SystemUnlockManager:checkAndTip(L_SystemConst.enum.productQuick)
  if not result then
    callback(false)
    return
  end
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)
  self:refreshView()
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._selectProduct = nil
  L_TimerManager:clearTimer(self)
end

function this:show()
  if self.isHide then
    if self.bind.rl_productQueue then
      for i = 1, #self.bind.rl_productQueue do
        local cellProductQueue = self.bind.rl_productQueue:getItemCls(i)
        if cellProductQueue and cellProductQueue.bind.tab_mode == 2 then
          cellProductQueue.bindComponents.animation_none_on:Play("anim_product_queue_normal_in")
        end
      end
    end
    self.isHide = false
  end
end

function this:hide()
  self.isHide = true
  if self.bind.rl_productQueue then
    local cellProductQueue = self.bind.rl_productQueue:getItemCls(1)
    if cellProductQueue then
      cellProductQueue:hideFinishFX()
    end
  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
      module:refresh()
    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 productId = value.productId
      local productsTpl = _productsTpl:getTplById(productId)
      local reward = _productsTpl:getRewardId(productsTpl)
      local itemData = L_DataUtil.parseRewardConfigItem(reward[1])
      local itemParse = L_ItemTplManager:getItemConfig(itemData.itemType, itemData.itemId)
      local tmp = {
        id = productId,
        productId = productId,
        itemData = itemData,
        itemName = itemParse.name
      }
      table.insert(dataList, tmp)
    end
  end
  local dataNum = #dataList
  local colNum = columnNum
  local lineNum = math.ceil(dataNum / colNum)
  lineNum = math.max(lineNum, minLineNum)
  for i = 1, lineNum * colNum - dataNum do
    table.insert(dataList, {isFood = false, itemData = false})
  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)
  else
    if self._selectProduct ~= nil then
      self:selectItemOnClick(self._selectProduct)
    else
    end
  end
end

function this:selectItemOnClick(productId)
  if productId == nil then
    return
  end
  local moduleProductInfo = self.modules.module_Product
  local _selectItemOnClick = function()
    self._selectProduct = productId
    local productTpl = _productsTpl:getTplById(productId)
    if productTpl == nil then
      return
    end
    for i = 1, #self.bind.grl_product do
      self.bind.grl_product:change(i, {
        selectIconActive = self.bind.grl_product:getValue(i, "id") == productId
      })
    end
    local groupType = _productsTpl:getGroupId(productTpl)
    local buildFunctionType = L_ProductManager:onGetHomeBuildFunctionType(groupType)
    if buildFunctionType == L_HomeConst.HomeBuildFunctionType.PRODUCT then
      moduleProductInfo:setSingleQualityProduct(0, productId, groupType)
    elseif buildFunctionType == L_HomeConst.HomeBuildFunctionType.EQUIP then
    end
  end
  if self._selectProduct ~= productId then
    local animation = moduleProductInfo.bindComponents.animation
    if animation.isPlaying then
      animation:Stop()
    end
    animation:Play("anim_product_commoninfo_change1")
    L_TimerManager:newOrResetTimer(self, "selectItemOnClick", function()
      _selectItemOnClick()
    end, 0.066, 1)
  else
    _selectItemOnClick()
  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 insertArrayList = {}
  for i = 1, 4 do
    local fastProductQueueIndex = hadWorkList[i] or 0
    local productInfo = L_ProductStore:getFastProductByIndex(fastProductQueueIndex)
    local queueData = {}
    queueData.isUnlock = true
    queueData.product = productInfo
    queueData.isEmpty = table.isEmpty(productInfo) or math.isEmpty(productInfo.product_id)
    queueData.isFastQueue = true
    local tmp = {viewIndex = i, queueData = queueData}
    table.insert(insertArrayList, tmp)
  end
  if not self.hasInsert then
    self.bind.rl_productQueue:clear()
    self.bind.rl_productQueue:insert_array(insertArrayList)
    self.hasInsert = true
  else
    for i = 1, #self.bind.rl_productQueue do
      if i == 1 and (self.oldInsertArrayList[1].queueData.isEmpty == false and insertArrayList[1].queueData.isEmpty == true or self.oldInsertArrayList[1].queueData.product and insertArrayList[1].queueData.product and self.oldInsertArrayList[1].queueData.product.total_count ~= insertArrayList[1].queueData.product.total_count or self.oldInsertArrayList[1].queueData.product and insertArrayList[1].queueData.product and self.oldInsertArrayList[1].queueData.product.product_guid ~= insertArrayList[1].queueData.product.product_guid) then
        local cellProductQueue = self.bind.rl_productQueue:getItemCls(i)
        cellProductQueue:playAnimProductQueueCompleteOfFastProduct()
      end
      self.bind.rl_productQueue:change(i, insertArrayList[i])
    end
  end
  local isWorked = false
  for i = 1, #self.bind.rl_productQueue do
    if self.oldInsertArrayList == nil then
      break
    end
    local itemData = self.oldInsertArrayList[i]
    if itemData.queueData.isEmpty == false then
      isWorked = true
      break
    end
  end
  local isWorking = false
  for i = 1, #self.bind.rl_productQueue do
    local itemData = insertArrayList[i]
    if itemData.queueData.isEmpty == false then
      isWorking = true
      break
    end
  end
  self.bind.alpha_img_desc_work = isWorking and 1 or 0
  self.bind.alpha_img_desc_sleep = isWorking and 0 or 1
  if isWorked == false and isWorking == true then
    self.bindComponents.anim_img_desc:Play("anim_product_quick_desc_towork")
  elseif isWorked == true and isWorking == false then
    self.bindComponents.anim_img_desc:Play("anim_product_quick_desc_tosleep")
  end
  self.oldInsertArrayList = insertArrayList
end

function this:onEventMessageRefreshFastProduct()
  self:refreshProductQueue()
  L_ProductManager:onShowAllFastProductRewards(true)
end

function this:onEventMessageRefreshBag()
  self:onRefreshList(true)
end

function this:destroy()
end

return this
