context: using Server.Gumps;
  using Server.Mobiles;
  using Server.Multis;
  using Server.Network;
  using Server.Regions;
  using Server.Targeting;
  using System;
  using System.Collections;
  using System.Collections.Generic;
  
  namespace Server.Items
  {
      public class HousePlacementTool : Item
      {
          public virtual bool UseCustomHousePlots => Server.Misc.TestCenter.Enabled;
  
          [Constructable]
          public HousePlacementTool()
              : base(0x14F6)
          {
              Weight = 3.0;
              LootType = LootType.Blessed;
          }
  
          public HousePlacementTool(Serial serial)
              : base(serial)
          {
          }
  
          public override int LabelNumber => 1060651; // a house placement tool
  
          public override void OnDoubleClick(Mobile from)
          {
              if (IsChildOf(from.Backpack))
              {
                  if (from.Map == Map.TerMur && !Server.Engines.Points.PointsSystem.QueensLoyalty.IsNoble(from))
                  {
                      from.SendLocalizedMessage(1113713); // You must rise to the rank of noble in the eyes of the Gargoyle Queen before her majesty will allow you to build a house in her lands.
                      return;
                  }
  
                  from.SendGump(new HousePlacementCategoryGump(this, from));
              }
  
              else
                  from.SendLocalizedMessage(1042001); // That must be in your pack for you to use it.
          }
  
          public virtual void OnPlacement(BaseHouse house)
          {
          }
  
          public override void Serialize(GenericWriter writer)
          {
              base.Serialize(writer);
              writer.Write(0); // version
          }
  
          public override void Deserialize(GenericReader reader)
          {
              base.Deserialize(reader);
              int version = reader.ReadInt();
  
              if (Weight == 0.0)
                  Weight = 3.0;
          }
      }
  
      public class HousePlacementCategoryGump : Gump
      {
          private const int LabelColor = 0x7FFF;
          private const int LabelColorDisabled = 0x4210;
  
          private readonly Mobile m_From;
          private readonly HousePlacementTool m_Tool;
  
          public HousePlacementCategoryGump(HousePlacementTool tool, Mobile from)
              : base(50, 50)
          {
              m_From = from;
              m_Tool = tool;
  
              from.CloseGump(typeof(HousePlacementCategoryGump));
              from.CloseGump(typeof(HousePlacementListGump));
              from.CloseGump(typeof(HouseSwapGump));
  
              AddPage(0);
  
              AddBackground(0, 0, 270, 170, 5054);
  
              AddImageTiled(10, 10, 250, 150, 2624);
              AddAlphaRegion(10, 10, 250, 150);
  
              AddHtmlLocalized(10, 10, 250, 20, 1060239, LabelColor, false, false); // <CENTER>HOUSE PLACEMENT TOOL</CENTER>
  
              AddButton(10, 130, 4017, 4019, 0, GumpButtonType.Reply, 0);
              AddHtmlLocalized(45, 130, 150, 20, 3000363, LabelColor, false, false); // Close
  
              AddPage(1);
  
              AddButton(10, 40, 4005, 4007, 1, GumpButtonType.Reply, 0);
              AddHtmlLocalized(45, 40, 200, 20, 1060390, LabelColor, false, false); // Classic Houses
  
              AddButton(10, 60, 4005, 4007, 2, GumpButtonType.Reply, 0);
              AddHtmlLocalized(45, 60, 200, 20, 1060391, LabelColor, false, false); // 2-Story Customizable Houses
  
              AddButton(10, 80, 4005, 4007, 3, GumpButtonType.Reply, 0);
              AddHtmlLocalized(45, 80, 200, 20, 1060392, LabelColor, false, false); // 3-Story Customizable Houses
  
              if (m_Tool.UseCustomHousePlots || from.AccessLevel > AccessLevel.Player)
              {
                  AddButton(10, 100, 4005, 4007, 4, GumpButtonType.Reply, 0);
                  AddHtmlLocalized(45, 100, 200, 20, 1158540, LabelColor, false, false); // Custom House Contest
              }
          }
  
          public override void OnResponse(Server.Network.NetState sender, RelayInfo info)
          {
              if (!m_From.CheckAlive() || m_From.Backpack == null || !m_Tool.IsChildOf(m_From.Backpack))
                  return;
  
              switch (info.ButtonID)
              {
                  case 1: // Classic Houses
                      {
                          m_From.SendGump(new HousePlacementListGump(m_Tool, m_From, HousePlacementEntry.HousesEJ, true));
                          break;
                      }
                  case 2: // 2-Story Customizable Houses
                      {
                          m_From.SendGump(new HousePlacementListGump(m_Tool, m_From, HousePlacementEntry.TwoStoryFoundations));
                          break;
                      }
                  case 3: // 3-Story Customizable Houses
                      {
                          m_From.SendGump(new HousePlacementListGump(m_Tool, m_From, HousePlacementEntry.ThreeStoryFoundations));
                          break;
                      }
                  case 4: // Custom House Contest
                      {
                          if (m_Tool.UseCustomHousePlots || m_From.AccessLevel > AccessLevel.Player)
                          {
                              m_From.SendGump(new HousePlacementListGump(m_Tool, m_From, HousePlacementEntry.CustomHouseContest));
                          }
                          break;
                      }
              }
          }
      }
  
      public class HousePlacementListGump : Gump
      {
          private const int LabelColor = 0x7FFF;
          private const int LabelHue = 0x481;
  
          private readonly Mobile m_From;
          private readonly HousePlacementEntry[] m_Entries;
          private readonly HousePlacementTool m_Tool;
  
          private readonly bool m_Classic;
  
          public HousePlacementListGump(HousePlacementTool tool, Mobile from, HousePlacementEntry[] entries, bool classic = false)
              : base(50, 50)
          {
              m_From = from;
              m_Tool = tool;
              m_Entries = entries;
              m_Classic = classic;
  
              from.CloseGump(typeof(HousePlacementCategoryGump));
              from.CloseGump(typeof(HousePlacementListGump));
              from.CloseGump(typeof(HouseSwapGump));
  
              AddPage(0);
  
              AddBackground(0, 0, 530, 430, 5054);
  
              AddImageTiled(10, 10, 500, 20, 2624);
              AddAlphaRegion(10, 10, 500, 20);
  
              AddHtmlLocalized(10, 10, 500, 20, 1060239, LabelColor, false, false); // <CENTER>HOUSE PLACEMENT TOOL</CENTER>
  
              AddImageTiled(10, 40, 500, 20, 2624);
              AddAlphaRegion(10, 40, 500, 20);
  
              AddHtmlLocalized(50, 40, 225, 20, 1060235, LabelColor, false, false); // House Description
              AddHtmlLocalized(275, 40, 75, 20, 1060236, LabelColor, false, false); // Storage
              AddHtmlLocalized(350, 40, 75, 20, 1060237, LabelColor, false, false); // Lockdowns
              AddHtmlLocalized(425, 40, 75, 20, 1060034, LabelColor, false, false); // Cost
  
              AddImageTiled(10, 70, 500, 280, 2624);
              AddAlphaRegion(10, 70, 500, 280);
  
              AddImageTiled(10, 370, 500, 20, 2624);
              AddAlphaRegion(10, 370, 500, 20);
  
              AddHtmlLocalized(10, 370, 250, 20, 1060645, LabelColor, false, false); // Bank Balance:
              AddLabel(250, 370, LabelHue, Banker.GetBalance(from).ToString("N0", System.Globalization.CultureInfo.GetCultureInfo("en-US")));
  
              AddImageTiled(10, 400, 500, 20, 2624);
              AddAlphaRegion(10, 400, 500, 20);
  
              AddButton(10, 400, 4017, 4019, 0, GumpButtonType.Reply, 0);
              AddHtmlLocalized(50, 400, 100, 20, 3000363, LabelColor, false, false); // Close
  
              int page = 1;
              int index = -1;
  
              for (int i = 0; i < entries.Length; ++i)
              {
                  if (m_Classic)
                  {
                      if (i == 8)
                      {
                          page = 2;
                          index = 0;
                      }
                      else if (i == 20)
                      {
                          page = 3;
                          index = 0;
                      }
                      else if (i == 32)
                      {
                          page = 4;
                          index = 0;
                      }
                      else if (i > 44)
                      {
                          page = 4 + ((i - 20) / 14);
                          index = (i - 20) % 14;
                      }
                      else
                      {
                          index++;
                      }
                  }
                  else
                  {
                      page = 1 + (i / 14);
                      index = i % 14;
                  }
  
                  if (index == 0)
                  {
                      if (page > 1)
                      {
                          AddButton(450, 400, 4005, 4007, 0, GumpButtonType.Page, page);
                          AddHtmlLocalized(400, 400, 100, 20, 3000406, LabelColor, false, false); // Next
                      }
  
                      AddPage(page);
  
                      if (page > 1)
                      {
                          AddButton(200, 400, 4014, 4016, 0, GumpButtonType.Page, page - 1);
                          AddHtmlLocalized(250, 400, 100, 20, 3000405, LabelColor, false, false); // Previous
                      }
                  }
  
                  HousePlacementEntry entry = entries[i];
  
                  int y = 70 + (index * 20);
                  int storage = (int)(entry.Storage * BaseHouse.GlobalBonusStorageScalar);
                  int lockdowns = (int)(entry.Lockdowns * BaseHouse.GlobalBonusStorageScalar);
  
                  AddButton(10, y, 4005, 4007, 1 + i, GumpButtonType.Reply, 0);
                  AddHtmlLocalized(50, y, 225, 20, entry.Description, LabelColor, false, false);
                  AddLabel(275, y, LabelHue, storage.ToString());
                  AddLabel(350, y, LabelHue, lockdowns.ToString());
                  AddLabel(425, y, LabelHue, entry.Cost.ToString("N0", System.Globalization.CultureInfo.GetCultureInfo("en-US")));
              }
          }
  
          public override void OnResponse(NetState sender, RelayInfo info)
          {
              if (!m_From.CheckAlive() || m_From.Backpack == null || !m_Tool.IsChildOf(m_From.Backpack))
                  return;
  
              int index = info.ButtonID - 1;
  
              if (index >= 0 && index < m_Entries.Length)
              {
                  m_From.Target = new NewHousePlacementTarget(m_Tool, m_Entries, m_Entries[index], m_Classic);
              }
              else if (m_Tool != null && m_Tool.GetType() == typeof(HousePlacementTool))
              {
  
gt:                 m_From.SendGump(new HousePlacementCategoryGump(m_Tool, m_From));