using System;
using Server;
using Server.Gumps;
using Server.Mobiles;
using Server.Network;

namespace Server.Custom.InnRooms
{
    public class InnRoomManagementGump : Gump
    {
        private PlayerMobile m_Player;
        private InnDoor m_Door;

        public InnRoomManagementGump(PlayerMobile pm, InnDoor door) : base(150, 150)
        {
            m_Player = pm;
            m_Door = door;

            if (m_Door == null || m_Door.House == null || m_Door.Deleted)
                return;

            AddPage(0);
            AddBackground(0, 0, 400, 350, 9270);
            AddHtml(0, 15, 400, 20, "<CENTER><BASEFONT COLOR=#FFFFFF>Room Management</BASEFONT></CENTER>", false, false);

            // --- STORAGE STATS ---
            // Tapping into the core BaseHouse math to get exact counts!
            int sec = 0, ven = 0, lck = 0, mc = 0;
            int usedStorage = m_Door.House.GetAosCurSecures(out sec, out ven, out lck, out mc);
            int maxStorage = m_Door.House.GetAosMaxSecures();

            AddHtml(40, 60, 150, 20, "<BASEFONT COLOR=#FCCA03>Storage Used:</BASEFONT>", false, false);
            AddHtml(150, 60, 200, 20, $"<BASEFONT COLOR=#FFFFFF>{usedStorage} / {maxStorage} Items</BASEFONT>", false, false);

            // --- RENT INFO ---
            AddHtml(40, 110, 150, 20, "<BASEFONT COLOR=#FCCA03>Rent Expires:</BASEFONT>", false, false);
            AddHtml(150, 110, 200, 20, $"<BASEFONT COLOR=#FFFFFF>{m_Door.RentExpires.ToString("g")}</BASEFONT>", false, false);

            // --- ACTIONS ---
            // 1. Auto-Renew Toggle
            AddHtml(75, 170, 250, 20, m_Door.AutoRenew ? "<BASEFONT COLOR=#00FF00>Auto-Renew is ON</BASEFONT>" : "<BASEFONT COLOR=#777777>Auto-Renew is OFF</BASEFONT>", false, false);
            AddButton(40, 170, 4005, 4007, 1, GumpButtonType.Reply, 0);

            // 2. Manual Renew
            AddHtml(75, 210, 250, 20, $"<BASEFONT COLOR=#FFFFFF>Manual Renew ({m_Door.RentCost} Gold)</BASEFONT>", false, false);
            AddButton(40, 210, 4005, 4007, 2, GumpButtonType.Reply, 0);

            // 3. Cancel Button
            AddHtml(75, 280, 250, 20, "<BASEFONT COLOR=#FF0000>Cancel Rental Contract</BASEFONT>", false, false);
            AddButton(40, 280, 4005, 4007, 3, GumpButtonType.Reply, 0);
        }

        public override void OnResponse(NetState sender, RelayInfo info)
        {
            if (m_Door == null || m_Door.Deleted || m_Door.Renter != m_Player) 
                return;

            switch (info.ButtonID)
            {
                case 1: // Toggle Auto-Renew
                    m_Door.AutoRenew = !m_Door.AutoRenew;
                    m_Player.SendMessage(68, m_Door.AutoRenew ? "Auto-Renew enabled." : "Auto-Renew disabled.");
                    m_Player.SendGump(new InnRoomManagementGump(m_Player, m_Door)); // Refresh Gump
                    break;

                case 2: // Manual Renew
                    if (Banker.Withdraw(m_Player, m_Door.RentCost))
                    {
                        m_Door.RentExpires += m_Door.RentDuration;
                        m_Player.PlaySound(0x249);
                        m_Player.SendMessage(68, $"You paid {m_Door.RentCost} gold. Room paid until: {m_Door.RentExpires.ToString("g")}");
                        m_Door.InvalidateProperties();
                    }
                    else
                    {
                        m_Player.SendMessage(33, $"You need {m_Door.RentCost} gold in your bank to renew.");
                    }
                    m_Player.SendGump(new InnRoomManagementGump(m_Player, m_Door)); // Refresh Gump
                    break;

                case 3: // Cancel Contract
                    m_Player.SendGump(new CancelRentGump(m_Door));
                    break;
            }
        }
    }

    // --- CANCELLATION CONFIRMATION GUMP ---
    public class CancelRentGump : Gump
    {
        private InnDoor m_Door;

        public CancelRentGump(InnDoor door) : base(200, 200)
        {
            m_Door = door;

            AddPage(0);
            AddBackground(0, 0, 300, 150, 9270);
            AddHtml(0, 15, 300, 20, "<CENTER><BASEFONT COLOR=#FFFFFF>Cancel Room Rental?</BASEFONT></CENTER>", false, false);
            AddHtml(20, 50, 260, 40, "<BASEFONT COLOR=#FFFFFF>This will instantly pack your items into your bank and evict you.</BASEFONT>", false, false);
            
            AddButton(40, 100, 4005, 4007, 1, GumpButtonType.Reply, 0);
            AddHtml(75, 100, 100, 20, "<BASEFONT COLOR=#FFFFFF>Confirm</BASEFONT>", false, false);
            
            AddButton(160, 100, 4005, 4007, 0, GumpButtonType.Reply, 0);
            AddHtml(195, 100, 100, 20, "<BASEFONT COLOR=#FFFFFF>Cancel</BASEFONT>", false, false);
        }

        public override void OnResponse(NetState sender, RelayInfo info)
        {
            if (info.ButtonID == 1 && m_Door != null && !m_Door.Deleted)
            {
                m_Door.EvictTenant();
            }
        }
    }
}
