local base = require("ui/pages/task/moduleTaskInfoBase")
local this = class("moduleOtherTaskInfo", base)
local _taskTpl = L_GameTpl:getTaskTpl()
local _taskStepTpl = L_GameTpl:getTaskStepTpl()
local _taskTargetTpl = L_GameTpl:getTaskTargetTpl()

function this:bind()
  return {
    txtName = "",
    activeName = false,
    txtNameOn = "",
    activeNameOn = false,
    txtMap = "",
    activeMap = false,
    targetList = {
      moduleName = "pages/task/cellTaskTargetInfo"
    },
    txtDes = "",
    activeSign = false,
    txtSign = "",
    selectedRewardTabId = 0,
    rewardList = L_Const.ModuleInfo.CellIconBag,
    activeRewardTab = false,
    activeRewardTitle = false,
    txtRewardTitle = "",
    activeReward = false,
    activeLimit = false,
    txtLimit = "",
    selectTaskTabId = 0,
    tabList = {
      moduleName = "pages/task/cellGroupTask"
    }
  }
end

function this:methods()
  return {
    onSelectRewardTab = function(self, tabId, a)
      self:onSelectRewardTab(tabId)
    end,
    onSelectTaskTab = function(self, tabId, a)
      for _, v in ipairs(self.modules.tabList) do
        v:refreshSelected()
      end
      self.curSelectedId = tabId
      self:refreshTask(tabId)
      self.bindComponents.animation:Stop()
      self.bindComponents.animation:Play("anim_task_m2_fresh")
    end
  }
end

function this:playShowAnim()
  self.bindComponents.animation:Stop()
  if self.isShow then
    self.bindComponents.animation:Play("anim_task_m2_fresh")
  else
    self.bindComponents.animation:Play("anim_task_m2_switch")
  end
end

function this:refreshTaskByType(typeId)
  self.super.refreshTaskByType(self, typeId)
  local taskIds = L_TaskManager:getCurTaskIdsByTabType(typeId)
  if #taskIds < 1 then
    return false
  end
  local datas = {}
  local traceId = L_TaskStore:getTracedTask()
  for _, id in ipairs(taskIds) do
    local tplTask = _taskTpl:getTplById(id)
    local timeLimit, endTime = AzurWorld.TaskMgr:TryGetTaskEndTime(id)
    local taskLimit, limitTxt = self:tryGetTaskShortLimitTxt(id)
    local txtMap
    local list = C_TaskManager.GetTaskNodeCommonData(id)
    if list.Count > 0 then
      txtMap = C_TaskManager.GetCityBorthData(list[0].sceneId, list[0].birthId, true)
    end
    local namePos = string.isEmpty(txtMap) and C_Vector2(217, 0) or C_Vector2(217, 9.4)
    local data = {
      tabId = id,
      txtNameOn = _taskTpl:getName(tplTask),
      txtNameOff = _taskTpl:getName(tplTask),
      txtMapOn = txtMap or "",
      txtMapOff = txtMap or "",
      tacking = traceId == id,
      activeRed = L_CommonUtil.getLocalValue("NewTask_" .. id) == nil,
      anchoredPosNameOn = namePos,
      anchoredPosNameOff = namePos,
      activeLimitIcon = taskLimit,
      endTime = timeLimit and C_TimeUtility.DateTimeToTimestamp(endTime),
      showLimit = taskLimit,
      txtLimit = limitTxt
    }
    table.insert(datas, data)
  end
  for _, v in ipairs(self.modules.tabList) do
    v.bind.tabId = 0
  end
  self.bind.tabList:clear()
  self.bind.tabList:insert_array(datas)
  local targetTask = 0
  if self.curSelectedId and self.curSelectedId ~= 0 then
    targetTask = self.curSelectedId
  else
    targetTask = self.defaultOpenTaskId or datas[1].tabId or self.taskId
    self.curSelectedId = targetTask
  end
  self.defaultOpenTaskId = nil
  self.bind.selectTaskTabId = targetTask
  self.taskId = nil
  self:refreshTask(targetTask)
  return true
end

function this:tryGetTaskShortLimitTxt(taskId)
  local nodeId = C_TaskManager.GetExcuteNode(taskId)
  if nodeId then
    local stepId = L_TaskManager:GetTaskStepId(taskId, nodeId)
    local tplTaskStep = _taskStepTpl:getTplById(stepId)
    if tplTaskStep and _taskStepTpl:getIsLimitStep(tplTaskStep) then
      local infos = L_TaskManager:GetNodeInfoParam(taskId, nodeId)
      for i = 0, infos.Count - 1 do
        local taskInfoData = infos[i]
        if not taskInfoData.isComplate then
          local tplTaskTarget = _taskTargetTpl:getTplById(taskInfoData.conditionInfoId)
          if not tplTaskTarget then
            errorf(string.format("Can't get tplTaskTarget by id:%s, taskNodeId:%s", taskInfoData.conditionInfoId, taskInfoData.nodeId))
            return
          end
          return true, _taskTargetTpl:getShortLockText(tplTaskTarget)
        end
      end
    end
  end
end

function this:refreshTaskTraceInfo()
  local tplTask = _taskTpl:getTplById(self.taskId)
  if L_TaskStore:getTracedTask() == self.taskId then
    self.bind.txtNameOn = _taskTpl:getName(tplTask)
    self.bind.activeName = false
    self.bind.activeNameOn = true
  else
    self.bind.txtName = _taskTpl:getName(tplTask)
    self.bind.activeName = true
    self.bind.activeNameOn = false
  end
end

function this:showRewards(rewards)
  self.super.showRewards(self, rewards)
  if #self.bind.rewardList < 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)
  self.defaultOpenTaskId = taskId
end

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

return this
