local this = {}
local _dungeonEntrustDifficultyTpl = L_GameTpl:getDungeonEntrustDifficultyTpl()
local _dungeonEntrustTpl = L_GameTpl:getDungeonEntrustTpl()
local _dungeonEntrustRewardTpl = L_GameTpl:getDungeonEntrustRewardTpl()

function this:init()
  this.super.init(self)
  self.data = {
    entrustInfo = {},
    entrustReward = {},
    trainTaskRecord = {},
    entrustKiboInfo = {}
  }
end

function this:getFirstCanReceiveStarNum(entrustType, difficulty)
  difficulty = difficulty or 0
  local modeInfo = _dungeonEntrustRewardTpl:getInfoByDifficulty(entrustType, difficulty)
  if not modeInfo then
    return 0, -1
  end
  for _, v in ipairs(modeInfo) do
    if not self:checkEntrustRewardReceived(v.id) then
      return v.starNum, v.id
    end
  end
  return modeInfo[#modeInfo].starNum, -1
end

function this:getCanReceiveRewardIdList(entrustType, curStarNum, difficulty)
  difficulty = difficulty or 0
  local modeInfo = _dungeonEntrustRewardTpl:getInfoByDifficulty(entrustType, difficulty)
  if not modeInfo then
    return nil
  end
  local rewardIdList
  for _, v in ipairs(modeInfo) do
    if curStarNum >= v.starNum and not self:checkEntrustRewardReceived(v.id) then
      rewardIdList = rewardIdList or {}
      table.insert(rewardIdList, v.id)
    end
  end
  return rewardIdList
end

function this:getEntrustStarNumByDifficulty(entrustType, difficulty)
  local starNum = 0
  if entrustType == L_Const.EntrustType.AreaEntrust then
    for id, v in pairs(self.data.entrustInfo) do
      local cfg = _dungeonEntrustTpl:getTplById(id)
      if _dungeonEntrustTpl:getDifficulty(cfg) == difficulty then
        starNum = starNum + v.entrust_star
      end
    end
    return starNum
  elseif entrustType == L_Const.EntrustType.PetDuel then
    for id, v in pairs(self.data.entrustKiboInfo) do
      starNum = starNum + v.entrust_star
    end
  end
  return starNum
end

function this:getEntrustLevelStar(levelId)
  if not self.data.entrustInfo[levelId] then
    return 0
  end
  return self.data.entrustInfo[levelId].entrust_star
end

function this:getEntrustKiboLevelStar(levelId)
  if not self.data.entrustKiboInfo[levelId] then
    return 0
  end
  return self.data.entrustKiboInfo[levelId].entrust_star
end

function this:checkEntrustKiboBattleSuc(levelIdLevel)
  if levelIdLevel == nil then
    return true
  end
  if #levelIdLevel == 0 then
    return true
  elseif 1 < #levelIdLevel then
    errorf("kibo battle levelIdLevel length > 1")
  end
  local levelId = levelIdLevel[1]
  if self.data.entrustKiboInfo[levelId] == nil then
    return false
  end
  return 0 < self.data.entrustKiboInfo[levelId].entrust_star
end

function this:isEntrustKiboBattleLevel(levelId)
  if levelId == nil then
    return false
  end
  local duelGroupTpl = L_GameTpl:getKiBoDuelGroupTpl()
  local list = duelGroupTpl:getValue()
  for level_id, v in pairs(list) do
    printf("Test", level_id)
    if level_id == levelId then
      return true
    end
  end
  return false
end

function this:checkEntrustLevelSuc(levelId)
  if not levelId then
    return false
  end
  return self:getEntrustLevelStar(levelId) > 0
end

function this:checkEntrustRewardReceived(rewardId)
  if not self.data.entrustReward[rewardId] then
    return false
  end
  return self.data.entrustReward[rewardId].reward_time > 0
end

function this:checkDifficultyUnlock(difficulty, bShowTips)
  local cfg = _dungeonEntrustDifficultyTpl:getTplById(difficulty)
  local isComplete, results, lockIndex = L_ConditionManager:isComplete(_dungeonEntrustDifficultyTpl:getUnlockCondition(cfg))
  if not bShowTips then
    return isComplete
  end
  if not isComplete then
    local results = L_ConditionManager:getDescs(_dungeonEntrustDifficultyTpl:getUnlockCondition(cfg))
    L_FlyMsgManager:showNormalMsg(results[lockIndex])
    return false
  end
  return true
end

function this:checkEntrustLevelShow(levelId)
  local cfg = _dungeonEntrustTpl:getTplById(levelId)
  local isComplete, results, lockIndex = L_ConditionManager:isComplete(_dungeonEntrustTpl:getTaskShow(cfg))
  return isComplete
end

function this:checkEntrustLevelUnlock(levelId)
  local cfg = _dungeonEntrustTpl:getTplById(levelId)
  local isComplete, results, lockIndex = L_ConditionManager:isComplete(_dungeonEntrustTpl:getTaskUnlock(cfg))
  if not isComplete then
    local results = L_ConditionManager:getDescs(_dungeonEntrustTpl:getTaskUnlock(cfg))
    return isComplete, results[lockIndex]
  end
  return isComplete
end

function this:getIsTrainTaskCompleted(trainTaskId)
  return self.data.trainTaskRecord[trainTaskId] == true
end

function this:getCurChallengeEntrustId()
  return self.data.curChallengeEntrustId
end

function this:setCurChallengeEntrustId(id)
  self.data.curChallengeEntrustId = id
end

return this
