local item = class("cellActivityTaskItem", G_UIModuleBase)

function item.bind()
  return {
    taskDescribe = "",
    currentCount = "",
    totalCount = "",
    taskActivityReward = 0,
    currentPercent = 0,
    finishBg = false,
    finishIcon = false,
    rewardList = {
      moduleName = "pages/ActivityTask/cellStatusRewardItem"
    },
    show_receiveBtn = false,
    show_progressing = false,
    txt_progress = L_WordsTpl:getValue("ui_cellNoviceTaskItem_03"),
    show_goto = false,
    targetBgAlpha = C_Color(0.1568627450980392, 0.17254901960784313, 0.1843137254901961, 1),
    taskNumBg = true,
    taskDescGo = true,
    taskFinishBg = true,
    txt_receive = L_WordsTpl:getValue("ui_cellNoviceTaskItem_06")
  }
end

function item.methods()
  return {
    onClick_receive = function(self)
      self:emit("onClick_receive", self.bind)
      self:onClick()
    end,
    onClick_goto = function(self)
      L_JumpMgr:jumpTo(self.bind.systemId)
    end
  }
end

function item:open(...)
end

function item:hide()
end

function item:refresh()
  self:updateTaskData()
end

function item:updateTaskData()
  self.bind.rewardList:clear()
  if math.isEmpty(self.bind.curActivityId) then
    self.bind.taskNumBg = false
    self.bind.taskDescGo = true
    self.bind.taskDescribe = L_WordsTpl:getValue("ui_cellNoviceTaskItem_04")
    self.bind.taskFinishBg = false
    self.bind.show_progressing = false
    self.bind.show_goto = false
    self.bind.finishBg = false
    self.bind.txt_receive = L_WordsTpl:getValue("ui_cellNoviceTaskItem_05")
    self.bind.show_receiveBtn = true
  else
    if self.bind.finishState == 2 then
      self.bind.finishBg = true
      self.bind.show_receiveBtn = false
      self.bind.show_progressing = false
      self.bind.show_goto = false
      self.bind.targetBgAlpha = C_Color(1, 1, 1, 0.5)
    elseif self.bind.finishState == 0 then
      self.bind.finishBg = false
      self.bind.show_receiveBtn = false
      self.bind.targetBgAlpha = C_Color(1, 1, 1, 1)
      self:checkSystemJump()
    elseif self.bind.finishState == 1 then
      self.bind.show_receiveBtn = true
      self.bind.finishBg = false
      self.bind.show_progressing = false
      self.bind.show_goto = false
      self.bind.targetBgAlpha = C_Color(1, 1, 1, 1)
    end
    if self.bind.itemReward then
      local base_data = {}
      for i, v in ipairs(self.bind.itemReward) do
        table.insert(base_data, {
          itemId = v[2],
          itemType = v[1],
          itemNum = v[3],
          isFinish = self.bind.finishState == 2
        })
      end
      self.bind.rewardList:insert_array(base_data)
    end
    local desc = self.bind.describe
    local colorTxt = self.bind.index == 1 and "#24344e" or "#564320"
    if self.bind.finishState == 1 then
      self.bind.count = self.bind.targetCount
    end
    local str = string.format("<color=%s>(%d/%d)</color>", colorTxt, self.bind.count, self.bind.targetCount)
    self.bind.taskDescribe = string.format("%s%s", desc, str)
    self.bind.currentPercent = self.bind.finishState == 2 and 0 or self.bind.count / self.bind.targetCount
  end
  self.bind.finishIcon = self.bind.finishBg
end

function item:checkSystemJump()
  if math.isEmpty(self.bind.systemId) then
    self.bind.show_goto = false
    self.bind.txt_progress = L_WordsTpl:getValue("ui_cellNoviceTaskItem_03")
    self.bind.show_progressing = true
  elseif L_JumpMgr:checkJumpFuncOpen(self.bind.systemId) then
    self.bind.show_progressing = false
    self.bind.show_goto = true
  else
    self.bind.show_goto = false
    self.bind.txt_progress = L_WordsTpl:getValue("ui_cellNoviceTaskItem_02")
    self.bind.show_progressing = true
  end
end

function item:onClick()
  if not self.isBind then
    return
  end
  if self.bindComponents.cellAni then
    self.bindComponents.cellAni:Stop()
    self.bindComponents.cellAni:Play("anim_AT_cell2_finish")
    self.bind.show_receiveBtn = false
    self.bind.show_progressing = false
    self.bind.show_goto = false
  end
end

return item
