using X86Disassembler.X86.Operands;
namespace X86Disassembler.X86.Handlers.And;
/// 
/// Handler for AND r/m16, imm8 instruction (0x83 /4 with 0x66 prefix)
/// 
public class AndImmToRm16SignExtendedHandler : InstructionHandler
{
    /// 
    /// Initializes a new instance of the AndImmToRm16SignExtendedHandler class
    /// 
    /// The instruction decoder that owns this handler
    public AndImmToRm16SignExtendedHandler(InstructionDecoder decoder) 
        : base(decoder)
    {
    }
    
    /// 
    /// Checks if this handler can decode the given opcode
    /// 
    /// The opcode to check
    /// True if this handler can decode the opcode
    public override bool CanHandle(byte opcode)
    {
        // AND r/m16, imm8 is encoded as 0x83 with 0x66 prefix
        if (opcode != 0x83)
        {
            return false;
        }
        // Only handle when the operand size prefix is present
        if (!Decoder.HasOperandSizePrefix())
        {
            return false;
        }
        // Check if we can read the ModR/M byte
        if (!Decoder.CanReadByte())
        {
            return false;
        }
        // Check if the reg field of the ModR/M byte is 4 (AND)
        var reg = ModRMDecoder.PeakModRMReg();
        return reg == 4; // 4 = AND
    }
    
    /// 
    /// Decodes an AND r/m16, imm8 instruction
    /// 
    /// The opcode of the instruction
    /// The instruction object to populate
    /// True if the instruction was successfully decoded
    public override bool Decode(byte opcode, Instruction instruction)
    {
        // Set the instruction type
        instruction.Type = InstructionType.And;
        // Read the ModR/M byte to get the destination operand
        var (_, reg, _, destinationOperand) = ModRMDecoder.ReadModRM16();
        
        // Check if we have enough bytes for the immediate value
        if (!Decoder.CanReadByte())
        {
            return false;
        }
        // Read the immediate value and sign-extend it to 16 bits
        sbyte imm8 = (sbyte)Decoder.ReadByte();
        short signExtendedImm = imm8;
        ushort imm16 = (ushort)signExtendedImm;
        // Create the immediate operand
        var sourceOperand = OperandFactory.CreateImmediateOperand(imm16);
        // Set the structured operands
        instruction.StructuredOperands = 
        [
            destinationOperand,
            sourceOperand
        ];
        return true;
    }
}