local this = class("moduleProductQueueNew", G_UIModuleBase)

function this.bind()
  return {
    list_queue = {
      moduleName = "pages/product/cellProductQueue"
    }
  }
end

function this.methods()
  return {
    list_queue = {
      onClickPageProductReward = function(self, bind)
        local viewIndex = bind.viewIndex
        local cellProductQueue = self.bind.list_queue:getItemCls(viewIndex)
        if cellProductQueue:checkIsPlayingAnimProductQueueGetOrCompleteOfNormalProduct() then
          return
        end
        self:onClickPageProductReward()
      end
    }
  }
end

function this:open()
  self:refreshQueue()
  L_ProductStore:listenCallFunc(L_ProductStore.event.refreshHomeProduct, self.refreshQueue, self)
end

function this:close()
  L_ProductStore:listenCallFunc(L_ProductStore.event.refreshHomeProduct, self.refreshQueue, self)
  L_TimerManager:clearTimer(self)
end

function this:initModule(guid)
  self._buildingGuid = guid
  self:refreshQueue()
end

function this:refreshQueue()
  if not self.isBind or not self._buildingGuid then
    return
  end
  if self.waitForAnimProductQueueGetOfNormalProductFlag then
    return
  end
  local build = L_HomeStore:getHomeBuildingByGuid(self._buildingGuid)
  local queueData = L_ProductManager:getProduceQueueForBuildingWithoutLuaData(self._buildingGuid)
  local tmp = {}
  for i = 1, #queueData do
    table.insert(tmp, {
      viewIndex = i,
      queueData = queueData[i]
    })
  end
  if not self.hasInsert then
    self.bind.list_queue:clear()
    self.bind.list_queue:insert_array(tmp)
    self.hasInsert = true
  else
    for i = 1, #self.bind.list_queue do
      self.bind.list_queue:change(i, tmp[i])
    end
  end
end

function this:onClickPageProductReward()
  local csHomeBuildData = L_HomeStore:getCsHomeBuildDataByGuid(self._buildingGuid)
  local reqNormalProductQueueList = {}
  for i = 1, #self.bind.list_queue do
    local cellProductQueue = self.bind.list_queue:getItemCls(i)
    local csHomeBuildProductData = cellProductQueue:getCsHomeBuildProductData()
    if csHomeBuildProductData then
      local finishCount = csHomeBuildProductData.finishCount
      if 0 < finishCount then
        if L_HomeUtil.getBuildingType(self._buildingGuid) == L_Const.WorldMapBuildType.WMBT_HATCH then
          L_PetStore:onClickHatchOutPetEgg(self._buildingGuid, csHomeBuildProductData.specialGuid)
        else
          table.insert(reqNormalProductQueueList, cellProductQueue)
        end
      end
    end
  end
  if 0 < #reqNormalProductQueueList then
    for _, cellProductQueue in pairs(reqNormalProductQueueList) do
      cellProductQueue:playAnimProductQueueGetOfNormalProduct()
    end
    self.waitForAnimProductQueueGetOfNormalProductFlag = true
    local animStartTime = C_TimeUtility.NowServer
    L_ProductStore:req_CSProtoProductFinish(self._buildingGuid, function(rspData, errCode)
      if errCode ~= L_Const.errorCode.ErrCodeSucc then
        return
      end
      local showReward = function()
        local reward
        if rspData.reward then
          reward = rspData.reward.rewards
        end
        local specialReward
        if rspData.special_reward then
          specialReward = rspData.special_reward.rewards
        end
        if not table.isEmpty(reward) then
          rspData.reward.ignoreQueueCheck = true
          L_RewardManager:ShowReward(rspData.reward)
        end
        if not table.isEmpty(specialReward) then
          rspData.special_reward.ignoreQueueCheck = true
          L_RewardManager:ShowReward(rspData.special_reward)
        end
      end
      local restTime = 0.55 - (C_TimeUtility.NowServer - animStartTime)
      L_TimerManager:newOrResetTimer(self, "showRewardWaitForAnimProductQueueGetOfNormalProduct", function()
        self.waitForAnimProductQueueGetOfNormalProductFlag = false
        showReward()
        self:refreshQueue()
      end, restTime, 1)
    end)
  end
end

return this
