local this = {}
local _iTaskTpl = L_GameTpl:getIconographyTaskTpl()

function this:req_CSProtoGetCatalogInfo(callback)
end

function this:rsp_syncPetCatalog(data)
  for i, v in pairs(data) do
    if not self.data.setHasInitSync then
      self.data.petCatalogMap[v] = 2
    else
      self.data.petCatalogMap[v] = 1
    end
  end
  self:call(self.event.refreshCatalog)
end

function this:rsp_syncPetCatalogTask(data)
  local handbook = data.infos
  if handbook then
    for i, v in pairs(handbook) do
      local petId = v.pet_id
      local playModule = L_WorldManager:getCurModule()
      local needFlyMsg = playModule == L_Const.worldModule.main and self.data.setCatalogHasInitSync
      local oldLv, oldExp, oldTaskCnt
      local cntChange = {}
      local isNew = true
      if self.data.petCatalog[petId] then
        oldTaskCnt = {}
        for _, tData in pairs(self.data.petCatalog[petId].tasks) do
          oldTaskCnt[tData.task_id] = tData.count
        end
        oldLv = self.data.petCatalog[petId].lv
        oldExp = self.data.petCatalog[petId].exp
        table.clear(self.data.petCatalog[petId])
        isNew = false
      elseif self.data.setCatalogHasInitSync then
        oldLv = 1
        oldExp = 0
        L_CommonUtil.setLocalValue("petCatalogNew" .. tostring(petId), true)
      end
      local tasks = {}
      for _, t in ipairs(v.tasks) do
        table.insert(tasks, {
          task_id = t.task_id,
          count = t.count,
          complete = t.complete
        })
        if isNew then
          table.insert(cntChange, {
            task_id = t.task_id,
            new = t.count,
            old = 0
          })
        elseif oldTaskCnt and oldTaskCnt[t.task_id] and t.count ~= oldTaskCnt[t.task_id] then
          table.insert(cntChange, {
            task_id = t.task_id,
            new = t.count,
            old = oldTaskCnt[t.task_id]
          })
        elseif oldTaskCnt and not oldTaskCnt[t.task_id] then
          local oldTaskId
          for tid, tCnt in pairs(oldTaskCnt) do
            local tempId = tid
            while _iTaskTpl:getTplById(tempId).nextTaskId ~= 0 do
              tempId = _iTaskTpl:getTplById(tempId).nextTaskId
              if tempId == t.task_id then
                oldTaskId = tid
                break
              end
            end
          end
          local taskTpl = _iTaskTpl:getTplById(oldTaskId)
          local target = taskTpl.taskcondition[#taskTpl.taskcondition] or 1
          table.insert(cntChange, {
            task_id = oldTaskId,
            new = target,
            old = oldTaskCnt[oldTaskId]
          })
        end
      end
      self.data.petCatalog[petId] = {
        lv = v.lv,
        exp = v.exp,
        tasks = tasks
      }
      self:calcPetAttr(petId)
      if 0 < #cntChange and oldLv and oldLv < v.lv then
        L_CommonUtil.setLocalValue("petCatalogTask" .. tostring(petId), true)
      end
      if needFlyMsg and 0 < #cntChange then
        local taskId = cntChange[1].task_id
        local isGetPet = self:_isGetPet(taskId)
        local taskTpl = L_GameTpl:getIconographyTaskTpl():getTplById(taskId)
        local target = taskTpl.taskcondition[#taskTpl.taskcondition] or 1
        local oldTaskTxt = L_Config:provider(taskTpl.taskname) .. " [" .. cntChange[1].old .. "/" .. tostring(target) .. "]"
        local newTaskTxt = L_Config:provider(taskTpl.taskname) .. " [" .. cntChange[1].new .. "/" .. tostring(target) .. "]"
        if oldLv and oldLv < v.lv then
          self:_tryShowPetCatalogTip({
            petId = petId,
            isTaskChange = true,
            isExpChange = true,
            isLvChange = true,
            oldTaskTxt = oldTaskTxt,
            newTaskTxt = newTaskTxt,
            oldExp = oldExp,
            newExp = v.exp,
            oldLv = oldLv,
            catalogLv = v.lv,
            taskId = taskId,
            isGetPet = isGetPet
          })
        elseif oldExp and oldExp < v.exp then
          self:_tryShowPetCatalogTip({
            petId = petId,
            isTaskChange = true,
            isExpChange = true,
            oldTaskTxt = oldTaskTxt,
            newTaskTxt = newTaskTxt,
            oldExp = oldExp,
            newExp = v.exp,
            oldLv = oldLv,
            taskId = taskId,
            isGetPet = isGetPet
          })
        else
          self:_tryShowPetCatalogTip({
            petId = petId,
            isTaskChange = true,
            oldTaskTxt = oldTaskTxt,
            newTaskTxt = newTaskTxt,
            taskId = taskId,
            isGetPet = isGetPet,
            oldExp = oldExp,
            oldLv = oldLv
          })
        end
      end
    end
  end
end

function this:_isGetPet(taskId)
  return _iTaskTpl:getTplById(taskId).taskcondition[1] == 13050
end

function this:_tryShowPetCatalogTip(tipInfo)
  if not tipInfo.isGetPet then
    L_FlyMsgManager:showPetCatalogTip(tipInfo)
    print("ShowPetCatalogTip not isGetPet")
    return
  end
  self.data.readyTipInfo = tipInfo
  self.data.catalogFlyReadyCnt = true
end

function this:_triggerCommonGetPet(pet, isNew)
  if not self.data.catalogFlyReadyCnt then
    if not isNew then
      L_FlyMsgManager:showGetPetTip(pet)
    end
  else
    local readyId = self.data.readyTipInfo.petId
    if readyId == pet.id then
      self.data.readyTipInfo.pet = pet
      L_FlyMsgManager:showPetCatalogTip(self.data.readyTipInfo)
      print("ShowPetCatalogTip isGetPet")
    else
      print("PetGuid not match. readyId = " .. tostring(readyId) .. "  getId = " .. tostring(pet.id))
      self.data.readyTipInfo = nil
    end
    self.data.catalogFlyReadyCnt = false
  end
end

return this
