local PveLevelTemplate = BaseClass("PveLevelTemplate")
local Const = require("Scene.PVEBattleLevel.Const")
local ConstLW = require("Scene.LWBattle.Const")
local __init = function(self)
  self.id = 0
  self.spawnPos = {x = 0, y = 0}
  self.spawnRot = 180
  self.triggerList = {}
  self.scene = ""
  self.name = 0
  self.type = PveLevelType.NormalLevel
  self.reEnter = false
  self.buildCondition = 0
  self.order = 0
  self.enterResourceCost = {}
  self.enterResourceItemCost = {}
  self.enterItemCost = {}
  self.needArmy = {level = 0, count = 0}
  self.resultShowType = PveResultShowType.Normal
  self.timeLimitList = {}
  self.timeRewardList = {}
  self.maxArmyCount = {}
  self.maxHeroCount = 5
  self.maxHeroLevel = IntMaxValue
  self.progressType = nil
  self.progressIcon = nil
  self.heroSize = nil
  self.triggerExpSize = nil
  self.camera = nil
  self.axeSize = nil
  self.carry = nil
  self.zombiePath = {}
  self.player_model = ""
  self.task = {}
  self.hp_show = PveShowBloodType.No
  self.resCheck = {}
  self.showSkill = PveShowSkill.Show
  self.zoomOn = 0
  self.fog_type = FogType.Black
  self.bgm = ""
  self.enterTips = {}
  self.Ymove = Const.YMoveType.No
  self.benSkin = ""
  self.darkCornerType = Const.DarkCornerType.None
  self.fogVolumeProfile = ""
  self.sceneCfgArr = nil
  self.stageMeta = nil
end
local __delete = function(self)
  self.id = nil
  self.spawnPos = nil
  self.triggerList = nil
  self.scene = nil
  self.name = nil
  self.type = nil
  self.reEnter = nil
  self.buildCondition = nil
  self.order = nil
  self.enterResourceCost = nil
  self.enterItemCost = nil
  self.needArmy = nil
  self.resultShowType = nil
  self.heroSize = nil
  self.triggerExpSize = nil
  self.camera = nil
  self.axeSize = nil
  self.carry = nil
  self.timeLimitList = nil
  self.timeRewardList = nil
  self.maxArmyCount = nil
  self.maxHeroCount = nil
  self.maxHeroLevel = nil
  self.heroSize = nil
  self.triggerExpSize = nil
  self.camera = nil
  self.axeSize = nil
  self.zombiePath = nil
  self.player_model = nil
  self.task = nil
  self.resCheck = nil
  self.showSkill = nil
  self.zoomOn = nil
  self.fog_type = FogType.Black
  self.bgm = ""
  self.enterTips = nil
  self.Ymove = Const.YMoveType.No
  self.benSkin = nil
  self.darkCornerType = nil
  self.fogVolumeProfile = nil
  self.sceneCfgArr = nil
  self.stageMeta = nil
end
local InitData = function(self, row)
  if row == nil then
    return
  end
  self.id = tonumber(row:getValue("id")) or 0
  local spawnPosStrs = string.split(row:getValue("SpawnPos") or "", ",")
  if #spawnPosStrs == 2 then
    self.spawnPos.x = tonumber(spawnPosStrs[1]) or 0
    self.spawnPos.y = tonumber(spawnPosStrs[2]) or 0
  end
  self.triggerList = {}
  local triggerStrs = string.split(row:getValue("Triggers") or "", ";")
  for _, str in ipairs(triggerStrs) do
    table.insert(self.triggerList, tonumber(str))
  end
  self.initTriggerList = {}
  local initTriggerStrs = string.split(row:getValue("init_trigger") or "", ";")
  for _, str in ipairs(initTriggerStrs) do
    table.insert(self.initTriggerList, tonumber(str))
  end
  self.scene = row:getValue("Scene") or ""
  self.name = tonumber(row:getValue("name")) or 0
  self.type = tonumber(row:getValue("type")) or PveLevelType.NormalLevel
  self.reEnter = tonumber(row:getValue("reEnter")) == 1
  self.buildCondition = tonumber(row:getValue("build_condition")) or 0
  self.order = tonumber(row:getValue("order")) or 0
  self.enterResourceCost = {}
  local enterResourceCostStrs = string.split(row:getValue("enter_resource_cost") or "", ";")
  for _, str in ipairs(enterResourceCostStrs) do
    local spls = string.split(str, ",")
    if #spls == 2 then
      self.enterResourceCost[tonumber(spls[1])] = tonumber(spls[2])
    end
  end
  self.enterResourceItemCost = {}
  local enterResourceItemStrs = string.split(row:getValue("enter_resource_item_cost") or "", ";")
  for _, str in ipairs(enterResourceItemStrs) do
    local spls = string.split(str, ",")
    if #spls == 2 then
      self.enterResourceItemCost[tonumber(spls[1])] = tonumber(spls[2])
    end
  end
  self.enterItemCost = {}
  local enterItemCostStrs = string.split(row:getValue("enter_item_cost") or "", ";")
  for _, str in ipairs(enterItemCostStrs) do
    local spls = string.split(str, ",")
    if #spls == 2 then
      self.enterItemCost[tonumber(spls[1])] = tonumber(spls[2])
    end
  end
  local needArmyStrs = string.split(row:getValue("need_army") or "", ";")
  if #needArmyStrs == 2 then
    self.needArmy = {
      level = tonumber(needArmyStrs[1]) or 0,
      count = tonumber(needArmyStrs[2]) or 0
    }
  end
  self.resultShowType = tonumber(row:getValue("ResultShow")) or PveResultShowType.Normal
  local heroSize = row:getValue("hero_size")
  if heroSize then
    self.heroSize = tonumber(heroSize)
  end
  local axeSize = row:getValue("axe_size")
  if axeSize then
    self.axeSize = tonumber(axeSize)
  end
  local camera = string.split_ff_array(row:getValue("camera"), ";")
  if camera and #camera == 3 then
    self.camera = {}
    self.camera.height = camera[1]
    self.camera.rotation = camera[2]
    self.camera.fov = camera[3]
  end
  local triggerExpSize = string.split_ff_array(row:getValue("trigger_exp_size") or "", ";")
  if triggerExpSize and #triggerExpSize == 2 then
    self.triggerExpSize = {}
    self.triggerExpSize.min = triggerExpSize[1]
    self.triggerExpSize.max = triggerExpSize[2]
  end
  self.progressType = tonumber(row:getValue("ProgressType"))
  self.progressIcon = row:getValue("ProgressPara")
  self.maxArmyCount = {}
  local maxArmyCountStr = row:getValue("MaxArmyCount") or ""
  if not string.IsNullOrEmpty(maxArmyCountStr) then
    local strs = string.split(maxArmyCountStr, "|")
    if #strs == 2 then
      local mainLvStrs = string.split(strs[1], ";")
      local countStrs = string.split(strs[2], ";")
      if #mainLvStrs == #countStrs then
        for i = 1, #mainLvStrs do
          local t = {}
          t.mainLv = tonumber(mainLvStrs[i]) or 0
          t.count = tonumber(countStrs[i]) or 0
          table.insert(self.maxArmyCount, t)
        end
      end
    end
  end
  self.maxHeroCount = tonumber(row:getValue("MaxHeroCount")) or 5
  self.maxHeroLevel = tonumber(row:getValue("MaxHeroLevel")) or IntMaxValue
  local carry = row:getValue("carry")
  if carry ~= nil and carry ~= "" then
    local spl = string.split_ii_array(carry, ";")
    if 2 <= #spl then
      self.carry = {}
      self.carry.row = spl[1]
      self.carry.perRowCount = spl[2]
    end
  end
  self.zombiePath = {}
  local zombiePathStr = row:getValue("ZombiePath")
  if not string.IsNullOrEmpty(zombiePathStr) then
    local zombiePathStrArray = string.split_ss_array(zombiePathStr, "|")
    for _, pathStr in ipairs(zombiePathStrArray) do
      local pathStrArray = string.split_ss_array(pathStr, ";")
      if 2 <= #pathStrArray then
        local id = tonumber(pathStrArray[1])
        local path = {}
        for i = 2, #pathStrArray do
          local x, y = string.split_ii(pathStrArray[i], ",")
          path[#path + 1] = {x = x, y = y}
        end
        self.zombiePath[id] = path
      end
    end
  end
  self.timeLimitList = {}
  local timeLimitStr = string.split(row:getValue("time_limit") or "", ";")
  for _, str in ipairs(timeLimitStr) do
    table.insert(self.timeLimitList, tonumber(str))
  end
  self.timeRewardList = {}
  local timeRewardStr = string.split(row:getValue("time_reward") or "", ";")
  for _, str in ipairs(timeRewardStr) do
    table.insert(self.timeRewardList, tonumber(str))
  end
  local initRot = row:getValue("init_rotation")
  if not string.IsNullOrEmpty(initRot) then
    self.spawnRot = tonumber(initRot) or 180
  end
  self.player_model = row:getValue("player_model")
  if self.player_model == nil or self.player_model == "" then
    self.player_model = Const.DefinePlayerName
  end
  self.task = {}
  local task = string.split(row:getValue("quest") or "", ";")
  for _, str in ipairs(task) do
    table.insert(self.task, tonumber(str))
  end
  local hp_show = row:getValue("hp_show")
  if hp_show == nil or hp_show == "" then
    self.hp_show = PveShowBloodType.No
  else
    self.hp_show = tonumber(hp_show)
  end
  self.resCheck = {}
  local resCheckStr = row:getValue("resource_check") or ""
  for _, str in ipairs(string.split(resCheckStr, "|")) do
    local spls = string.split(str, ";")
    if #spls == 2 then
      self.resCheck[tonumber(spls[1])] = tonumber(spls[2])
    end
  end
  local Skill_Unclock = row:getValue("Skill_Unclock")
  if Skill_Unclock == nil or Skill_Unclock == "" then
    self.showSkill = PveShowSkill.Show
  else
    self.showSkill = tonumber(Skill_Unclock)
  end
  self.zoomOn = tonumber(row:getValue("zoom_on")) or 0
  local fog_type = row:getValue("fog_type")
  if fog_type == nil or fog_type == "" then
    self.fog_type = FogType.Black
  else
    self.fog_type = tonumber(fog_type)
  end
  self.bgm = row:getValue("bgm")
  self.enterTips = {}
  local enterTipStr = row:getValue("enter_tips") or ""
  if enterTipStr ~= "" then
    for _, str in ipairs(string.split(enterTipStr, "|")) do
      local spls = string.split(str, ";")
      local t = {}
      t.leftText = spls[1]
      t.rightText = spls[2]
      t.pic = spls[3]
      table.insert(self.enterTips, t)
    end
  end
  self.Ymove = tonumber(row:getValue("Ymove")) or Const.YMoveType.No
  self.benSkin = row:getValue("ben_skin") or ""
  self.darkCornerType = tonumber(row:getValue("dark_corner")) or Const.DarkCornerType.None
  self.fogVolumeProfile = row:getValue("fog_volume_profile") or ""
end
local InitByLWData = function(self, stageMeta)
  self.bgm = stageMeta:getValue("bgm")
  self.id = tonumber(stageMeta:getValue("id")) or 1
  self.type = PveLevelType.ZombieBattle
  self.sceneCfgArr = {}
  self.stageMeta = stageMeta
  LocalController:instance():visitTable(TableName.LW_StageGroup, function(id, lineData)
    local spl = string.split(lineData.stage_group, "|")
    for k, stageId in ipairs(spl) do
      if tonumber(stageId) == stageMeta.id then
        local groupId = id
        self.stageGroupMeta = LocalController:instance():getLine(TableName.LW_StageGroup, groupId)
        self.isFinalStage = k == #spl
        return true
      end
    end
  end)
  local sceneIdArray = string.split(stageMeta:getValue("scene") or "", ",")
  local offset = 0
  for _, sceneId in ipairs(sceneIdArray) do
    local sceneMeta = LocalController:instance():getLine(LuaEntry.Player:GetABTestTableName(TableName.LW_Scene), sceneId)
    local sceneCfg = {}
    sceneCfg.meta = sceneMeta
    sceneCfg.offset = offset
    sceneCfg.isDeco = false
    table.insert(self.sceneCfgArr, sceneCfg)
    offset = offset + sceneMeta.scene_size
  end
  local sceneMeta = LocalController:instance():getLine(LuaEntry.Player:GetABTestTableName(TableName.LW_Scene), stageMeta:getValue("scene_tail"))
  local sceneCfg = {}
  sceneCfg.meta = sceneMeta
  sceneCfg.offset = offset
  sceneCfg.isDeco = true
  table.insert(self.sceneCfgArr, sceneCfg)
  offset = offset + sceneMeta.scene_size
  sceneCfg = {}
  sceneCfg.meta = sceneMeta
  sceneCfg.offset = -1 * sceneMeta.scene_size
  sceneCfg.isDeco = true
  table.insert(self.sceneCfgArr, sceneCfg)
  self.winCondition = {}
  local winParam = string.split(self.stageMeta.win_condition, "|")
  local winType = tonumber(winParam[1])
  self.winCondition.winType = winType
  if winType == ConstLW.StageWinType.KillMonster then
    self.winCondition.needKillNum = tonumber(winParam[2])
  elseif winType == ConstLW.StageWinType.KillTargetMonster then
    self.winCondition.needKillTargetId = tonumber(winParam[2])
    self.winCondition.needKillNum = tonumber(winParam[3])
  elseif winType == ConstLW.StageWinType.Time then
    self.winCondition.timeLimit = tonumber(winParam[2])
  elseif winType == ConstLW.StageWinType.KillBoss then
    self.winCondition.needKillBossNum = tonumber(winParam[2])
  end
  self.triggerCfgArr = {}
  if not string.IsNullOrEmpty(self.stageMeta.trigger_item) then
    local triggers = string.split(self.stageMeta.trigger_item, ",")
    for _, trigger in ipairs(triggers) do
      local rawCfg = string.split(trigger, "|")
      local cfg = {}
      cfg.wayPointIndex = tonumber(rawCfg[1])
      cfg.triggerMetaId = tonumber(rawCfg[2])
      table.insert(self.triggerCfgArr, cfg)
    end
  end
  self.plotTriggerCfgArr = {}
  for _, plotCfg in ipairs(self.stageMeta.trigger_plot) do
    local cfgArr = string.split(plotCfg, ",")
    local cfg = {}
    cfg.wayPointIndex = tonumber(cfgArr[1])
    cfg.plotGroupId = tonumber(cfgArr[2])
    cfg.playTimes = tonumber(cfgArr[3])
    cfg.playInterval = tonumber(cfgArr[4])
    table.insert(self.plotTriggerCfgArr, cfg)
  end
  self.camera = {}
  self.camera.fov = 60
  self.camera.height = 20
  self.camera.rotation = 45
end
local IsTimeLimited = function(self)
  return #self.timeLimitList > 0
end
local IsStarLevel = function(self)
  return #self.timeRewardList > 0
end
local GetCarryMaxNum = function(self)
  if self.carry ~= nil then
    return self.carry.row * self.carry.perRowCount
  end
  return 0
end
local GetCarryRow = function(self)
  if self.carry ~= nil then
    return self.carry.row
  end
  return 0
end
local GetCarryPerRowCount = function(self)
  if self.carry ~= nil then
    return self.carry.perRowCount
  end
  return 0
end
local GetMaxArmyCount = function(self)
  local count = 0
  local mainLv = DataCenter.BuildManager.MainLv
  for _, v in ipairs(self.maxArmyCount) do
    count = v.count
    if mainLv <= v.mainLv then
      break
    end
  end
  return count
end
local IsUseMoveY = function(self)
  return self.Ymove == Const.YMoveType.Yes
end
local GetRandomEnterTip = function(self)
  local tip
  if #self.enterTips > 0 then
    tip = table.randomArrayValue(self.enterTips)
  end
  return tip
end
local GetFirstBattleMonsterTemplate = function(self)
  if self.type ~= PveLevelType.FightLevel then
    return nil
  end
  for _, triggerId in ipairs(self.triggerList) do
    local triggerType = tonumber(GetTableData(TableName.PVETrigger, tonumber(triggerId), "UnclockType")) or 0
    if triggerType == Const.TriggerType.Monster then
      local monsterId = tonumber(GetTableData(TableName.PVETrigger, tonumber(triggerId), "UnclockPara")) or 0
      return DataCenter.MonsterTemplateManager:GetMonsterTemplate(tonumber(monsterId))
    end
  end
  return nil
end
PveLevelTemplate.__init = __init
PveLevelTemplate.__delete = __delete
PveLevelTemplate.InitData = InitData
PveLevelTemplate.IsTimeLimited = IsTimeLimited
PveLevelTemplate.IsStarLevel = IsStarLevel
PveLevelTemplate.GetCarryMaxNum = GetCarryMaxNum
PveLevelTemplate.GetCarryRow = GetCarryRow
PveLevelTemplate.GetCarryPerRowCount = GetCarryPerRowCount
PveLevelTemplate.GetMaxArmyCount = GetMaxArmyCount
PveLevelTemplate.IsUseMoveY = IsUseMoveY
PveLevelTemplate.GetRandomEnterTip = GetRandomEnterTip
PveLevelTemplate.GetFirstBattleMonsterTemplate = GetFirstBattleMonsterTemplate
PveLevelTemplate.InitByLWData = InitByLWData
return PveLevelTemplate
