local this = class("moduleMapTaskInfo", G_UIModuleBase)
local _worldCityTpl = L_GameTpl:getWorldCityTpl()
local _mapNodeTpl = L_GameTpl:getMapNodeTpl()
local _taskStepTpl = L_GameTpl:getTaskStepTpl()
local _worldAreaTpl = L_GameTpl:getWorldAreaTpl()

function this:ctor(...)
  this.super.ctor(self, ...)
end

function this.bind()
  return {
    icon = "",
    name = "",
    desc = "",
    txt_trace = "",
    show_desc = true,
    taskTarget = "",
    mapAreaName = "",
    rewardGroupList = {
      moduleName = "pages/map/bigMap/cellRewardGroupInfo"
    },
    taskInfoList = {
      moduleName = "pages/map/bigMap/cellMapTaskInfo"
    },
    isTraced = false,
    trace = true
  }
end

function this.methods()
  return {
    onClick_trace = function(self)
      if self.node.id == L_TaskStore:getTracedTask() then
        L_TaskStore:disposeTraceTask()
      else
        L_TaskStore:setTraceTask(self.node.id)
      end
    end,
    onClick_goto = function(self)
      self.parent:teleportNodeLogic()
    end
  }
end

function this:open()
  L_TaskStore:listenCallFunc(L_TaskStore.event.setCurrentTracedTask, self.refreshButtonState, self)
end

function this:close()
  L_TaskStore:unListenCallFunc(L_TaskStore.event.setCurrentTracedTask, self.refreshButtonState)
end

function this:initModule(node)
  self.node = node
  self.bind.icon = node.iconPath
  self.bind.show_desc = not string.isEmpty(node.desc) and true or false
  self.bind.desc = node.desc
  self.bind.name = node.name
  local sceneTpl = _worldCityTpl:getTplById(node.sceneId)
  self.bind.mapAreaName = node.mapAreaName
  if self.bind.show_desc then
    L_GameUtil.forceRebuildLayout(self.bindComponents.rect_desc)
  end
  if node.id then
    local data = {}
    local taskNodeId = C_TaskManager.GetExcuteNode(node.id)
    local stepId = L_TaskManager:GetTaskStepId(node.id, taskNodeId)
    local tplTaskStep = _taskStepTpl:getTplById(stepId)
    if tplTaskStep then
      self.bind.desc = _taskStepTpl:getDesc(tplTaskStep)
      local reward = _taskStepTpl:getRewardShow(tplTaskStep)
      if not table.isEmpty(reward) then
        table.insert(data, {
          desc = L_WordsTpl:getValue("ui_period_13"),
          reward = _taskStepTpl:getRewardShow(tplTaskStep)
        })
      end
    end
    local curReward = L_TaskStore:getTaskRewardItem(node.id)
    if not table.isEmpty(curReward) then
      table.insert(data, {
        desc = L_WordsTpl:getValue("ui_period_14"),
        reward = curReward
      })
    end
    self.bind.rewardGroupList:clear()
    self.bind.rewardGroupList:insert_array(data)
    local taskInfo = {}
    local infos = C_TaskManager.GetExecuteNodeInfoParam(node.id)
    if infos and infos.Count > 0 then
      for i = 0, infos.Count - 1 do
        local tips = L_TaskManager:getTaskNodeTagetText(infos[i], true)
        table.insert(taskInfo, {desc = tips})
      end
    end
    self.bind.taskInfoList:clear()
    if 0 < #taskInfo then
      self.bind.taskInfoList:insert_array(taskInfo)
    end
    self:refreshButtonState()
  end
  L_GameUtil.forceRebuildLayout(self.bindComponents.taskListTrans)
  L_GameUtil.forceRebuildLayout(self.bindComponents.infoTrans)
end

function this:refreshButtonState(taskId)
  local taskTpl = L_GameTpl:getTaskTpl()
  local tpl = taskTpl:getTplById(self.node.id)
  local taskType = taskTpl:getType(tpl)
  if taskType == L_Const.taskType.guide then
    self.bind.isTraced = true
    self.bind.trace = false
  else
    self.bind.isTraced = false
    self.bind.trace = true
    local curTracedTask = taskId or L_TaskStore:getTracedTask()
    if math.isEmpty(curTracedTask) then
      self.bind.txt_trace = L_WordsTpl:getValue("ui_moduleMapCollectionInfo_04")
    elseif curTracedTask == self.node.id then
      self.bind.txt_trace = L_WordsTpl:getValue("ui_moduleMapCollectionInfo_05")
    else
      self.bind.txt_trace = L_WordsTpl:getValue("ui_moduleMapCollectionInfo_04")
    end
  end
end

return this
