private void addToCompound_Click(object sender, RoutedEventArgs e)
        {
            item_template t = itemList.SelectedItem as item_template;
            if (t == null)
                return;

            if (t.Quality < 1)
                return;

            if (t.@class != 2 && t.@class != 4)
                return;

            int maxid = (from d in LegacyToolBox.Data.CompoundRecipe select d.ID).Max() + 1;

            compound_recipe recipe = new compound_recipe();
            recipe.ID = maxid;
            recipe.Item1 = t.entry;
            recipe.ItemCount1 = 1;
            recipe.Enabled = 1;

            int level = 0;
            if (t.ItemLevel >= 90 + (t.Quality - 1) * 10)
                level = 9;
            else if (t.ItemLevel >= 80 + (t.Quality - 1) * 10)
                level = 8;
            else if (t.ItemLevel >= 70 + (t.Quality - 1) * 10)
                level = 7;
            else if (t.ItemLevel >= 60 + (t.Quality - 1) * 10)
                level = 6;
            else if (t.ItemLevel >= 50 + (t.Quality - 1) * 10)
                level = 5;
            else if (t.ItemLevel >= 40 + (t.Quality - 1) * 10)
                level = 4;
            else if (t.ItemLevel >= 30 + (t.Quality - 1) * 10)
                level = 3;
            else if (t.ItemLevel >= 20 + (t.Quality - 1) * 10)
                level = 2;
            else if (t.ItemLevel >= 10 + (t.Quality - 1) * 10)
                level = 1;
            else
                level = 0;

            int type = 0;
            if (t.@class == 2)
            {
                if (ItemManager.IsPhysicalItem(t))
                    type = (int)CompoundReagentType.PhysicalWeapon;
                else
                    type = (int)CompoundReagentType.MagicalWeapon;
                if (t.subclass == 19)
                    type = (int)CompoundReagentType.Trinket;
            }
            else if (t.@class == 4)
            {
                switch (t.subclass)
                {
                    case 1:
                        type = (int)CompoundReagentType.ClothEquipment;
                        break;
                    case 2:
                        type = (int)CompoundReagentType.LightEquipment;
                        break;
                    case 3:
                        type = (int)CompoundReagentType.ChainEquipment;
                        break;
                    case 4:
                    case 6:
                        type = (int)CompoundReagentType.PlateEquipment;
                        break;
                }

                switch (t.InventoryType)
                {
                    case 2:
                    case 11:
                    case 12:
                        type = (int)CompoundReagentType.Trinket;
                        break;
                }
            }

            int slot = 0;
            if (t.@class == 2)
            {
                // physical & magical weapon has same slot id
                switch ((WeaponSubClass)t.subclass)
                {
                    case WeaponSubClass.Axe1H:
                        slot = (int)CompoundSlot.Weapon_1H_Axe;
                        break;
                    case WeaponSubClass.Axe2H:
                        slot = (int)CompoundSlot.Weapon_2H_Axe;
                        break;
                    case WeaponSubClass.Bow:
                        slot = (int)CompoundSlot.Weapon_Bow;
                        break;
                    case WeaponSubClass.Crossbow:
                        slot = (int)CompoundSlot.Weapon_Crossbow;
                        break;
                    case WeaponSubClass.Dagger:
                        slot = (int)CompoundSlot.Weapon_Dagger;
                        break;
                    case WeaponSubClass.Fist:
                        slot = (int)CompoundSlot.Weapon_Fist;
                        break;
                    case WeaponSubClass.Gun:
                        slot = (int)CompoundSlot.Weapon_Gun;
                        break;
                    case WeaponSubClass.Mace1H:
                        slot = (int)CompoundSlot.Weapon_1H_Mace;
                        break;
                    case WeaponSubClass.Mace2H:
                        slot = (int)CompoundSlot.Weapon_2H_Mace;
                        break;
                    case WeaponSubClass.Polearm:
                        slot = (int)CompoundSlot.Weapon_Polearm;
                        break;
                    case WeaponSubClass.Staff:
                        slot = (int)CompoundSlot.Weapon_Staff;
                        break;
                    case WeaponSubClass.Sword1H:
                        slot = (int)CompoundSlot.Weapon_1H_Sworld;
                        break;
                    case WeaponSubClass.Sword2H:
                        slot = (int)CompoundSlot.Weapon_2H_Sworld;
                        break;
                    case WeaponSubClass.Thrown:
                        slot = (int)CompoundSlot.Weapon_Thrown;
                        break;
                    case WeaponSubClass.Wand:
                        slot = (int)CompoundSlot.Trinket_Wand;
                        break;
                    default:
                        break;
                }
            }
            else if (t.@class == 4)
            {
                // all equipments have same slot id
                switch ((InventoryType)t.InventoryType)
                {
                    case InventoryType.Head:
                        slot = (int)CompoundSlot.ClothArmor_Head;
                        break;
                    case InventoryType.Shoulder:
                        slot = (int)CompoundSlot.ClothArmor_Shoulder;
                        break;
                    case InventoryType.Chest:
                    case InventoryType.Robe:
                        slot = (int)CompoundSlot.ClothArmor_Chest;
                        break;
                    case InventoryType.Waists:
                        slot = (int)CompoundSlot.ClothArmor_Waist;
                        break;
                    case InventoryType.Wrists:
                        slot = (int)CompoundSlot.ClothArmor_Wrist;
                        break;
                    case InventoryType.Hands:
                        slot = (int)CompoundSlot.ClothArmor_Hand;
                        break;
                    case InventoryType.Shield:
                        slot = (int)CompoundSlot.PlateArmor_Shield;
                        break;
                    case InventoryType.Feet:
                        slot = (int)CompoundSlot.ClothArmor_Foot;
                        break;
                    case InventoryType.Cloak:
                        slot = (int)CompoundSlot.ClothArmor_Back;
                        break;
                    case InventoryType.Legs:
                        slot = (int)CompoundSlot.ClothArmor_Leg;
                        break;
                    case InventoryType.Finger:
                        slot = (int)CompoundSlot.Trinket_Ring;
                        break;
                    case InventoryType.Neck:
                        slot = (int)CompoundSlot.Trinket_Necklace;
                        break;
                    case InventoryType.Trinket:
                        slot = (int)CompoundSlot.Trinket_Amulet;
                        break;
                    default:
                        break;
                }
            }
            else
                slot = -1;

            int reagentID = DataProcessor.SelectReagentByCompoundInfo(type, level, t.Quality - 1, slot);
            if (reagentID != 0)
            {
                recipe.Reagent1 = reagentID;
                recipe.Count1 = 1;
                var reqLevel = (from d in LegacyToolBox.Data.ItemTemplate where d.entry == reagentID select d.RequiredLevel).SingleOrDefault();

                t.RequiredLevel = reqLevel;
                DataProcessor.SaveItemTemplate(t);

                compound_recipe r = (from d in LegacyToolBox.Data.CompoundRecipe where d.Item1 == reagentID select d).FirstOrDefault();

                if (r != null)
                {
                    recipe.ReqSkill = r.ReqSkill;
                    recipe.ReqSkill2 = r.ReqSkill2;
                    recipe.ReqSkillValue = r.ReqSkillValue;
                    recipe.ReqSkillValue2 = r.ReqSkillValue2;
                    recipe.SkillUpGreen = r.SkillUpGreen;
                    recipe.SkillUpGreen2 = r.SkillUpGreen2;
                    recipe.SkillUpYellow = r.SkillUpYellow;
                    recipe.SkillUpYellow2 = r.SkillUpYellow2;
                    recipe.ReqSpellFocus = r.ReqSpellFocus;
                    recipe.ReqTool1 = r.ReqTool1;
                    recipe.ReqTool2 = r.ReqTool2;
                    recipe.ReqTotem1 = r.ReqTotem1;
                    recipe.ReqTotem2 = r.ReqTotem2;
                }
            }

            LegacyToolBox.Data.CompoundRecipe.Add(recipe);
            DataProcessor.SaveCompoundRecipe(recipe);

            LegacyToolBox.Signal("Compound Created.");
        }
 public void AddCompound(compound_recipe recipe)
 {
     LegacyToolBox.Data.CompoundRecipe.Add(recipe);
     DataProcessor.SaveCompoundRecipe(recipe);
 }