context: using Server.Gumps;
  using Server.Items;
  using Server.Mobiles;
  using Server.Network;
  using Server.Targeting;
  using System;
  using System.Collections.Generic;
  
  namespace Server.Engines.NewMagincia
  {
      public class CommodityBrokerGump : BaseBazaarGump
      {
          private readonly CommodityBroker m_Broker;
  
          public CommodityBrokerGump(CommodityBroker broker, Mobile from)
              : base(520, 520)
          {
              m_Broker = broker;
  
              AddHtmlLocalized(10, 10, 500, 18, 1114513, "#1150636", RedColor16, false, false);  // Commodity Broker
  
              if (m_Broker.Plot.ShopName != null && m_Broker.Plot.ShopName.Length > 0)
              {
                  AddHtml(10, 37, 500, 18, Color(FormatStallName(m_Broker.Plot.ShopName), BlueColor), false, false);
              }
              else
              {
                  AddHtmlLocalized(10, 37, 500, 18, 1114513, "#1150314", BlueColor16, false, false); // This Shop Has No Name
              }
  
              AddHtmlLocalized(10, 55, 260, 18, 1114514, "#1150313", BlueColor16, false, false); // Proprietor:
              AddHtml(280, 55, 260, 18, Color(String.Format("{0}", broker.Name), BlueColor), false, false);
  
              AddHtmlLocalized(10, 100, 500, 18, 1114513, "#1150328", GreenColor16, false, false); // OWNER MENU
  
              AddButton(150, 150, 4005, 4007, 1, GumpButtonType.Reply, 0);
              AddHtmlLocalized(190, 150, 200, 18, 1150392, OrangeColor16, false, false); // INFORMATION
  
              AddHtmlLocalized(38, 180, 200, 18, 1150199, RedColor16, false, false); // Broker Account Balance
              AddHtml(190, 180, 300, 18, FormatAmt(broker.BankBalance), false, false);
  
              int balance = Banker.GetBalance(from);
              AddHtmlLocalized(68, 200, 200, 18, 1150149, GreenColor16, false, false); // Your Bank Balance:
              AddHtml(190, 200, 200, 18, FormatAmt(balance), false, false);
  
              AddHtmlLocalized(32, 230, 200, 18, 1150329, OrangeColor16, false, false); // Broker Sales Comission
              AddHtmlLocalized(190, 230, 100, 18, 1150330, OrangeColor16, false, false); // 5%
  
              AddHtmlLocalized(109, 250, 200, 18, 1150331, OrangeColor16, false, false); // Weekly Fee:
              AddHtml(190, 250, 100, 18, FormatAmt(broker.GetWeeklyFee()), false, false);
  
              AddHtmlLocalized(113, 280, 200, 18, 1150332, OrangeColor16, false, false); // Shop Name:
              AddBackground(190, 280, 285, 22, 9350);
              AddTextEntry(191, 280, 285, 20, LabelHueBlue, 0, m_Broker.Plot.ShopName == null ? "" : m_Broker.Plot.ShopName);
              AddButton(480, 280, 4014, 4016, 2, GumpButtonType.Reply, 0);
  
              AddHtmlLocalized(85, 305, 150, 18, 1150195, OrangeColor16, false, false); // Withdraw Funds
              AddBackground(190, 305, 285, 22, 9350);
              AddTextEntry(191, 305, 285, 20, LabelHueBlue, 1, "");
              AddButton(480, 305, 4014, 4016, 3, GumpButtonType.Reply, 0);
  
              AddHtmlLocalized(97, 330, 150, 18, 1150196, OrangeColor16, false, false); // Deposit Funds
              AddBackground(190, 330, 285, 22, 9350);
              AddTextEntry(191, 330, 285, 20, LabelHueBlue, 2, "");
              AddButton(480, 330, 4014, 4016, 4, GumpButtonType.Reply, 0);
  
              AddButton(150, 365, 4005, 4007, 5, GumpButtonType.Reply, 0);
              AddHtmlLocalized(190, 365, 200, 18, 1150192, OrangeColor16, false, false); // ADD TO INVENTORY
  
              AddButton(150, 390, 4005, 4007, 6, GumpButtonType.Reply, 0);
              AddHtmlLocalized(190, 390, 300, 18, 1150193, OrangeColor16, false, false); // VIEW INVENTORY / REMOVE ITEMS
  
              AddButton(150, 415, 4005, 4007, 7, GumpButtonType.Reply, 0);
              AddHtmlLocalized(190, 415, 300, 18, 1150194, OrangeColor16, false, false); // SET PRICES AND LIMITS
          }
  
          public override void OnResponse(NetState state, RelayInfo info)
          {
              Mobile from = state.Mobile;
  
              if (m_Broker == null || m_Broker.Plot == null)
                  return;
  
              switch (info.ButtonID)
              {
                  default:
                  case 0: return;
                  case 1:
                      from.SendGump(new BazaarInformationGump(0, 1150637, new CommodityBrokerGump(m_Broker, from)));
                      return;
                  case 2: // Set Shop Name
                      TextRelay tr = info.TextEntries[0];
                      string text = tr.Text;
  
                      if (!m_Broker.Plot.TrySetShopName(from, text))
                          from.SendLocalizedMessage(1150775); // Shop names are limited to 40 characters in length. Shop names must pass an obscenity filter check. The text you have entered is not valid.
                      break;
                  case 3: // Withdraw Funds
                      TextRelay tr1 = info.TextEntries[1];
                      string text1 = tr1.Text;
                      int amount = 0;
  
                      try
                      {
                          amount = Convert.ToInt32(text1);
                      }
                      catch { }
  
                      if (amount > 0)
                      {
                          m_Broker.TryWithdrawFunds(from, amount);
                      }
                      break;
                  case 4: // Deposit Funds
                      TextRelay tr2 = info.TextEntries[2];
                      string text2 = tr2.Text;
                      int amount1 = 0;
  
                      try
                      {
                          amount1 = Convert.ToInt32(text2);
                      }
                      catch { }
  
                      if (amount1 > 0)
                      {
                          m_Broker.TryDepositFunds(from, amount1);
                      }
                      break;
                  case 5: // ADD TO INVENTORY
                      if (m_Broker.BankBalance < 0)
                      {
                          from.SendGump(new BazaarInformationGump(1150623, 1150615));
                          return;
                      }
  
                      from.Target = new InternalTarget(m_Broker);
                      from.SendGump(new CommodityTargetGump(m_Broker));
                      return;
                  case 6: // VIEW INVENTORY / REMOVE ITEMS
                      if (m_Broker.BankBalance < 0)
                      {
                          from.SendGump(new BazaarInformationGump(1150623, 1150615));
                          return;
                      }
                      from.SendGump(new ViewInventoryGump(m_Broker));
                      return;
                  case 7: // SET PRICES AND LIMITS
                      if (m_Broker.BankBalance < 0)
                      {
                          from.SendGump(new BazaarInformationGump(1150623, 1150615));
                          return;
                      }
                      from.SendGump(new SetPricesAndLimitsGump(m_Broker));
                      return;
              }
  
              from.SendGump(new CommodityBrokerGump(m_Broker, from));
          }
  
          public class InternalTarget : Target
          {
              private readonly CommodityBroker m_Broker;
              private bool m_HasPickedCommodity;
  
              public InternalTarget(CommodityBroker broker) : this(broker, false)
              {
              }
  
              public InternalTarget(CommodityBroker broker, bool haspicked) : base(-1, false, TargetFlags.None)
              {
                  m_Broker = broker;
                  m_HasPickedCommodity = haspicked;
              }
  
              protected override void OnTarget(Mobile from, object targeted)
              {
                  if (from.HasGump(typeof(CommodityTargetGump)))
                      from.CloseGump(typeof(CommodityTargetGump));
  
                  if (targeted is Item && (targeted is ICommodity || targeted is CommodityDeed))
                  {
                      Item item = (Item)targeted;
  
                      if (item.IsChildOf(from.Backpack))
                      {
                          if (item is CommodityDeed && ((CommodityDeed)item).Commodity == null)
                          {
                              from.SendLocalizedMessage(1150222); // That item is not a commodity that a broker can trade.
                          }
                          else if (m_Broker.CommodityEntries.Count < CommodityBroker.MaxEntries)
                          {
                              if (!m_Broker.TryAddBrokerEntry(item, from))    // Adds new entry									// Already has an entry, adds item to inventory for selling
                                  m_Broker.AddInventory(from, item);          // or adds to the stock
  
                              m_HasPickedCommodity = true;
                          }
                          else
                          {
                              from.SendMessage("You can only have a maximum of {0} commodities on your merchant at a time.", CommodityBroker.MaxEntries.ToString());
                          }
                      }
                      else
                      {
                          from.SendLocalizedMessage(1054107); // This item must be in your backpack.
                      }
                  }
                  else
                  {
                      from.SendLocalizedMessage(1150222); // That item is not a commodity that a broker can trade.
                  }
  
                  from.Target = new InternalTarget(m_Broker, m_HasPickedCommodity);
              }
  
              protected override void OnTargetCancel(Mobile from, TargetCancelType cancelType)
              {
                  if (from.HasGump(typeof(CommodityTargetGump)))
                      from.CloseGump(typeof(CommodityTargetGump));
  
                  if (m_HasPickedCommodity)
                      from.SendGump(new SetPricesAndLimitsGump(m_Broker));
                  else
                      from.SendGump(new CommodityBrokerGump(m_Broker, from));
              }
          }
      }
  
      public class CommodityTargetGump : BaseBazaarGump
      {
          private readonly CommodityBroker m_Broker;
  
          public CommodityTargetGump(CommodityBroker broker)
              : base(520, 520)
          {
              m_Broker = broker;
  
              AddHtmlLocalized(10, 10, 500, 18, 1114513, "#1150636", RedColor16, false, false);  // Commodity Broker
  
              if (m_Broker.Plot.ShopName != null && m_Broker.Plot.ShopName.Length > 0)
              {
                  AddHtml(10, 37, 500, 18, Color(FormatStallName(m_Broker.Plot.ShopName), BlueColor), false, false);
              }
              else
              {
                  AddHtmlLocalized(10, 37, 500, 18, 1114513, "#1150314", BlueColor16, false, false); // This Shop Has No Name
              }
  
              AddHtmlLocalized(10, 55, 260, 18, 1114514, "#1150313", BlueColor16, false, false); // Proprietor:
              AddHtml(280, 55, 260, 18, Color(String.Format("{0}", broker.Name), BlueColor), false, false);
  
              /* Target commodity items or filled commodity deeds in your backpack to add them to the 
  			 * broker's inventory. These items will be retrievable, and the broker will not trade them 
  			 * until you establish prices.<br><br>When done, press the [ESC] key to cancel your targeting 
  			 * cursor, or click the MAIN MENU button below.*/
  
              AddHtmlLocalized(10, 100, 500, 400, 1150209, OrangeColor16, false, false);
          }
  
          public override void OnResponse(NetState state, RelayInfo info)
          {
              Mobile from = state.Mobile;
  
              if (info.ButtonID == 1)
              {
                  if (from.Target is CommodityBrokerGump.InternalTarget)
                      Target.Cancel(from);
              }
          }
      }
  
      public class SetPricesAndLimitsGump : BaseBazaarGump
      {
          private readonly CommodityBroker m_Broker;
          private int m_Index;
          private readonly int m_Page;
  
          public SetPricesAndLimitsGump(CommodityBroker broker) : this(broker, -1, 0) { }
  
          public SetPricesAndLimitsGump(CommodityBroker broker, int index, int page)
              : base(520, 520)
          {
              m_Broker = broker;
              m_Index = index;
              m_Page = page;
  
              AddHtmlLocalized(10, 10, 500, 18, 1114513, "#1150636", RedColor16, false, false);  // Commodity Broker
  
              AddButton(150, 40, 4005, 4007, 1, GumpButtonType.Reply, 0);
              AddHtmlLocalized(190, 40, 200, 18, 1150392, OrangeColor16, false, false); // INFORMATION
  
              AddHtmlLocalized(100, 65, 100, 18, 1150140, OrangeColor16, false, false); // ITEM
              AddHtmlLocalized(170, 65, 100, 18, 1150204, OrangeColor16, false, false); // BUY AT
              AddHtmlLocalized(230, 65, 100, 18, 1150645, OrangeColor16, false, false); // BUY LMT
              AddHtmlLocalized(315, 65, 100, 18, 1150205, OrangeColor16, false, false); // SELL AT
              AddHtmlLocalized(380, 65, 100, 18, 1150646, OrangeColor16, false, false); // SELL LMT
              AddHtmlLocalized(475, 65, 100, 18, 1150647, OrangeColor16, false, false); // EDIT
  
              int y = 85;
              int perPage = 10;
  
              if (index > -1)
                  m_Page = index <= 0 ? 0 : index / perPage;
  
              int start = page * perPage;
              int count = 1;
  
              for (int i = start; i < broker.CommodityEntries.Count && /*index <= perPage * (m_Page + 1) &&*/ count <= perPage; i++)
              {
                  int col = i == m_Index ? YellowColor : OrangeColor;
                  int col16 = col == YellowColor ? YellowColor16 : OrangeColor16;
                  CommodityBrokerEntry entry = broker.CommodityEntries[i];
  
                  AddHtmlLocalized(1, y, 130, 18, 1114514, String.Format("#{0}", entry.Label), col16, false, false); // <DIV ALIGN=RIGHT>~1_TOKEN~</DIV>
                  AddHtml(170, y, 45, 18, Color(AlignRight(FormatAmt(entry.BuyPricePer)), col), false, false);
                  AddHtml(230, y, 80, 18, Color(AlignRight(FormatAmt(entry.BuyLimit)), col), false, false);
                  AddHtml(315, y, 45, 18, Color(AlignRight(FormatAmt(entry.SellPricePer)), col), false, false);
                  AddHtml(380, y, 80, 18, Color(AlignRight(FormatAmt(entry.SellLimit)), col), false, false);
  
                  AddButton(475, y, 4014, 4016, 2 + i, GumpButtonType.Reply, 0);
  
                  y += 22;
                  count++;
              }
  
              if (m_Page > 0) // back
  
gt:                 AddButton(162, 321, 4014, 4016, 400, GumpButtonType.Reply, 0);