using System;
using System.IO;
using Server;
using Server.Items;
using Server.Commands;

namespace Server.Misc
{
    public class GenTeleporters
    {
        public static void Initialize()
        {
            CommandSystem.Register( "GenTeleporters", AccessLevel.Administrator, new CommandEventHandler( GenTeleporters_OnCommand ) );
        }

        [Usage( "GenTeleporters" )]
        [Description( "Generates teleporters from Data/Config/teleporters.cfg" )]
        public static void GenTeleporters_OnCommand( CommandEventArgs e )
        {
            e.Mobile.SendMessage( "Generating teleporters..." );
            string filePath = Path.Combine( Core.BaseDirectory, "Data/Config/teleporters.cfg" );
            
            int count = 0;

            if ( File.Exists( filePath ) )
            {
                using ( StreamReader ip = new StreamReader( filePath ) )
                {
                    string line;
                    while ( (line = ip.ReadLine()) != null )
                    {
                        line = line.Trim();
                        
                        // Skip empty lines and comments
                        if ( line.Length == 0 || line.StartsWith( "#" ) )
                            continue;

                        string[] split = line.Split( ' ' );
                        
                        // We need at least 8 parameters: Map1 X1 Y1 Z1 Map2 X2 Y2 Z2
                        if ( split.Length >= 8 )
                        {
                            try
                            {
                                Map map1 = Map.Parse( split[0] );
                                int x1 = Convert.ToInt32( split[1] );
                                int y1 = Convert.ToInt32( split[2] );
                                int z1 = Convert.ToInt32( split[3] );

                                Map map2 = Map.Parse( split[4] );
                                int x2 = Convert.ToInt32( split[5] );
                                int y2 = Convert.ToInt32( split[6] );
                                int z2 = Convert.ToInt32( split[7] );

                                // Default to two-way unless false is explicitly stated
                                bool twoWay = true; 
                                if ( split.Length >= 9 && split[8].ToLower() == "false" )
                                {
                                    twoWay = false;
                                }

                                Point3D loc1 = new Point3D( x1, y1, z1 );
                                Point3D loc2 = new Point3D( x2, y2, z2 );

                                // Create Teleporter A (Location 1 -> Location 2)
                                Teleporter tele1 = new Teleporter( loc2, map2 );
                                tele1.MoveToWorld( loc1, map1 );
                                count++;

                                // Conditionally Create Teleporter B (Location 2 -> Location 1)
                                if ( twoWay )
                                {
                                    Teleporter tele2 = new Teleporter( loc1, map1 );
                                    tele2.MoveToWorld( loc2, map2 );
                                    count++;
                                }
                            }
                            catch
                            {
                                Console.WriteLine( "Warning: Error parsing teleporter line: {0}", line );
                            }
                        }
                    }
                }
                e.Mobile.SendMessage( "{0} teleporters generated successfully.", count );
            }
            else
            {
                e.Mobile.SendMessage( "Error: Could not find Data/Config/teleporters.cfg" );
            }
        }
    }
}
