require "threads"
require "tinyandbig"

-- dieser trigger laesst big zum naechsten punkt springen 
function triggerBigTouchPortal(entity, trigger, portalId)
  if (entity.identifier == Big.IDENTIFIER) then
    if (portalId == nil) then
      print("No PortalID was given")
      return
    end
    
    entity:jumpToPortal(portalId)
    local level = scape.tinyandbig.Act.currentAct()
    level.worldModel.entities:remove(trigger)
  end
end

-- dieser trigger laesst tiny sterben
function triggerTinyDie(entity, trigger)
  if (entity.identifier == Tiny.IDENTIFIER) then
    entity:die(DeadState.MOD_HITBYSTONE)
  end
end

-- dieser trigger beendet das spiel
function triggerGameSuccess(entity, trigger)
  if (entity.identifier == Big.IDENTIFIER) then
    local level = scape.tinyandbig.Act.currentAct()
    level.worldModel.entities:remove(trigger)
    print("Game succeed")
    local level = scape.tinyandbig.Act.currentAct()
    level:succeed()
   
  end
end


function triggerCrystalImpact(entity, trigger)
  if (entity.identifier == Crystal.IDENTIFIER) then
    local level = scape.tinyandbig.Act.currentAct()
    level.worldModel.entities:remove(entity)
    local entity = level.currentAct().worldModel:getTiny() 
    entity.cameraController:shake(30)
  end
end

-- dieser trigger beendet das spiel
function triggerGameFailed(entity, trigger)
  if (entity.identifier == Tiny.IDENTIFIER) then
    scape.tinyandbig.Level.currentAct():exit()
    scape.tinyandbig.Level.currentAct().nextAct = "menu.lua"
  end
end

-- dieser trigger laesst big zum naechsten punkt springen
-- wenn er von tiny beruehrt wird
function triggerTinyTouchPortal(entity, trigger, portalId)
  if (entity.identifier == Tiny.IDENTIFIER) then
    if (portalId == nil) then
      print("No PortalID was given")
      return
    end
    local level = scape.tinyandbig.Act.currentAct()
    local big = level.worldModel:getBig()
    print("Big jump to portal " .. portalId)
    if (big) then
      big:jumpToPortal(portalId)
    end
    level.worldModel.entities:remove(trigger)
  end
end

function triggerSave(entity, trigger)
  if (entity.identifier == Tiny.IDENTIFIER) then
    local level = scape.tinyandbig.Act.currentAct()
    level.worldModel:backup()
    entity.currentSaveMonkey = nil
    entity.resetPosition = entity.node.translation
    level.worldModel.entities:remove(trigger)
  end
end

function triggerFallingStones(entity, trigger, id)
  if (entity.identifier == Tiny.IDENTIFIER) then
    print("triggerFallingStones" .. tostring(id) )
    if (id and type(id) == "number") then
      Crystal.activateGroup(id)
      Crystal.setHiddenToGroup(id, false)
    end
  end
end

function triggerTinyBeam(entity, trigger, portalId)
  if (entity.identifier == Tiny.IDENTIFIER) then
    entity:jumpToPortal(portalId)
    local level = scape.tinyandbig.Act.currentAct()
    level.worldModel.entities:remove(trigger)
  end
end

function triggerCameraShake(entity, trigger, intensity)
  if (entity.identifier == Tiny.IDENTIFIER) then
    entity.cameraController:shake(intensity)
    local level = scape.tinyandbig.Act.currentAct()
    level.worldModel.entities:remove(trigger)
  end
end

function triggerSwitchAmbient(entity, trigger, soundwhat)
  if (entity.identifier == Tiny.IDENTIFIER) then
    scape.tinyandbig.Act.currentAct().ambient.ambientSoundSystemController:play(soundwhat)
    local level = scape.tinyandbig.Act.currentAct()
    level.worldModel.entities:remove(trigger)
  end
end

function triggerDialog(entity, trigger, dialogIdentifier)
  if (entity.identifier == Tiny.IDENTIFIER or entity.identifier == Big.IDENTIFIER) then
    local level = scape.tinyandbig.Act.currentAct()
    level.dialogSystem:showDialog(dialogIdentifier)
    level.worldModel.entities:remove(trigger)  
  end
end

function triggerTutorialDialog(entity, trigger, dialogIdentifier)
  if (entity.identifier ==  Crystal.IDENTIFIER) then
    local level = scape.tinyandbig.Act.currentAct()
    level.dialogSystem:showDialog(dialogIdentifier)
    level.worldModel.entities:remove(trigger)  
  end
end

function triggerFallingColossus(entity, trigger, id)
  if (entity.identifier ==  Crystal.IDENTIFIER and entity.groupId ~=id ) then
    local level = scape.tinyandbig.Act.currentAct()
    level.worldModel.entities:remove(trigger)
    print("triggerFallingCollos " .. tostring(id) )
    if (id and type(id) == "number") then
      Crystal.activateGroup(id)
    end
  end
end
  
