local this = class("worldTriggerTpl")

function this:init(config)
  self.data = {}
  for i, v in pairs(config) do
    if self.data[v.triggerId] == nil then
      self.data[v.triggerId] = {}
    end
    table.insert(self.data[v.triggerId], v)
  end
  for _, v in pairs(self.data) do
    table.sort(v, function(a, b)
      return a.step < b.step
    end)
  end
end

function this:getTpl(gId, index)
  local list = self.data[gId]
  if list == nil then
    errorf("world trigger \233\148\153\232\175\175,\233\133\141\231\189\174\228\184\141\229\173\152\229\156\168" .. gId)
    return nil
  end
  for _, v in ipairs(list) do
    if v.step == index then
      return v
    end
  end
  return list
end

function this:getTplListByStep(gId, step)
  local list = self.data[gId]
  local retList = {}
  if list == nil then
    errorf("world trigger \233\148\153\232\175\175,\233\133\141\231\189\174\228\184\141\229\173\152\229\156\168" .. gId)
    return retList
  end
  for _, v in ipairs(list) do
    if v.step == step then
      table.insert(retList, v)
    end
  end
  return retList
end

function this:getTplByStepAndIndex(gId, step, index)
  local list = self.data[gId]
  local count = 0
  if list == nil then
    errorf("world trigger \233\148\153\232\175\175,\233\133\141\231\189\174\228\184\141\229\173\152\229\156\168" .. gId)
    return nil
  end
  for _, v in ipairs(list) do
    if v.step == step then
      if count == index then
        return v
      end
      count = count + 1
    end
  end
  return nil
end

function this:getType(tpl)
  return tpl.type
end

function this:getParam(tpl)
  return tpl.param
end

function this:getBattleConfigId(tpl)
  local param = self:getParam(tpl)
  if table.isEmpty(param) then
    return
  end
  if table.isEmpty(param[1]) then
    return
  end
  return param[1][1]
end

function this:getConditionState(tpl)
  return tpl.conditionState
end

function this:getState(tpl)
  return tpl.state
end

function this:getConditionItem(tpl)
  return tpl.conditionItem
end

function this:getCondtionTask(tpl)
  return tpl.conditionTask
end

function this:getConditionBattle(tpl)
  return tpl.conditionBattle
end

function this:getCount(tpl)
  return tpl.count
end

function this:getPlayAnim(tpl)
  return tpl.playAnim
end

function this:getNextStep(tpl)
  return tpl.nextStep
end

function this:getResetTime(tpl)
  return tpl.resetTime
end

function this:getFreezeState(tpl)
  return tpl.freezeState
end

function this:getCostResource(tpl)
  return tpl.costResource
end

function this:getStepReward(tpl)
  return tpl.stepReward
end

function this:getHasState(tpl_worldMap, state)
  local worldMapTpl = L_GameTpl:getWorldMapTpl()
  local triggerId = worldMapTpl:getTriggerId(tpl_worldMap)
  if triggerId == 0 then
    return false
  end
  local _, index = table.ipairsFind(self.data[triggerId], function(value, index)
    return state == value.state
  end)
  if not index then
    return false
  end
  return true
end

function this:hasCompleteEffect(tpl)
  return tpl.stepCompleteEffect ~= ""
end

function this:getCompleteEffect(tpl)
  return tpl.stepCompleteEffect
end

function this:hasCompleteSE(tpl)
  return #tpl.stepCompleteSE > 0
end

function this:getCompleteSE(tpl)
  return tpl.stepCompleteSE[1], tonumber(tpl.stepCompleteSE[2] or 0)
end

return this
