local this = class("moduleTaskInfoBase", G_UIModuleBase)
local _taskTpl = L_GameTpl:getTaskTpl()
local _taskStepTpl = L_GameTpl:getTaskStepTpl()

function this:showTask(typeId, playAnim)
  if self:refreshTaskByType(typeId) then
    self.gameObject:SetActive(true)
    if playAnim then
      self:playShowAnim()
    end
    self.isShow = true
  elseif playAnim then
    self.isShow = false
  else
    self:hideTask()
  end
end

function this:hideTask()
  self.isShow = false
  self.gameObject:SetActive(false)
end

function this:playShowAnim()
end

function this:refreshTaskByType(typeId)
  self.typeId = typeId
end

function this:refreshTaskTraceInfo()
end

function this:refreshTask(taskId)
  self.taskId = taskId
  local tplTask = _taskTpl:getTplById(taskId)
  if tplTask == nil then
    return
  end
  self:refreshTaskTraceInfo()
  self.nodeId = C_TaskManager.GetExcuteNode(taskId)
  local list = C_TaskManager.GetTaskNodeCommonData(taskId)
  if list.Count > 0 then
    self.bind.txtMap = C_TaskManager.GetCityBorthData(list[0].sceneId, list[0].birthId, true)
  else
    self.bind.txtMap = ""
  end
  self.bind.activeMap = not string.isEmpty(self.bind.txtMap)
  self.bind.txtDes = ""
  local tplTaskStep
  if self.nodeId then
    local stepId = L_TaskManager:GetTaskStepId(self.taskId, self.nodeId)
    tplTaskStep = _taskStepTpl:getTplById(stepId)
  end
  local sign, rewards
  if tplTaskStep then
    sign = _taskStepTpl:getSign(tplTaskStep)
    self.bind.txtDes = _taskStepTpl:getDesc(tplTaskStep)
    rewards = self:getStepRewards(self.taskId, self.nodeId)
  end
  if string.isEmpty(sign) then
    self.bind.activeSign = false
  else
    self.bind.activeSign = true
    self.bind.txtSign = sign
  end
  local taskRewards = _taskTpl:getTaskReward(tplTask)
  if rewards and 0 < #rewards then
    self.bind.activeReward = true
    if 0 < #taskRewards then
      self.bind.activeRewardTab = true
      self.bind.activeRewardTitle = false
      self.bind.selectedRewardTabId = 1
    else
      self.bind.activeRewardTab = false
      self.bind.activeRewardTitle = true
      self.bind.txtRewardTitle = L_WordsTpl:getValue("ui_task_finishstagereward")
    end
    self:showRewards(rewards)
  elseif 0 < #taskRewards then
    self.bind.activeReward = true
    self.bind.activeRewardTab = false
    self.bind.activeRewardTitle = true
    self.bind.txtRewardTitle = L_WordsTpl:getValue("ui_task_finishtotalreward")
    self:showRewards(taskRewards)
  else
    self.bind.activeReward = false
  end
  self.bind.targetList:clear()
  if tplTaskStep then
    local infos = L_TaskManager:GetNodeInfoParam(taskId, self.nodeId)
    if _taskStepTpl:getIsLimitStep(tplTaskStep) then
      self.bind.activeLimit = true
      for i = 0, infos.Count - 1 do
        if not infos[i].isComplate then
          local txt = L_WordsTpl:getValue("ui_task_unlock_text")
          self.bind.txtLimit = txt .. L_TaskManager:getTaskNodeTagetText(infos[i], false)
          break
        end
      end
    else
      self.bind.activeLimit = false
      local listDatas = L_DataUtil.getEmptyModuleDatas(infos.Count)
      for i = 1, #listDatas do
        local taskInfo = infos[i - 1]
        listDatas[i].txt_info = L_TaskManager:getTaskNodeTagetText(taskInfo, true)
        listDatas[i].toggle_isDone = taskInfo.isComplate
        listDatas[i].isSecondary = taskInfo.isUnneed
      end
      self.bind.targetList:insert_array(listDatas)
    end
  end
  self.parent:onShowTask(taskId)
end

function this:onSelectRewardTab(tabId)
  if tabId == 1 then
    local rewards = self:getStepRewards(self.taskId, self.nodeId)
    self:showRewards(rewards)
  elseif tabId == 2 then
    local tplTask = _taskTpl:getTplById(self.taskId)
    local rewards = _taskTpl:getTaskReward(tplTask)
    self:showRewards(rewards)
  end
end

function this:getStepRewards(taskId, nodeId)
  if not taskId or not nodeId then
    return
  end
  local stepId = L_TaskManager:GetTaskStepId(taskId, nodeId)
  local tplTaskStep = _taskStepTpl:getTplById(stepId)
  if tplTaskStep then
    return _taskStepTpl:getRewardShow(tplTaskStep)
  end
end

function this:showRewards(rewards)
  self.bind.rewardList:clear()
  if rewards == nil then
    return
  end
  local rewardData = L_DataUtil.parseRewardConfig(rewards)
  for _, v in ipairs(rewardData) do
    local itemConf = L_ItemTplManager:getItemConfig(v.itemType, v.itemId)
    v.quality = itemConf.quality
  end
  L_CommonUtil.multipleSortByKey(rewardData, {
    "quality",
    "-itemType",
    "-itemId"
  })
  self.bind.rewardList:insert_array(rewardData)
  if #rewardData < 9 then
    self.bindComponents.rewardList.contentPivot = Unity.Vector2(0.5, 0.5)
  else
    self.bindComponents.rewardList.contentPivot = Unity.Vector2(0, 0.5)
  end
end

function this:setDefaultOpenTaskId(taskId)
end

function this:refreshTrace()
  self:refreshTaskTraceInfo()
end

return this
