using Server.Accounting;
using Server.Commands.Generic;
using Server.Commands;
using Server.Items;
using Server.Misc;
using Server.Mobiles;
using Server.Network;
using Server.Regions;
using Server;
using Server.Gumps;
using System.Collections.Generic;
using System.Collections;
using System.IO;
using System;

namespace Server.Scripts.Commands 
{
    public class BuildWorld
    {
        public static void Initialize()
        {
            CommandSystem.Register("BuildWorld", AccessLevel.Counselor, new CommandEventHandler( BuildWorld_OnCommand ));
        }

        [Usage("BuildWorld")]
        [Description("Opens the Vaelen World Builder control panel.")]
        public static void BuildWorld_OnCommand( CommandEventArgs e )
        {
            Mobile m = e.Mobile;
            
            if (m.HasGump(typeof(BuildWorldGump)))
                m.CloseGump(typeof(BuildWorldGump));
            
            m.SendGump(new BuildWorldGump());
        }

        public static void DoWipe( Mobile m )
        {
            m.SendMessage( "Removing ALL Spawners and wild mobs... This may take a moment." );
            Console.WriteLine( "Removing Spawners..." );

            ArrayList targets = new ArrayList();
            foreach ( Item item in World.Items.Values )
            {
                // Widened net: Catches Premium, Standard, and Proxy Base spawners
                if ( item is PremiumSpawner || item is Spawner || item is BaseSpawner )
                {
                    targets.Add( item );
                }
            }
            for ( int i = 0; i < targets.Count; ++i )
            {
                Item item = ( Item )targets[ i ];
                item.Delete();
            }

            Console.WriteLine( "Removing wild mobs and animals..." );
            ArrayList mobsToDelete = new ArrayList();
            
            foreach ( Mobile mob in World.Mobiles.Values )
            {
                if ( mob is BaseCreature )
                {
                    BaseCreature bc = (BaseCreature)mob;
                    if ( !bc.Controlled && !bc.IsStabled )
                    {
                        mobsToDelete.Add( bc );
                    }
                }
            }
            for ( int i = 0; i < mobsToDelete.Count; ++i )
            {
                Mobile mob = ( Mobile )mobsToDelete[ i ];
                mob.Delete();
            }
            
            m.SendMessage( "Wipe Complete: All Spawners and wild mobs deleted!" );
        }

        public static void DoDecorate( Mobile m )
        {
            m.SendMessage( "Generating World Decorations, Signs & Teleporters, Inn Room Doors..." );
            Console.WriteLine( "Decorating the world...");
            
            CommandSystem.Handle(m, String.Format("{0}Decorate", CommandSystem.Prefix));
            CommandSystem.Handle(m, String.Format("{0}SignGen", CommandSystem.Prefix));
            CommandSystem.Handle(m, String.Format("{0}TelGen", CommandSystem.Prefix));
            CommandSystem.Handle(m, String.Format("{0}InnGen", CommandSystem.Prefix));
            
            m.SendMessage( "Decorations, Signs, Inn Room Doors, and Teleporters generated!" );
        }

        public static void DoSpawns( Mobile m )
        {
            m.SendMessage( "Parsing map files and generating spawners..." );

            Console.WriteLine( "Spawning Overworld...");
            Server.SpawnGenerator.Parse( m, "overworld.map" );

            Console.WriteLine( "Spawning Town Animals...");
            Server.SpawnGenerator.Parse( m, "townanimals.map" );

            Console.WriteLine( "Spawning Camps..." );
            Server.SpawnGenerator.Parse( m, "danger.map" );

            Console.WriteLine( "Spawning NPC's..." );
            Server.SpawnGenerator.Parse( m, "npcs.map" );

            Console.WriteLine( "Spawning Dungeons..." );
            Server.SpawnGenerator.Parse( m, "dungeons.map" );
            
            Console.WriteLine( "Spawning Dungeon Chests..." );
            Server.SpawnGenerator.Parse( m, "chests.map" );

            if ( Server.Settings.S_DungeonFloorTraps )
            {
                Console.WriteLine( "Spawining Dungeon Floor Traps..." );
                Server.SpawnGenerator.Parse( m, "traps.map" );
            }

            m.SendMessage( "Map parsing complete and world populated!" );
        }
    }

    // ==============================================================================
    // --- BUILD WORLD GUMP UI ---
    // ==============================================================================

    public class BuildWorldGump : Gump
    {
        public BuildWorldGump() : base( 50, 50 )
        {
            Closable = true;
            Disposable = true;
            Dragable = true;
            Resizable = false;

            AddPage(0);
            
            // Background 3600: Modern semi-transparent black with border
            AddBackground(0, 0, 350, 360, 3600); 

            // Header
            AddHtml(0, 15, 350, 20, "<CENTER><BASEFONT COLOR=#FFFFFF>Vaelen World Builder</BASEFONT></CENTER>", false, false);
            AddImageTiled(25, 40, 300, 2, 96); // Divider line

            AddHtml(25, 55, 300, 40, "<BASEFONT COLOR=#CCCCCC>Select a specific system to generate, or run the Full Rebuild to wipe and recreate everything.</BASEFONT>", false, false);

            // Button 1: Full Rebuild
            AddButton(35, 105, 4005, 4007, 1, GumpButtonType.Reply, 0);
            AddLabel(75, 106, 1152, "Run Full Rebuild (Wipe & Generate)");

            AddImageTiled(35, 140, 280, 1, 96); // Divider line

            // Button 2: Wipe Only
            AddButton(35, 155, 4005, 4007, 2, GumpButtonType.Reply, 0);
            AddLabel(75, 156, 38, "Surgical Sweep (Wipe Spawners/Mobs)");

            // Button 3: Decorate Only
            AddButton(35, 195, 4005, 4007, 3, GumpButtonType.Reply, 0);
            AddLabel(75, 196, 63, "Generate World Decor & Teleporters");

            // Button 4: Map Spawns Only
            AddButton(35, 235, 4005, 4007, 4, GumpButtonType.Reply, 0);
            AddLabel(75, 236, 63, "Parse Map Files & Generate Spawns");

            AddImageTiled(35, 280, 280, 1, 96); // Divider line

            // Close Button
            AddButton(125, 305, 4017, 4019, 0, GumpButtonType.Reply, 0);
            AddLabel(165, 307, 1152, "Close Panel");
        }

        public override void OnResponse(NetState sender, RelayInfo info)
        {
            Mobile m = sender.Mobile;

            if (m == null || m.Deleted || info.ButtonID == 0)
                return;

            switch (info.ButtonID)
            {
                case 1: // Full Rebuild
                    BuildWorld.DoWipe( m );
                    BuildWorld.DoDecorate( m );
                    BuildWorld.DoSpawns( m );
                    Console.WriteLine( "The world has been built." );
                    m.SendMessage( "The world has been completely rebuilt." );
                    break;

                case 2: // Wipe Only
                    BuildWorld.DoWipe( m );
                    m.SendGump(new BuildWorldGump()); 
                    break;

                case 3: // Decorate Only
                    BuildWorld.DoDecorate( m );
                    m.SendGump(new BuildWorldGump()); 
                    break;

                case 4: // Spawns Only
                    BuildWorld.DoWipe( m );
                    BuildWorld.DoSpawns( m );
                    m.SendGump(new BuildWorldGump()); 
                    break;
            }
        }
    }
}
