# discord_tools/scripts/ghost_typer.py
import os
import sys
import time

# Add the parent directory to the Python path
script_dir = os.path.dirname(os.path.abspath(__file__))
project_root = os.path.dirname(os.path.dirname(script_dir))
sys.path.insert(0, project_root)

from discord_tools.utils.api_utils import make_discord_request

def send_typing_indicator(channel_id):
    # Send typing indicator to a channel
    # This makes it look like you're typing
    endpoint = f'/channels/{channel_id}/typing'
    response = make_discord_request('POST', endpoint)
    return response is not None

def ghost_type_continuous(channel_id, duration=None):
    # Keep sending typing indicator for a specified duration or indefinitely
    print(f"Ghost typing in channel: {channel_id}")
    
    if duration:
        print(f"Duration: {duration} seconds")
    else:
        print("Duration: Infinite (press Ctrl+C to stop)")
    
    print("=" * 60)
    
    start_time = time.time()
    count = 0
    
    try:
        while True:
            success = send_typing_indicator(channel_id)
            
            if success:
                count += 1
                elapsed = time.time() - start_time
                print(f"[{count}] Typing indicator sent ({elapsed:.0f}s elapsed)")
            else:
                print(f"[!] Failed to send typing indicator")
            
            # Typing indicator lasts ~10 seconds, send every 8 to be safe
            time.sleep(8)
            
            # Check if duration is up
            if duration and (time.time() - start_time) >= duration:
                break
    
    except KeyboardInterrupt:
        print("\n\nStopped ghost typing")
    
    total_time = time.time() - start_time
    print("\n" + "=" * 60)
    print(f"Total indicators sent: {count}")
    print(f"Total time: {total_time:.0f} seconds")
    print("=" * 60)

def ghost_type_burst(channel_id, count):
    # Send typing indicator a specific number of times
    print(f"Sending {count} typing indicators to channel: {channel_id}")
    print("=" * 60)
    
    success_count = 0
    
    for i in range(count):
        success = send_typing_indicator(channel_id)
        
        if success:
            success_count += 1
            print(f"[{i+1}/{count}] Sent")
        else:
            print(f"[{i+1}/{count}] Failed")
        
        # Small delay between sends
        if i < count - 1:
            time.sleep(2)
    
    print("\n" + "=" * 60)
    print(f"Successfully sent: {success_count}/{count}")
    print("=" * 60)

def main():
    print("Ghost Typer")
    print("=" * 60)
    print("Send typing indicators without actually typing\n")
    
    print("1. Continuous ghost typing")
    print("2. Timed ghost typing")
    print("3. Burst (send X times)")
    print("=" * 60)
    
    choice = input("Choice: ").strip()
    
    channel_id = input("\nChannel ID: ").strip()
    
    if not channel_id.isdigit():
        print("Invalid channel ID")
        return
    
    if choice == '1':
        # Continuous
        ghost_type_continuous(channel_id)
    
    elif choice == '2':
        # Timed
        duration = input("Duration (seconds): ").strip()
        
        if not duration.isdigit():
            print("Invalid duration")
            return
        
        ghost_type_continuous(channel_id, int(duration))
    
    elif choice == '3':
        # Burst
        count = input("How many times: ").strip()
        
        if not count.isdigit():
            print("Invalid count")
            return
        
        ghost_type_burst(channel_id, int(count))
    
    else:
        print("Invalid choice")

if __name__ == "__main__":
    main()
