local this = {}

function this:setTraceTask(taskID, cb)
  if L_BattleDataManager:checkPlayerBattle() then
    L_FlyMsgManager:showNormalMsg(L_BattleDataManager:getBattleBlockTips(L_Const.BattleBlockType.SetTaskTrace))
    return
  end
  C_TaskManager.ReqTaskClientTrace(taskID, true, function(errorCode)
    if errorCode == L_Const.errorCode.ErrCodeSucc then
      self:call(self.event.setCurrentTracedTask, taskID)
      if cb then
        cb()
      end
    end
  end)
end

function this:disposeTraceTask(cb)
  if L_BattleDataManager:checkPlayerBattle() then
    L_FlyMsgManager:showNormalMsg(L_BattleDataManager:getBattleBlockTips(L_Const.BattleBlockType.SetTaskTrace))
    return
  end
  local taskId = self:getTracedTask()
  if 0 < taskId then
    C_TaskManager.ReqTaskClientTrace(taskId, false, function(errorCode)
      if errorCode == L_Const.errorCode.ErrCodeSucc then
        self:call(self.event.setCurrentTracedTask)
        if cb then
          cb()
        end
      end
    end)
  end
end

function this:req_creatSubmitItemData(subType, taskId, nodeId, index, itemList, callBack)
  local subData = {
    task_id = taskId,
    node_id = nodeId,
    node_index = index
  }
  if itemList ~= nil then
    local items = {}
    for i, sub in pairs(itemList) do
      table.insert(items, {
        item_id = sub.itemId,
        item_type = sub.itemType,
        item_count = sub.count,
        item_guid = sub.itemGuid
      })
    end
    subData.items = items
  end
  if subType == L_Const.taskClientEvent.submitItem then
    self:req_doTaskSubmitItem(subData, callBack)
  elseif subType == L_Const.taskClientEvent.submitTaskItem then
    self:req_doTaskSubmitItem(subData, callBack)
  elseif subType == L_Const.taskClientEvent.submitTypeItem then
    self:req_doTaskSubmitItemType(subData, callBack)
  elseif subType == L_Const.taskClientEvent.submitGroupItem then
    self:req_doTaskSubmitItemChoose(subData, callBack)
  end
end

function this:req_doTaskSubmitItem(subData, callback)
  L_Net:sendMessage(MsgGenCode.CSProtoTaskSubmitItem, subData, function(rspData, errorCode)
    if errorCode == L_Const.errorCode.ErrCodeSucc then
      if callback then
        callback()
      end
    else
      L_FlyMsgManager:showNormalMsg(L_WordsTpl:getValue("notice_takeAction"))
    end
  end)
end

function this:req_doTaskSubmitItemChoose(subData, callback)
  L_Net:sendMessage(MsgGenCode.CSProtoTaskSubmitItemChoose, subData, function(rspData, errorCode)
    if errorCode == L_Const.errorCode.ErrCodeSucc then
      if callback then
        callback()
      end
    else
      L_FlyMsgManager:showNormalMsg(L_WordsTpl:getValue("notice_takeAction"))
    end
  end)
end

function this:req_doTaskSubmitItemType(subData, callback)
  L_Net:sendMessage(MsgGenCode.CSProtoTaskSubmitItemType, subData, function(rspData, errorCode)
    if errorCode == L_Const.errorCode.ErrCodeSucc then
      if callback then
        callback()
      end
    else
      L_FlyMsgManager:showNormalMsg(L_WordsTpl:getValue("notice_takeAction"))
    end
  end)
end

return this
