import logging


def render_disassembly(dis, match_offset, match_len, context_lines=4):
    """
    Accepts a DecodeGenerator from distorm and returns a string that will be directly rendered in the ICE yara results page
    dis: DecodeGenerator from distorm.Decode()
    match_offset: offset into the file where the match occured
    match_len: Length of yara  match
    context_lines: How many lines of disassembly to return before and after the matching lines
    """
    lines = []
    first_line = None
    last_line = None
    for i in range(len(dis)):
        instr = dis[i]
        asm = "0x{:08X}     {:<20}{}".format(instr[0], instr[3], instr[2])
        if instr[0] >= match_offset and instr[0] < match_offset + match_len:
            lines.append("<b>{}</b>".format(asm))
            if not first_line:
                first_line = i
        else:
            lines.append(asm)
            if first_line and not last_line:
                last_line = i
    lines = (
        lines[:first_line][-context_lines - 1 :]
        + lines[first_line:last_line]
        + lines[last_line:][:context_lines]
    )
    logging.error("Rendered disassembly: {}".format("\n".join(lines)))
    return "\n".join(lines)