using System;
using Decal.Adapter;

namespace MosswartMassacre
{
    /// <summary>
    /// Routes EchoFilter.ServerDispatch network messages to the appropriate handlers.
    /// Owns the routing of 0xF7B0 sub-events and 0x02CF to CharacterStats.
    /// </summary>
    internal class GameEventRouter
    {
        private readonly IPluginLogger _logger;

        internal GameEventRouter(IPluginLogger logger)
        {
            _logger = logger;
        }

        internal void OnServerDispatch(object sender, NetworkMessageEventArgs e)
        {
            try
            {
                if (e.Message.Type == Constants.GameEventMessageType)
                {
                    int eventId = (int)e.Message["event"];

                    if (eventId == Constants.AllegianceInfoEvent)
                    {
                        CharacterStats.ProcessAllegianceInfoMessage(e);
                    }
                    else if (eventId == Constants.LoginCharacterEvent)
                    {
                        CharacterStats.ProcessCharacterPropertyData(e);
                    }
                    else if (eventId == Constants.TitlesListEvent)
                    {
                        CharacterStats.ProcessTitlesMessage(e);
                    }
                    else if (eventId == Constants.SetTitleEvent)
                    {
                        CharacterStats.ProcessSetTitleMessage(e);
                    }
                }
                else if (e.Message.Type == Constants.PrivateUpdatePropertyInt64)
                {
                    CharacterStats.ProcessPropertyInt64Update(e);
                }
            }
            catch (Exception ex)
            {
                _logger?.Log($"[CharStats] ServerDispatch error: {ex.Message}");
            }
        }
    }
}
