//
//  NostaleHookManager.cs
//
//  Copyright (c) František Boháček. All rights reserved.
//  Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System.ComponentModel;
using NosSmoothCore;
using Remora.Results;
namespace NosSmooth.LocalClient.Hooks;
/// 
/// The manager for hooking functions.
/// 
public class NostaleHookManager
{
    private readonly NosClient _nosClient;
    /// 
    /// Initializes a new instance of the  class.
    /// 
    /// The nostale client.
    public NostaleHookManager(NosClient nosClient)
    {
        _nosClient = nosClient;
    }
    /// 
    /// Event for the character walk function.
    /// 
    public event Func? ClientWalked;
    /// 
    /// Hook the Character.Walk function.
    /// 
    /// A result that may or may not have succeeded.
    public Result HookCharacterWalk()
    {
        try
        {
            _nosClient.GetCharacter().SetWalkCallback(Walk);
        }
        catch (Exception e)
        {
            return e;
        }
        return Result.FromSuccess();
    }
    /// 
    /// Reset the registered hooks.
    /// 
    /// A result that may or may not have succeeded.
    public Result ResetHooks()
    {
        try
        {
            _nosClient.ResetHooks();
        }
        catch (Exception e)
        {
            return e;
        }
        return Result.FromSuccess();
    }
    private bool Walk(int position)
    {
        return ClientWalked?.Invoke(new WalkEventArgs(position & 0xFFFF, (int)((position & 0xFFFF0000) >> 16))) ?? true;
    }
}