using System;
using Decal.Adapter;
using Decal.Interop.Dat;

namespace Decal.Filters;

public sealed class FileService : FilterBase
{
	private AttributeTable mAttributeTable;

	private VitalTable mVitalTable;

	private VitalFormulaTable mVitalFormulaTable;

	private SkillTable mSkillTable;

	private ComponentTable mComponentTable;

	private SpellTable mSpellTable;

	private LevelTables mLevelTables;

	private GenderTable mGenderTable;

	private HeritageTable mHeritageTable;

	private SpeciesTable mSpeciesTable;

	private MaterialTable mMaterialTable;

	public override string ReferenceName => "Decal.FileService";

	public AttributeTable AttributeTable => mAttributeTable;

	public VitalTable VitalTable => mVitalTable;

	public VitalFormulaTable VitalFormulaTable => mVitalFormulaTable;

	public SkillTable SkillTable => mSkillTable;

	public ComponentTable ComponentTable => mComponentTable;

	public SpellTable SpellTable => mSpellTable;

	public LevelTables LevelTables => mLevelTables;

	public GenderTable GenderTable => mGenderTable;

	public HeritageTable HeritageTable => mHeritageTable;

	public SpeciesTable SpeciesTable => mSpeciesTable;

	public MaterialTable MaterialTable => mMaterialTable;

	public event EventHandler<UpdateEventArgs> OnUpdatePortal;

	public event EventHandler<UpdateEventArgs> OnUpdateCell;

	protected override void Startup()
	{
		base.ServerDispatch += OnServerDispatch;
		mAttributeTable = new AttributeTable(GetPortalFile(620756998));
		mVitalTable = new VitalTable(GetPortalFile(620756999));
		mVitalFormulaTable = new VitalFormulaTable(GetPortalFile(234881027));
		mSkillTable = new SkillTable(GetPortalFile(234881028));
		mComponentTable = new ComponentTable(GetPortalFile(234881039));
		mSpellTable = new SpellTable(GetPortalFile(234881038));
		mLevelTables = new LevelTables(GetPortalFile(234881048));
		mGenderTable = new GenderTable(GetPortalFile(570425354));
		mHeritageTable = new HeritageTable(GetPortalFile(570425355));
		mSpeciesTable = new SpeciesTable(GetPortalFile(570425358));
		mMaterialTable = new MaterialTable(GetPortalFile(654311424));
	}

	protected override void Shutdown()
	{
		base.ServerDispatch -= OnServerDispatch;
		mAttributeTable = null;
		mVitalTable = null;
		mVitalFormulaTable = null;
		mSkillTable = null;
		mComponentTable = null;
		mSpellTable = null;
		mLevelTables = null;
		mGenderTable = null;
		mHeritageTable = null;
		mSpeciesTable = null;
		mMaterialTable = null;
	}

	private void OnServerDispatch(object sender, NetworkMessageEventArgs e)
	{
		Message message = e.Message;
		if (message.Type != 63458)
		{
			return;
		}
		int num = message.Value<int>("type");
		if (num == 1 || num == 2)
		{
			int num2 = message.Value<int>("resource");
			byte[] data = message.Value<byte>("compression") switch
			{
				0 => message.RawValue("data"), 
				1 => zlib.Inflate(message.RawValue("data"), message.Value<int>("fileSize")), 
				_ => null, 
			};
			switch (num2)
			{
			case 620756998:
				mAttributeTable.UpdateFrom(data);
				break;
			case 620756999:
				mVitalTable.UpdateFrom(data);
				break;
			case 234881027:
				mVitalFormulaTable.UpdateFrom(data);
				break;
			case 234881028:
				mSkillTable.UpdateFrom(data);
				break;
			case 234881039:
				mComponentTable.UpdateFrom(data);
				break;
			case 234881038:
				mSpellTable.UpdateFrom(data);
				break;
			case 234881048:
				mLevelTables.UpdateFrom(data);
				break;
			case 570425354:
				mGenderTable.UpdateFrom(data);
				break;
			case 570425355:
				mHeritageTable.UpdateFrom(data);
				break;
			case 570425358:
				mSpeciesTable.UpdateFrom(data);
				break;
			case 654311424:
				mMaterialTable.UpdateFrom(data);
				break;
			}
			switch (num)
			{
			case 1:
				this.OnUpdatePortal(this, new UpdateEventArgs(num2));
				break;
			case 2:
				this.OnUpdateCell(this, new UpdateEventArgs(num2));
				break;
			}
		}
	}

	public byte[] GetPortalFile(int fileId)
	{
		return GetFile("services\\DecalDat.DatService\\portal\\" + fileId.ToString("X8"));
	}

	public byte[] GetCellFile(int fileId)
	{
		return GetFile("services\\DecalDat.DatService\\cell\\" + fileId.ToString("X8"));
	}

	private byte[] GetFile(string decalObject)
	{
		IDatStream datStream = (IDatStream)base.Host.Decal.GetObject(decalObject, "{B27D3F72-2640-432F-BAE4-175E1AA0CA39}");
		if (datStream == null)
		{
			return null;
		}
		int size = datStream.Size;
		byte[] array = (byte[])Array.CreateInstance(typeof(byte), size);
		datStream.Restart();
		datStream.ReadBinary(size, ref array[0]);
		return array;
	}
}
