local this = class("cellProduceItem", G_UIModuleBase)
local itemStatus = {
  free = 1,
  producingNoReward = 2,
  producingHasReward = 3,
  completed = 4
}
local _, proressBarColor = C_ColorUtility.TryParseHtmlString("#AF9575")
local _, produceNumBgColor = C_ColorUtility.TryParseHtmlString("#A08E75")
local _, rewardedProressBarColor = C_ColorUtility.TryParseHtmlString("#E88E2D")
local _, rewardedProduceNumBgColor = C_ColorUtility.TryParseHtmlString("#EB9C48")

function this.bind()
  return {
    rewardBgActive = false,
    normalBgActive = false,
    lockBgActive = false,
    lockContentActive = false,
    freeFlipActive = false,
    producingFlipActive = false,
    leftProducingActive = false,
    rightProducingActive = false,
    finishIconActive = false,
    cancleBtnActive = false,
    unlockDes = "",
    progressBarColor = proressBarColor,
    produceNumBgColor = produceNumBgColor,
    leftTime = "",
    rightTime = "",
    produceNum = "",
    progressBar = 0,
    itemIcon = "",
    buildIcon = "",
    itemIconActive = true,
    buildIconActive = false,
    leftTimeACtive = false,
    rightTimeActive = false,
    leftFinishedActive = false,
    rightFinishedActive = false,
    collectBtnActive = false,
    contentFlipSize = C_Vector2(538, 136),
    bgFlipSclae = C_Vector3(1, 1, 1),
    progressFillOrigin = 0
  }
end

function this.methods()
  return {
    onClick_collectBtn = function(self)
      if self.pause then
        return
      end
      L_ProductManager:reqCollectProduct(self.bind.buildingGuid)
    end,
    onClick_cancleBtn = function(self)
      local data_tip = {
        txtTitle = L_WordsTpl:getValue("ui_product_cancle"),
        txtContent = L_WordsTpl:getValue("ui_product_cancle_tip"),
        confirmCallback = function()
          L_ProductManager:reqCancelProduct(self.bind.buildingGuid, self.bind.product.product_guid)
        end
      }
      L_GameUtil.showCommonTip(data_tip)
    end
  }
end

function this:open()
  if self.isBind then
    self:refreshCell()
  end
end

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

function this:resetUI()
  self.bind.rewardBgActive = false
  self.bind.normalBgActive = false
  self.bind.lockBgActive = false
  self.bind.lockContentActive = false
  self.bind.freeFlipActive = false
  self.bind.producingFlipActive = false
  self.bind.leftProducingActive = false
  self.bind.rightProducingActive = false
  self.bind.finishIconActive = false
  self.bind.cancleBtnActive = false
  self.bind.leftTimeACtive = false
  self.bind.rightTimeActive = false
  self.bind.leftFinishedActive = false
  self.bind.rightFinishedActive = false
  self.bind.collectBtnActive = false
  self.bind.produceNumBgColor = produceNumBgColor
  self.bind.progressBarColor = proressBarColor
end

function this:refreshCell()
  if not self.isBind then
    return
  end
  self:resetUI()
  local isUnlock = self.bind.isUnlock
  self.bind.lockBgActive = not isUnlock
  self.bind.lockContentActive = not isUnlock
  L_TimerManager:stopTimer(self, "timeChange")
  if isUnlock then
    local isFree = self.bind.isEmpty
    if not isFree then
      self._onceTime = L_ProductManager:getProduceOnceTime(self.bind.buildingGuid, self.bind.product.product_id)
      self._endTime = self.bind.product.start_time + self.bind.product.total_count * self._onceTime
      L_TimerManager:newOrResetTimer(self, "timeChange", function()
        self:refreshStatus()
        self:refreshUI()
      end, 0.5, -1)
      self:refreshStatus()
    else
      self._currentStatus = itemStatus.free
    end
    self:refreshUI()
  else
    self.bind.unlockDes = L_WordsTpl:getValue("ui_product_unlock_tip", {
      [0] = self.bind.unlockLevel
    })
  end
end

function this:refreshStatus()
  self._currentStatus = itemStatus.free
  self._lastStatus = self._lastStatus or itemStatus.free
  local nowTime = L_TimeUtil.getServerTime()
  if nowTime >= self._endTime then
    self._currentStatus = itemStatus.completed
    L_TimerManager:stopTimer(self, "timeChange")
  elseif nowTime < self.bind.product.start_time then
    self._currentStatus = itemStatus.producingNoReward
  else
    local finishNum, _, _ = L_ProductManager:getProductProduceInfo(self.bind.buildingGuid, self.bind.product)
    if 0 < finishNum then
      self._currentStatus = itemStatus.producingHasReward
    else
      self._currentStatus = itemStatus.producingNoReward
    end
  end
end

function this:refreshUI()
  if self._lastStatus ~= self._currentStatus then
    self:resetUI()
  end
  if self._currentStatus == itemStatus.free then
    self.bind.normalBgActive = true
    self.bind.freeFlipActive = true
  else
    self:setInfo(self.bind.product, self.bind.buildingGuid)
    self.bind.leftProducingActive = self.bind.index % 2 ~= 0
    self.bind.rightProducingActive = self.bind.index % 2 == 0
    self.bind.cancleBtnActive = true
    self.bind.producingFlipActive = true
    if self._currentStatus == itemStatus.producingNoReward then
      self.bind.normalBgActive = true
      self.bind.leftTimeACtive = true
      self.bind.rightTimeActive = true
    end
    if self._currentStatus == itemStatus.producingHasReward then
      self.bind.rewardBgActive = true
      self.bind.produceNumBgColor = rewardedProduceNumBgColor
      self.bind.progressBarColor = rewardedProressBarColor
      self.bind.leftTimeACtive = true
      self.bind.rightTimeActive = true
      self.bind.collectBtnActive = true
    end
    if self._currentStatus == itemStatus.completed then
      self.bind.rewardBgActive = true
      self.bind.cancleBtnActive = false
      self.bind.produceNumBgColor = rewardedProduceNumBgColor
      self.bind.progressBarColor = rewardedProressBarColor
      self.bind.leftFinishedActive = true
      self.bind.rightFinishedActive = true
      self.bind.collectBtnActive = true
      self.bind.finishIconActive = true
      self.bind.progressBar = 1
    end
  end
  local flip = 1
  self.bind.progressFillOrigin = 0
  if self.bind.index % 2 == 0 then
    flip = -1
    self.bind.progressFillOrigin = 1
  end
  self.bind.contentFlipSize = C_Vector2(flip * 538, 136)
  self.bind.bgFlipSclae = C_Vector3(flip * 1, 1, 1)
  self._lastStatus = self._currentStatus
end

function this:setInfo(product, buildingGuid)
  local itemData = L_ProductManager:getProductRewardItemData(product.product_id)
  local item = L_ItemTplManager:getItemConfig(itemData.itemType, itemData.itemId)
  self.bind.itemIcon = item.icon
  self.bind.buildIcon = item.icon
  self.bind.buildIconActive = itemData.itemType == L_Const.resType.homeBuilding
  self.bind.itemIconActive = itemData.itemType ~= L_Const.resType.homeBuilding
  if L_SatietyStore:isBuildingPause(buildingGuid) then
    self.bind.leftTime = L_WordsTpl:getValue("notice_cellProduceItem_01")
    self.bind.rightTime = L_WordsTpl:getValue("notice_cellProduceItem_01")
    self.bind.produceNum = L_WordsTpl:getValue("notice_cellProduceItem_02")
    self.bind.progressBar = 0
    self._currentStatus = itemStatus.producingNoReward
    self:resetUI()
    self.bind.leftProducingActive = self.bind.index % 2 ~= 0
    self.bind.rightProducingActive = self.bind.index % 2 == 0
    self.bind.cancleBtnActive = true
    self.bind.producingFlipActive = true
    if self._currentStatus == itemStatus.producingNoReward then
      self.bind.normalBgActive = true
      self.bind.leftTimeACtive = true
      self.bind.rightTimeActive = true
    end
    self.pause = true
    return
  end
  self.pause = false
  local onceTime = L_ProductManager:getProduceOnceTime(buildingGuid, product.product_id)
  local endTime = product.start_time + product.total_count * onceTime
  local nowTime = L_TimeUtil.getServerTime()
  local leftTime = endTime - nowTime
  local finishNum, total, amount = L_ProductManager:getProductProduceInfo(buildingGuid, product)
  if product.start_time > L_TimeUtil.getServerTime() then
    self.bind.progressBar = 0
    leftTime = onceTime * product.total_count
    self.bind.leftTime = L_TimeUtil.secondToLangString(leftTime)
    self.bind.rightTime = L_TimeUtil.secondToLangString(leftTime)
    self.bind.produceNum = L_WordsTpl:getValue("ui_product_produce_number", {
      [0] = 0,
      [1] = total
    })
  else
    if leftTime < 0 then
      leftTime = 0
    end
    self.bind.progressBar = amount
    self.bind.leftTime = L_TimeUtil.secondToLangString(leftTime)
    self.bind.rightTime = L_TimeUtil.secondToLangString(leftTime)
    self.bind.produceNum = L_WordsTpl:getValue("ui_product_produce_number", {
      [0] = finishNum,
      [1] = total
    })
  end
end

return this
