using System;
using Server.Items;
using Server.Targeting;
using Server.Mobiles;
using System.Collections.Generic;
namespace Server.Engines.Craft
{
    public enum EnhanceResult
    {
        None,
        NotInBackpack,
        BadItem,
        BadResource,
        AlreadyEnhanced,
        Success,
        Failure,
        Broken,
        NoResources,
        NoSkill
    }
    public class Enhance
    {
        private static Dictionary<Type, CraftSystem> _SpecialTable;
        public static void Initialize()
        {
            _SpecialTable = new Dictionary<Type, CraftSystem>();
            _SpecialTable[typeof(ClockworkLeggings)] = DefBlacksmithy.CraftSystem;
            _SpecialTable[typeof(GargishClockworkLeggings)] = DefBlacksmithy.CraftSystem;
        }
        private static bool IsSpecial(Item item, CraftSystem system)
        {
            foreach (KeyValuePair<Type, CraftSystem> kvp in _SpecialTable)
            {
                if (kvp.Key == item.GetType() && kvp.Value == system)
                    return true;
            }
            return false;
        }
        public static EnhanceResult Invoke(Mobile from, CraftSystem craftSystem, BaseTool tool, Item item, CraftResource resource, Type resType, ref object resMessage)
        {
            if (item == null)
                return EnhanceResult.BadItem;
            if (!item.IsChildOf(from.Backpack))
                return EnhanceResult.NotInBackpack;
            if (!(item is BaseArmor) && !(item is BaseWeapon))
                return EnhanceResult.BadItem;
            if (item is IArcaneEquip)
            {
                IArcaneEquip eq = (IArcaneEquip)item;
                if (eq.IsArcane)
                    return EnhanceResult.BadItem;
            }
            if (CraftResources.IsStandard(resource))
                return EnhanceResult.BadResource;
            int num = craftSystem.CanCraft(from, tool, item.GetType());
            if (num > 0)
            {
                resMessage = num;
                return EnhanceResult.None;
            }
            CraftItem craftItem = craftSystem.CraftItems.SearchFor(item.GetType());
            if (IsSpecial(item, craftSystem))
            {
                craftItem = craftSystem.CraftItems.SearchForSubclass(item.GetType());
            }
            
            if (craftItem == null || craftItem.Resources.Count == 0)
            {
                return EnhanceResult.BadItem;
            }
            #region Mondain's Legacy
            if (craftItem.ForceNonExceptional)
                return EnhanceResult.BadItem;
            #endregion
            bool allRequiredSkills = false;
            if (craftItem.GetSuccessChance(from, resType, craftSystem, false, ref allRequiredSkills) <= 0.0)
                return EnhanceResult.NoSkill;
            CraftResourceInfo info = CraftResources.GetInfo(resource);
            if (info == null || info.ResourceTypes.Length == 0)
                return EnhanceResult.BadResource;
            CraftAttributeInfo attributes = info.AttributeInfo;
            if (attributes == null)
                return EnhanceResult.BadResource;
            int resHue = 0, maxAmount = 0;
            if (!craftItem.ConsumeRes(from, resType, craftSystem, ref resHue, ref maxAmount, ConsumeType.None, ref resMessage))
                return EnhanceResult.NoResources;
            if (craftSystem is DefBlacksmithy)
            {
                AncientSmithyHammer hammer = from.FindItemOnLayer(Layer.OneHanded) as AncientSmithyHammer;
                if (hammer != null)
                {
                    hammer.UsesRemaining--;
                    if (hammer.UsesRemaining < 1)
                        hammer.Delete();
                }
            }
            int phys = 0, fire = 0, cold = 0, pois = 0, nrgy = 0;
            int dura = 0, luck = 0, lreq = 0, dinc = 0;
            int baseChance = 0;
            bool physBonus = false;
            bool fireBonus = false;
            bool coldBonus = false;
            bool nrgyBonus = false;
            bool poisBonus = false;
            bool duraBonus = false;
            bool luckBonus = false;
            bool lreqBonus = false;
            bool dincBonus = false;
            if (item is BaseWeapon)
            {
                BaseWeapon weapon = (BaseWeapon)item;
                if (!CraftResources.IsStandard(weapon.Resource))
                    return EnhanceResult.AlreadyEnhanced;
                baseChance = 20;
                dura = weapon.MaxHitPoints;
                luck = weapon.Attributes.Luck;
                lreq = weapon.WeaponAttributes.LowerStatReq;
                dinc = weapon.Attributes.WeaponDamage;
                fireBonus = (attributes.WeaponFireDamage > 0);
                coldBonus = (attributes.WeaponColdDamage > 0);
                nrgyBonus = (attributes.WeaponEnergyDamage > 0);
                poisBonus = (attributes.WeaponPoisonDamage > 0);
                duraBonus = (attributes.WeaponDurability > 0);
                luckBonus = (attributes.WeaponLuck > 0);
                lreqBonus = (attributes.WeaponLowerRequirements > 0);
                dincBonus = (dinc > 0);
            }
            else
            {
                BaseArmor armor = (BaseArmor)item;
                if (!CraftResources.IsStandard(armor.Resource))
                    return EnhanceResult.AlreadyEnhanced;
                baseChance = 20;
                phys = armor.PhysicalResistance;
                fire = armor.FireResistance;
                cold = armor.ColdResistance;
                pois = armor.PoisonResistance;
                nrgy = armor.EnergyResistance;
                dura = armor.MaxHitPoints;
                luck = armor.Attributes.Luck;
                lreq = armor.ArmorAttributes.LowerStatReq;
                physBonus = (attributes.ArmorPhysicalResist > 0);
                fireBonus = (attributes.ArmorFireResist > 0);
                coldBonus = (attributes.ArmorColdResist > 0);
                nrgyBonus = (attributes.ArmorEnergyResist > 0);
                poisBonus = (attributes.ArmorPoisonResist > 0);
                duraBonus = (attributes.ArmorDurability > 0);
                luckBonus = (attributes.ArmorLuck > 0);
                lreqBonus = (attributes.ArmorLowerRequirements > 0);
                dincBonus = false;
            }
            int skill = from.Skills[craftSystem.MainSkill].Fixed / 10;
            if (skill >= 100)
                baseChance -= (skill - 90) / 10;
            EnhanceResult res = EnhanceResult.Success;
            PlayerMobile user = from as PlayerMobile;
            if (physBonus)
                CheckResult(ref res, baseChance + phys);
            if (fireBonus)
                CheckResult(ref res, baseChance + fire);
            if (coldBonus)
                CheckResult(ref res, baseChance + cold);
            if (nrgyBonus)
                CheckResult(ref res, baseChance + nrgy);
            if (poisBonus)
                CheckResult(ref res, baseChance + pois);
            if (duraBonus)
                CheckResult(ref res, baseChance + (dura / 40));
            if (luckBonus)
                CheckResult(ref res, baseChance + 10 + (luck / 2));
            if (lreqBonus)
                CheckResult(ref res, baseChance + (lreq / 4));
            if (dincBonus)
                CheckResult(ref res, baseChance + (dinc / 4));
            if (user.NextEnhanceSuccess)
            {
                user.NextEnhanceSuccess = false;
                user.SendLocalizedMessage(1149969); // The magical aura that surrounded you disipates and you feel that your item enhancement chances have returned to normal.
                res = EnhanceResult.Success;
            }
            switch (res)
            {
                case EnhanceResult.Broken:
                    {
                        if (!craftItem.ConsumeRes(from, resType, craftSystem, ref resHue, ref maxAmount, ConsumeType.Half, ref resMessage))
                            return EnhanceResult.NoResources;
                        item.Delete();
                        break;
                    }
                case EnhanceResult.Success:
                    {
                        if (!craftItem.ConsumeRes(from, resType, craftSystem, ref resHue, ref maxAmount, ConsumeType.All, ref resMessage))
                            return EnhanceResult.NoResources;
                        if (item is BaseWeapon)
                        {
                            BaseWeapon w = (BaseWeapon)item;
                            w.Resource = resource;
                            #region Mondain's Legacy
                            if (resource != CraftResource.Heartwood)
                            {
                                w.Attributes.WeaponDamage += attributes.WeaponDamage;
                                w.Attributes.WeaponSpeed += attributes.WeaponSwingSpeed;
                                w.Attributes.AttackChance += attributes.WeaponHitChance;
                                w.Attributes.RegenHits += attributes.WeaponRegenHits;
                                w.WeaponAttributes.HitLeechHits += attributes.WeaponHitLifeLeech;
                            }
                            else
                            {
                                switch (Utility.Random(6))
                                {
                                    case 0:
                                        w.Attributes.WeaponDamage += attributes.WeaponDamage;
                                        break;
                                    case 1:
                                        w.Attributes.WeaponSpeed += attributes.WeaponSwingSpeed;
                                        break;
                                    case 2:
                                        w.Attributes.AttackChance += attributes.WeaponHitChance;
                                        break;
                                    case 3:
                                        w.Attributes.Luck += attributes.WeaponLuck;
                                        break;
                                    case 4:
                                        w.WeaponAttributes.LowerStatReq += attributes.WeaponLowerRequirements;
                                        break;
                                    case 5:
                                        w.WeaponAttributes.HitLeechHits += attributes.WeaponHitLifeLeech;
                                        break;
                                }
                            }
                            #endregion
                            int hue = w.GetElementalDamageHue();
                            if (hue > 0)
                                w.Hue = hue;
                        }
                        #region Mondain's Legacy
                        else if (item is BaseShield)
                        {
                            BaseShield shield = (BaseShield)item;
                            shield.Resource = resource;
                            switch (resource)
                            {
                                case CraftResource.AshWood:
                                    shield.ArmorAttributes.LowerStatReq += 20;
                                    break;
                                case CraftResource.YewWood:
                                    shield.Attributes.RegenHits += 1;
                                    break;
                                case CraftResource.Heartwood:
                                    switch (Utility.Random(7))
                                    {
                                        case 0:
                                            shield.Attributes.BonusDex += 2;
                                            break;
                                        case 1:
                                            shield.Attributes.BonusStr += 2;
                                            break;
                                        case 2:
                                            shield.Attributes.ReflectPhysical += 5;
                                            break;
                                        case 3:
                                            shield.Attributes.SpellChanneling = 1;
                                            shield.Attributes.CastSpeed = -1;
                                            break;
                                        case 4:
                                            shield.ArmorAttributes.SelfRepair += 2;
                                            break;
                                        case 5:
                                            shield.PhysicalBonus += 5;
                                            break;
                                        case 6:
                                            shield.ColdBonus += 3;
                                            break;
                                    }
                                    break;
                                case CraftResource.Bloodwood:
                                    shield.Attributes.RegenHits += 2;
                                    shield.Attributes.Luck += 40;
                                    break;
                                case CraftResource.Frostwood:
                                    shield.Attributes.SpellChanneling = 1;
                                    shield.Attributes.CastSpeed = -1;
                                    break;
                            }
                        }
                        #endregion
                        else if (item is BaseArmor)	//Sanity
                        {
                            ((BaseArmor)item).Resource = resource;
                            #region Mondain's Legacy
                            BaseArmor armor = (BaseArmor)item;
                            if (resource != CraftResource.Heartwood)
                            {
                                armor.Attributes.WeaponDamage += attributes.ArmorDamage;
                                armor.Attributes.AttackChance += attributes.ArmorHitChance;
                                armor.Attributes.RegenHits += attributes.ArmorRegenHits;
                                //armor.ArmorAttributes.MageArmor += attributes.ArmorMage;
                            }
                            else
                            {
                                switch (Utility.Random(5))
                                {
                                    case 0:
                                        armor.Attributes.WeaponDamage += attributes.ArmorDamage;
                                        break;
                                    case 1:
                                        armor.Attributes.AttackChance += attributes.ArmorHitChance;
                                        break;
                                    case 2:
                                        armor.ArmorAttributes.MageArmor += attributes.ArmorMage;
                                        break;
                                    case 3:
                                        armor.Attributes.Luck += attributes.ArmorLuck;
                                        break;
                                    case 4:
                                        armor.ArmorAttributes.LowerStatReq += attributes.ArmorLowerRequirements;
                                        break;
                                }
                            }
                            #endregion
                        }
                        break;
                    }
                case EnhanceResult.Failure:
                    {
                        if (!craftItem.ConsumeRes(from, resType, craftSystem, ref resHue, ref maxAmount, ConsumeType.Half, ref resMessage))
                            return EnhanceResult.NoResources;
                        break;
                    }
            }
            return res;
        }
        public static void CheckResult(ref EnhanceResult res, int chance)
        {
            if (res != EnhanceResult.Success)
                return; // we've already failed..


Answer:             int random = Utility.Random(100);