using System;
using Server;
using Server.Gumps;
using Server.Network;
using Server.Items;

namespace Server.Engines.Craft
{
    public class CraftQueueGump : Gump
    {
        private Mobile m_From;
        private CraftSystem m_CraftSystem;
        private BaseTool m_Tool;

        private const int LabelHue = 0x480;
        private const int LabelColor = 0x7FFF;

        public CraftQueueGump( Mobile from, CraftSystem craftSystem, BaseTool tool ) : base( 40, 40 )
        {
            m_From = from;
            m_CraftSystem = craftSystem;
            m_Tool = tool;

            from.CloseGump( typeof( CraftQueueGump ) );
            from.CloseGump( typeof( CraftGump ) );

            CraftQueue queue = CraftQueue.GetQueue( from );

            AddPage( 0 );
            AddBackground( 0, 0, 530, 437, 5054 );
            AddImageTiled( 10, 10, 510, 22, 2624 );
            AddAlphaRegion( 10, 10, 510, 417 );

            AddHtml( 10, 12, 510, 20, "<CENTER><BASEFONT COLOR=#FFFFFF>CRAFTING QUEUE MANAGER</BASEFONT></CENTER>", false, false );

            // Back Button
            AddButton( 15, 402, 4014, 4016, 0, GumpButtonType.Reply, 0 );
            AddHtmlLocalized( 50, 405, 150, 18, 1044150, LabelColor, false, false ); // BACK

            // Play / Pause Button
            string playPauseStr = queue.IsPaused ? "START QUEUE" : "PAUSE QUEUE";
            AddButton( 200, 402, 4005, 4007, 1, GumpButtonType.Reply, 0 );
            AddHtml( 235, 405, 150, 18, $"<BASEFONT COLOR=#FFFFFF>{playPauseStr}</BASEFONT>", false, false );

            // Update Quantities Button
            AddButton( 380, 402, 4005, 4007, 2, GumpButtonType.Reply, 0 );
            AddHtml( 415, 405, 150, 18, "<BASEFONT COLOR=#FFFFFF>UPDATE QTY</BASEFONT>", false, false );

            // Main List Background
            AddImageTiled( 10, 40, 510, 350, 2624 );

            // Column Headers
            AddHtml( 50, 45, 150, 18, "<BASEFONT COLOR=#FFFFFF>Item Name</BASEFONT>", false, false );
            AddHtml( 230, 45, 50, 18, "<BASEFONT COLOR=#FFFFFF>Target</BASEFONT>", false, false );
            AddHtml( 310, 45, 200, 18, "<BASEFONT COLOR=#FFFFFF>Made / Fail / Exc</BASEFONT>", false, false );

            // Loop through the queue entries (Displaying up to 10 for clean UI)
            for ( int i = 0; i < queue.Entries.Count && i < 10; i++ )
            {
                CraftQueueEntry entry = queue.Entries[i];
                int y = 70 + (i * 30);

                // Remove Item Button (Little red X)
                AddButton( 15, y, 4020, 4022, 10 + i, GumpButtonType.Reply, 0 ); 

                // Item Name
                if ( entry.Item.NameNumber > 0 )
                    AddHtmlLocalized( 50, y, 170, 18, entry.Item.NameNumber, LabelColor, false, false );
                else
                    AddLabel( 50, y, LabelHue, entry.Item.NameString );

                // Text Entry Box for Amount (Dark background)
                if ( entry.Amount > 0 )
                {
                    AddImageTiled( 230, y, 50, 20, 0xBBC );
                    AddTextEntry( 230, y, 50, 20, LabelHue, i, entry.Amount.ToString() );
                }
                else
                {
                    // If the item finishes, show a green DONE label instead of the text box
                    AddLabel( 230, y, 68, "DONE" ); 
                }

                // Statistics
                AddLabel( 310, y, LabelHue, $"{entry.Made} / {entry.Failed} / {entry.Exceptional}" );
            }

            if ( queue.Entries.Count == 0 )
            {
                AddHtml( 10, 100, 510, 20, "<CENTER><BASEFONT COLOR=#FFFFFF>Your crafting queue is currently empty.</BASEFONT></CENTER>", false, false );
            }
        }

        public override void OnResponse( NetState sender, RelayInfo info )
        {
            Mobile from = sender.Mobile;
            CraftQueue queue = CraftQueue.GetQueue( from );

            // Always read and save any typed text amounts before processing buttons
            for ( int i = 0; i < queue.Entries.Count && i < 10; i++ )
            {
                TextRelay relay = info.GetTextEntry( i );
                if ( relay != null )
                {
                    int amount;
                    if ( int.TryParse( relay.Text, out amount ) && amount >= 0 )
                    {
                        queue.Entries[i].Amount = amount;
                    }
                }
            }

            if ( info.ButtonID == 0 ) // Back
            {
                from.SendGump( new CraftGump( from, m_CraftSystem, m_Tool, null ) );
            }
            else if ( info.ButtonID == 1 ) // Toggle Pause
            {
                queue.IsPaused = !queue.IsPaused;
                from.SendGump( new CraftQueueGump( from, m_CraftSystem, m_Tool ) );
                
                if ( !queue.IsPaused )
                {
                    from.SendMessage( 0x480, "Crafting queue started." );
                    CraftQueue.ProcessQueue( from );
                }
                else
                {
                    from.SendMessage( 0x20, "Crafting queue paused." );
                }
            }
            else if ( info.ButtonID == 2 ) // Update Qty
            {
                from.SendMessage( 0x480, "Queue quantities updated." );
                from.SendGump( new CraftQueueGump( from, m_CraftSystem, m_Tool ) );
            }
            else if ( info.ButtonID >= 10 ) // Remove item from queue
            {
                int index = info.ButtonID - 10;
                if ( index >= 0 && index < queue.Entries.Count )
                {
                    queue.Entries.RemoveAt( index );
                    from.SendMessage( 0x20, "Item removed from queue." );
                }
                from.SendGump( new CraftQueueGump( from, m_CraftSystem, m_Tool ) );
            }
        }
    }
}
