context: using Server.Gumps;
  using Server.Mobiles;
  using Server.Network;
  using System;
  
  namespace Server.Items
  {
      public enum RefinementType
      {
          Reinforcing,
          Deflecting
      }
  
      public enum RefinementCraftType
      {
          Blacksmith,
          Tailor,
          Carpenter
      }
  
      public enum ModType
      {
          Defense,
          Protection,
          Hardening,
          Fortification,
          Invulnerability
      }
  
      public enum RefinementSubCraftType
      {
          StuddedLeather,
          StuddedSamurai,
          Hide,
          Bone,
          Ringmail,
          Chainmail,
          Platemail,
          PlatemailSamurai,
          GargishPlatemail,
          Dragon,
          Woodland,
          GargishStone
      }
  
      public class RefinementComponent : Item
      {
          private RefinementType m_RefinementType;
          private RefinementCraftType m_CraftType;
          private RefinementSubCraftType m_SubCraftType;
          private ModType m_ModType;
  
          [CommandProperty(AccessLevel.GameMaster)]
          public RefinementType RefinementType
          {
              get { return m_RefinementType; }
              set
              {
                  m_RefinementType = value;
                  GetItemID();
                  GetHue();
                  InvalidateProperties();
              }
          }
  
          [CommandProperty(AccessLevel.GameMaster)]
          public RefinementCraftType CraftType
          {
              get { return m_CraftType; }
              set
              {
                  RefinementCraftType old = m_CraftType;
  
                  m_CraftType = value;
  
                  if (old != m_CraftType)
                      GetSubCraftType();
  
                  GetItemID();
                  GetHue();
                  InvalidateProperties();
              }
          }
  
          [CommandProperty(AccessLevel.GameMaster)]
          public RefinementSubCraftType SubCraftType { get { return m_SubCraftType; } set { m_SubCraftType = value; } }
  
          [CommandProperty(AccessLevel.GameMaster)]
          public ModType ModType { get { return m_ModType; } set { m_ModType = value; InvalidateProperties(); } }
  
          [Constructable]
          public RefinementComponent()
              : this(RefinementType.Reinforcing, RefinementCraftType.Blacksmith, ModType.Defense)
          {
          }
  
          public RefinementComponent(RefinementType type, RefinementCraftType craftType, ModType modType)
              : base(0)
          {
              m_RefinementType = type;
              m_CraftType = craftType;
              GetSubCraftType();
              m_ModType = modType;
              GetItemID();
              GetHue();
          }
  
          public override void AddNameProperty(ObjectPropertyList list)
          {
              list.Add(1153966, string.Format("#{0}\t#{1}", Labels[(int)m_RefinementType][(int)m_CraftType], GetModLabel())); // ~1_OBJTYPE~ ~2_BONUSLEVEL~
          }
  
          public override void GetProperties(ObjectPropertyList list)
          {
              base.GetProperties(list);
  
              list.Add(1154002, string.Format("#{0}", 1153954 + (int)m_SubCraftType));                // Armor Type: ~1_TYPE~
              list.Add(1154124, string.Format("#{0}", m_RefinementType == RefinementType.Reinforcing ? 1154123 : 1154122));  // Bonus Type: ~1_TYPE~
          }
  
          public override void OnDoubleClick(Mobile from)
          {
              if (!IsChildOf(from.Backpack) || from.Backpack == null)
                  from.SendLocalizedMessage(1054107); // This item must be in your backpack.
              else if (!CheckSkill(from))
                  from.SendLocalizedMessage(1153981, m_CraftType.ToString()); // Only a Grandmaster ~1_PROFESSION~ would know what to do with this. 
              else if (!from.Backpack.ConsumeTotal(GetRawMaterial(), 20))
                  from.SendLocalizedMessage(1153978); // You lack the required quantity of raw materials to craft the Armor Refinement.
              else
              {
                  from.Backpack.DropItem(new RefinementItem(m_RefinementType, m_CraftType, m_SubCraftType, m_ModType));
                  from.PrivateOverheadMessage(MessageType.Regular, 0, 1153979, from.NetState); // *You carefully and skillfully craft an Armor Refinement* TODO Hue???
  
                  Consume();
              }
          }
  
          private void GetHue()
          {
              if (m_RefinementType == RefinementType.Reinforcing)
              {
                  switch (m_CraftType)
                  {
                      case RefinementCraftType.Blacksmith: Hue = 2967; break;
                      case RefinementCraftType.Tailor: Hue = 2587; break;
                      case RefinementCraftType.Carpenter: Hue = 0; break;
                  }
              }
              else
              {
                  switch (m_CraftType)
                  {
                      case RefinementCraftType.Blacksmith: Hue = 2506; break;
                      case RefinementCraftType.Tailor: Hue = 2952; break;
                      case RefinementCraftType.Carpenter: Hue = 2119; break;
                  }
              }
          }
  
          private bool CheckSkill(Mobile from)
          {
              SkillName check = SkillName.Blacksmith;
  
              switch (m_CraftType)
              {
                  case RefinementCraftType.Blacksmith: break;
                  case RefinementCraftType.Tailor: check = SkillName.Tailoring; break;
                  case RefinementCraftType.Carpenter: check = SkillName.Carpentry; break;
              }
  
              return from.Skills[check].Value >= 100;
          }
  
          private Type GetRawMaterial()
          {
              switch (m_CraftType)
              {
                  default:
                  case RefinementCraftType.Blacksmith: return typeof(MalleableAlloy);
                  case RefinementCraftType.Tailor: return typeof(LeatherBraid);
                  case RefinementCraftType.Carpenter: return typeof(SolventFlask);
              }
          }
  
          public void GetSubCraftType()
          {
              switch (m_CraftType)
              {
                  case RefinementCraftType.Blacksmith:
                      m_SubCraftType = (RefinementSubCraftType)Utility.RandomMinMax(4, 9); break;
                  case RefinementCraftType.Tailor:
                      m_SubCraftType = (RefinementSubCraftType)Utility.RandomMinMax(0, 3); break;
                  case RefinementCraftType.Carpenter:
                      m_SubCraftType = (RefinementSubCraftType)Utility.RandomMinMax(10, 11); break;
              }
          }
  
          private readonly int[][] Labels = new int[][]
          {
                          //Scour   Thread     Varnish
                          //Polish  Wash       Gloss
  			new int[] { 1153951, 1153948, 1153952 },
              new int[] { 1153950, 1153949, 1153953 }
          };
  
          private int GetModLabel()
          {
              switch ((int)m_ModType)
              {
                  default:
                  case 0: return 1153941;
                  case 1: return 1153944;
                  case 2: return 1153945;
                  case 3: return 1153946;
                  case 4: return 1153947;
              }
          }
  
          private readonly int[][] ItemIDs = new int[][]
          {
                       // Smith  Tail  Carp
  			new int[] { 19673, 5163, 11617 }, //Reinforcing
  			new int[] { 19672, 5162, 19674 }  //Deflecting
  		};
  
          private void GetItemID()
          {
              ItemID = ItemIDs[(int)m_RefinementType][(int)m_CraftType];
          }
  
          public static bool Roll(Container c, int rolls, double chance)
          {
              for (int i = 0; i < rolls; i++)
              {
                  if (chance >= Utility.RandomDouble())
                  {
                      c.DropItem(GetRandom());
                      return true;
                  }
              }
              return false;
          }
  
          public static RefinementComponent GetRandom()
          {
              return new RefinementComponent((RefinementType)Utility.Random(2), (RefinementCraftType)Utility.Random(3), GetRandomMod());
          }
  
          private static ModType GetRandomMod()
          {
              double ran = Utility.RandomDouble();
  
              if (ran >= .95) return ModType.Invulnerability;
              if (ran >= .80) return ModType.Fortification;
              if (ran >= .70) return ModType.Hardening;
              if (ran >= .50) return ModType.Protection;
  
              return ModType.Defense;
          }
  
          public RefinementComponent(Serial serial)
              : base(serial)
          {
          }
  
          public override void Serialize(GenericWriter writer)
          {
              base.Serialize(writer);
              writer.Write(0);
  
              writer.Write((int)m_RefinementType);
              writer.Write((int)m_CraftType);
              writer.Write((int)m_SubCraftType);
              writer.Write((int)m_ModType);
          }
  
          public override void Deserialize(GenericReader reader)
          {
              base.Deserialize(reader);
              int v = reader.ReadInt();
  
              m_RefinementType = (RefinementType)reader.ReadInt();
              m_CraftType = (RefinementCraftType)reader.ReadInt();
              m_SubCraftType = (RefinementSubCraftType)reader.ReadInt();
              m_ModType = (ModType)reader.ReadInt();
          }
      }
  
      public class RefinementItem : Item
      {
          private RefinementType m_RefinementType;
          private RefinementCraftType m_CraftType;
          private RefinementSubCraftType m_SubCraftType;
          private ModType m_ModType;
          private int m_ModAmount;
          private bool m_CheckBonus;
          private ModEntry m_Entry;
  
          [CommandProperty(AccessLevel.GameMaster)]
          public RefinementType RefinementType
          {
              get { return m_RefinementType; }
              set
              {
                  m_RefinementType = value;
                  GetItemID();
                  GetHue();
                  InvalidateProperties();
              }
          }
  
          [CommandProperty(AccessLevel.GameMaster)]
          public RefinementCraftType CraftType
          {
              get { return m_CraftType; }
              set
              {
                  RefinementCraftType old = m_CraftType;
  
                  m_CraftType = value;
  
                  if (old != m_CraftType)
                      GetSubCraftType();
  
                  GetItemID();
                  GetHue();
                  InvalidateProperties();
              }
          }
  
          [CommandProperty(AccessLevel.GameMaster)]
          public RefinementSubCraftType SubCraftType { get { return m_SubCraftType; } set { m_SubCraftType = value; InvalidateProperties(); } }
  
          [CommandProperty(AccessLevel.GameMaster)]
          public ModType ModType { get { return m_ModType; } set { m_ModType = value; InvalidateProperties(); ApplyModAmount(); } }
  
          [CommandProperty(AccessLevel.GameMaster)]
          public int ModAmount { get { return m_ModAmount; } set { m_ModAmount = value; if (m_ModAmount > 5) m_ModAmount = 5; if (m_ModAmount < 1) m_ModAmount = 1; ; } }
  
          [CommandProperty(AccessLevel.GameMaster)]
          public bool CheckBonus { get { return m_CheckBonus; } set { m_CheckBonus = value; } }
  
          public ModEntry Entry => m_Entry;
  
          [Constructable]
          public RefinementItem()
              : this(RefinementType.Reinforcing, RefinementCraftType.Blacksmith, RefinementSubCraftType.Ringmail, ModType.Defense)
          {
          }
  
          public RefinementItem(RefinementType type, RefinementCraftType craftType, RefinementSubCraftType srtype, ModType modType)
              : base(0)
          {
              m_RefinementType = type;
              m_CraftType = craftType;
              m_SubCraftType = srtype;
              ModType = modType;
              GetItemID();
              GetHue();
              ApplyModAmount();
  
  
gt:             m_Entry = new ModEntry(m_ModAmount);