local this = class("cellFastProductQueue", G_UIModuleBase)
local _commonItemTpl = L_GameTpl:getCommonItemTpl()
local _productsTpl = L_GameTpl:getProductsTpl()
local CellMode = {
  NORMAL = 1,
  NONE = 2,
  LOCK = 3
}
local rarityepColorMap = {
  [-1] = "#ffffff80",
  [0] = "#a0aeb780",
  [1] = "#27ae8980",
  [2] = "#3ab3ff80",
  [3] = "#a264ff80",
  [4] = "#ffbf3580"
}

function this.bind()
  return {
    tab_mode = -1,
    img_icon = "",
    img_iconFilter = "",
    color_rarityep = nil,
    txt_time = "",
    txt_finishNum = "",
    txt_totalNum = "",
    color_time = C_Color(1, 1, 1, 1),
    toggle_finish = false
  }
end

function this.methods()
  return {
    onClick_delete = function(self)
      self:OnEventBtnCancelProduct()
    end
  }
end

function this:open()
  this.super.open(self)
  self:refreshInfo()
end

function this:OnSetData(index, data)
end

function this:refreshInfo()
  L_TimerManager:clearTimer(self)
  if not self.isBind then
    return
  end
  local indexQueue = self.bind.productQueue
  self._indexQueue = indexQueue
  local productInfo = L_ProductStore:getFastProductByIndex(indexQueue)
  self._productInfo = productInfo
  if table.isEmpty(productInfo) then
    self.bind.tab_mode = CellMode.NONE
  else
    self.bind.tab_mode = CellMode.NORMAL
    local productId = productInfo.product_id
    local tpl = _productsTpl:getTplById(productId)
    if tpl == nil then
      return
    end
    local reward = _productsTpl:getRewardId(tpl)
    local itemData = L_DataUtil.parseRewardConfigItem(reward[1])
    local conf = L_ItemTplManager:getItemConfig(itemData.itemType, itemData.itemId)
    self.bind.img_icon = conf.icon
    local commonItemTpl = _commonItemTpl:getTplById(conf.itemId)
    self.bind.img_iconFilter = commonItemTpl and _commonItemTpl:getFilterIcon(commonItemTpl) or ""
    local _, color = C_ColorUtility.TryParseHtmlString(rarityepColorMap[conf.quality])
    self.bind.color_rarityep = color
    local startTime = self._productInfo.start_time or 0
    local endTime = self._productInfo.finish_time or 0
    if 0 < startTime and 0 < endTime then
      L_TimerManager:newOrResetTimer(self, "refresh", function()
        self:onTimer()
      end, 0.5, -1)
      self:onTimer()
    else
      self.bind.txt_time = L_WordsTpl:getValue("ui_cellFastProductQueue_01")
      self.bind.color_time = C_Color(1, 1, 1, 0.8)
      self.bind.txt_finishNum = "0"
      self.bind.txt_totalNum = string.format("/%s", self._productInfo.total_count)
    end
  end
end

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

function this:onTimer()
  if table.isEmpty(self._productInfo) then
    return
  end
  local productId = self._productInfo.product_id
  local totalNum = self._productInfo.total_count
  local finishNum = 0
  local startTime = self._productInfo.start_time
  local endTime = self._productInfo.finish_time
  if startTime == nil or startTime <= 0 and endTime == nil and endTime <= 0 then
    return
  end
  if finishNum == totalNum then
    self.bind.toggle_finish = true
    self.bind.txt_time = L_WordsTpl:getValue("notice_cellProductQueueNew_03")
    self.bind.color_time = C_Color(1.0, 0.7490196078431373, 0.20392156862745098, 1.0)
  else
    self.bind.toggle_finish = false
    self.bind.color_time = C_Color(1, 1, 1, 1)
  end
  local nowTime = L_TimeUtil.getServerTimeWithFloor()
  local leftTime = math.ceil(endTime - nowTime)
  self.bind.txt_time = L_TimeUtil.secondToLangString(leftTime)
  self.bind.txt_finishNum = string.format("%s", finishNum)
  self.bind.txt_totalNum = string.format("/%s", totalNum)
end

function this:OnEventBtnCancelProduct()
  if table.isEmpty(self._productInfo) then
    return
  end
  local guid = self._productInfo.product_guid
  L_ProductStore:req_CSProtoSimpleProductCancel(guid)
end

return this
