namespace Decal.Filters;

public sealed class SpellTable : IdNameTable<Spell>
{
	internal SpellTable(byte[] Data)
	{
		LoadFrom(Data);
	}

	internal override bool LoadFrom(byte[] Data)
	{
		int Index = 0;
		if (Get.DWord(Data, ref Index) != 234881038)
		{
			return false;
		}
		int num = Get.Word(Data, ref Index);
		InitializeTable(num);
		Index += 2;
		for (int i = 0; i < num; i++)
		{
			Spell spell = new Spell(Get.DWord(Data, ref Index));
			spell.Name = Get.EncString(Data, ref Index);
			spell.Description = Get.EncString(Data, ref Index);
			spell.School = SpellSchool.GetById(Get.DWord(Data, ref Index));
			spell.IconId = Get.DWord(Data, ref Index);
			spell.Family = Get.DWord(Data, ref Index);
			spell.Flags = Get.DWord(Data, ref Index);
			spell.Mana = Get.DWord(Data, ref Index);
			spell.Unknown1 = Get.Float(Data, ref Index);
			spell.Unknown2 = Get.Float(Data, ref Index);
			spell.Difficulty = Get.DWord(Data, ref Index);
			spell.Unknown3 = Get.Float(Data, ref Index);
			spell.Generation = Get.DWord(Data, ref Index);
			spell.Speed = Get.Float(Data, ref Index);
			spell.Type = Get.DWord(Data, ref Index);
			spell.Unknown4 = Get.DWord(Data, ref Index);
			spell.Duration = ((spell.Type == 1 || spell.Type == 7 || spell.Type == 12) ? Get.Double(Data, ref Index) : (-1.0));
			spell.Unknown5 = ((spell.Type == 1 || spell.Type == 12) ? Get.Double(Data, ref Index) : (-1.0));
			spell.ComponentIDs = new SpellComponentIDs(8, Spell.Hash(spell.Name, spell.Description));
			for (int j = 0; j < 8; j++)
			{
				spell.ComponentIDs.Add(Get.DWord(Data, ref Index));
			}
			spell.CasterEffect = Get.DWord(Data, ref Index);
			spell.TargetEffect = Get.DWord(Data, ref Index);
			spell.Unknown6 = Get.DWord(Data, ref Index);
			spell.Unknown7 = Get.DWord(Data, ref Index);
			spell.Unknown8 = Get.DWord(Data, ref Index);
			spell.Unknown9 = Get.DWord(Data, ref Index);
			spell.SortKey = Get.DWord(Data, ref Index);
			spell.TargetMask = Get.DWord(Data, ref Index);
			spell.Unknown10 = Get.DWord(Data, ref Index);
			Add(spell);
		}
		return true;
	}
}
