using Dalamud.Game.ClientState.Objects.SubKinds;
using FFXIVClientStructs.FFXIV.Client.Game.Character;

namespace HellionChat.GameFunctions.Types;

[Serializable]
public class TellTarget
{
    public string Name { get; set; }
    public uint World { get; set; }
    public ulong ContentId { get; private set; }
    public TellReason Reason { get; private set; }

    public TellTarget(string name, uint world, ulong contentId, TellReason reason)
    {
        Name = name;
        World = world;
        ContentId = contentId;
        Reason = reason;
    }

    public bool IsSet() => !string.IsNullOrEmpty(Name) && World > 0;

    public string ToWorldString() =>
        Sheets.WorldSheet.TryGetRow(World, out var worldRow)
            ? worldRow.Name.ToString()
            : string.Empty;

    public string ToTargetString() => $"{Name}@{ToWorldString()}";

    public unsafe void FromTarget(IPlayerCharacter target)
    {
        if (target.Address == nint.Zero)
            return;

        Name = target.Name.TextValue;
        World = target.HomeWorld.RowId;
        ContentId = ((Character*)target.Address)->ContentId;
    }

    public static TellTarget Empty() => new(string.Empty, 0, 0, TellReason.Direct);

    // ---------------------------------------------------------------
    // Cherry-picked from ChatTwo upstream f35b7d3 (Infiziert90, 2026-05-12)
    // - Replaced static From(t) with an instance-style Clone() so call
    //   sites read like a copy operation, not a factory.
    // TEST-MIRROR: ../../../Hellion Build test/_Helpers/TellTargetCloneTests.cs
    // ---------------------------------------------------------------
    public TellTarget Clone() => new(Name, World, ContentId, Reason);
}
