/**
 * UEFN (Unreal Editor for Fortnite) Verse Templates
 * Verse is Epic's new programming language for UEFN
 */

import { ScriptTemplate } from './templates';

export const uefnTemplates: ScriptTemplate[] = [
  {
    id: 'uefn-hello-world',
    name: 'Hello World',
    description: 'Basic Verse script to print messages',
    category: 'beginner',
    platform: 'uefn',
    code: `using { /Fortnite.com/Devices }
using { /Verse.org/Simulation }
using { /UnrealEngine.com/Temporary/Diagnostics }

hello_world_device := class(creative_device):

    OnBegin<override>()<suspends>:void=
        Print("Hello from UEFN!")
        Print("Welcome to Verse programming!")`,
  },
  {
    id: 'uefn-player-tracker',
    name: 'Player Join Handler',
    description: 'Detect when players join and leave the game',
    category: 'beginner',
    platform: 'uefn',
    code: `using { /Fortnite.com/Game }
using { /Fortnite.com/Characters }
using { /Verse.org/Simulation }
using { /UnrealEngine.com/Temporary/Diagnostics }

player_tracker_device := class(creative_device):

    OnBegin<override>()<suspends>:void=
        # Subscribe to player events
        GetPlayspace().PlayerAddedEvent().Subscribe(OnPlayerAdded)
        GetPlayspace().PlayerRemovedEvent().Subscribe(OnPlayerRemoved)

    OnPlayerAdded(Player:player):void=
        Print("Player joined: {Player}")

        # Get the player's character
        if (FortCharacter := Player.GetFortCharacter[]):
            Print("Character loaded for player")

    OnPlayerRemoved(Player:player):void=
        Print("Player left: {Player}")`,
  },
  {
    id: 'uefn-button-interaction',
    name: 'Button Interaction',
    description: 'Handle button press interactions in UEFN',
    category: 'ui',
    platform: 'uefn',
    code: `using { /Fortnite.com/Devices }
using { /Verse.org/Simulation }
using { /UnrealEngine.com/Temporary/Diagnostics }

button_handler_device := class(creative_device):

    @editable
    MyButton : button_device = button_device{}

    OnBegin<override>()<suspends>:void=
        # Subscribe to button interaction
        MyButton.InteractedWithEvent.Subscribe(OnButtonPressed)

    OnButtonPressed(Agent:agent):void=
        Print("Button pressed by agent!")

        # Activate the button (visual feedback)
        MyButton.Activate(Agent)

        # You can add game logic here
        # For example: award points, open doors, spawn items, etc.`,
  },
  {
    id: 'uefn-countdown-timer',
    name: 'Countdown Timer',
    description: 'Create a countdown timer with events',
    category: 'gameplay',
    platform: 'uefn',
    code: `using { /Fortnite.com/Devices }
using { /Verse.org/Simulation }
using { /UnrealEngine.com/Temporary/Diagnostics }
using { /UnrealEngine.com/Temporary/SpatialMath }

countdown_timer_device := class(creative_device):

    @editable
    CountdownSeconds : int = 60

    @editable
    EndGameDevice : end_game_device = end_game_device{}

    OnBegin<override>()<suspends>:void=
        Print("Timer starting: {CountdownSeconds} seconds")
        StartCountdown()

    StartCountdown()<suspends>:void=
        for (Index := CountdownSeconds..0):
            TimeRemaining := CountdownSeconds - Index
            Print("Time remaining: {TimeRemaining} seconds")

            # Wait 1 second
            Sleep(1.0)

        Print("Time's up!")
        OnTimerComplete()

    OnTimerComplete():void=
        Print("Timer completed - ending game")
        EndGameDevice.Activate()`,
  },
  {
    id: 'uefn-score-tracker',
    name: 'Score Tracker',
    description: 'Track and display player scores',
    category: 'gameplay',
    platform: 'uefn',
    code: `using { /Fortnite.com/Devices }
using { /Fortnite.com/Game }
using { /Verse.org/Simulation }
using { /UnrealEngine.com/Temporary/Diagnostics }

score_tracker_device := class(creative_device):

    @editable
    ScoreManager : score_manager_device = score_manager_device{}

    @editable
    PointsPerAction : int = 10

    OnBegin<override>()<suspends>:void=
        Print("Score tracker initialized")

        # Example: Award points when something happens
        # You would typically subscribe to game events here

    AwardPoints(Player:player):void=
        Print("Awarding {PointsPerAction} points to player")

        # Award score through the score manager
        ScoreManager.Activate(Player)

        # You can also track scores manually using a map
        # PlayerScores[Player] = CurrentScore + PointsPerAction

    GetPlayerScore(Player:player):int=
        # Implement score retrieval logic
        # This is a placeholder
        return 0`,
  },
  {
    id: 'uefn-trigger-zone',
    name: 'Trigger Zone',
    description: 'Detect when players enter/exit a trigger area',
    category: 'gameplay',
    platform: 'uefn',
    code: `using { /Fortnite.com/Devices }
using { /Fortnite.com/Characters }
using { /Verse.org/Simulation }
using { /UnrealEngine.com/Temporary/Diagnostics }

trigger_zone_device := class(creative_device):

    @editable
    TriggerDevice : trigger_device = trigger_device{}

    @editable
    ItemGranter : item_granter_device = item_granter_device{}

    OnBegin<override>()<suspends>:void=
        # Subscribe to trigger events
        TriggerDevice.TriggeredEvent.Subscribe(OnPlayerEntered)

    OnPlayerEntered(Agent:?agent):void=
        if (PlayerAgent := Agent?):
            Print("Player entered trigger zone!")

            # Grant item to player
            ItemGranter.Activate(PlayerAgent)`,
  },
  {
    id: 'uefn-damage-volume',
    name: 'Damage Volume',
    description: 'Create a damaging area that hurts players',
    category: 'gameplay',
    platform: 'uefn',
    code: `using { /Fortnite.com/Devices }
using { /Fortnite.com/Characters }
using { /Verse.org/Simulation }
using { /UnrealEngine.com/Temporary/Diagnostics }

damage_volume_device := class(creative_device):

    @editable
    TriggerDevice : trigger_device = trigger_device{}

    @editable
    DamageAmount : float = 10.0

    @editable
    DamageInterval : float = 1.0

    OnBegin<override>()<suspends>:void=
        # Subscribe to trigger events
        TriggerDevice.TriggeredEvent.Subscribe(OnPlayerEntered)

    OnPlayerEntered(Agent:?agent):void=
        if (PlayerAgent := Agent?):
            Print("Player entered damage zone!")
            spawn { ApplyDamageOverTime(PlayerAgent) }

    ApplyDamageOverTime(Agent:agent)<suspends>:void=
        # Apply damage repeatedly while player is in zone
        loop:
            if (FortCharacter := Agent.GetFortCharacter[]):
                Print("Applying {DamageAmount} damage")
                # Note: Actual damage application would use damage device
                Sleep(DamageInterval)
            else:
                # Player left or died
                break`,
  },
  {
    id: 'uefn-item-spawner',
    name: 'Item Spawner',
    description: 'Spawn items at regular intervals',
    category: 'tools',
    platform: 'uefn',
    code: `using { /Fortnite.com/Devices }
using { /Verse.org/Simulation }
using { /UnrealEngine.com/Temporary/Diagnostics }

item_spawner_device := class(creative_device):

    @editable
    ItemSpawner : item_spawner_device = item_spawner_device{}

    @editable
    SpawnInterval : float = 30.0

    @editable
    EnableAutoSpawn : logic = true

    OnBegin<override>()<suspends>:void=
        if (EnableAutoSpawn?):
            Print("Auto-spawn enabled, spawning every {SpawnInterval} seconds")
            spawn { AutoSpawnLoop() }
        else:
            Print("Auto-spawn disabled")

    AutoSpawnLoop()<suspends>:void=
        loop:
            Sleep(SpawnInterval)
            SpawnItem()

    SpawnItem():void=
        Print("Spawning item")
        ItemSpawner.Enable()
        ItemSpawner.Spawn()`,
  },
];
