import polars as pl
from typing import List

def parseASC(file_path: str, target_ids: List[int]) -> pl.DataFrame:
    """
    Parses an ASC file and extracts messages with specified IDs.

    Args:
        file_path: Path to the ASC file.
        target_ids: List of message IDs to extract.
    
    Returns:
        pl.DataFrame: A DataFrame containing the extracted messages.
    """
    messages = []
    
    # Read the ASC file line by line and extract relevant messages
    with open(file_path, 'r') as file:
        for line in file:
            parts = line.strip().split()
            if len(parts) < 8:
                continue
            # Cleans and parses each line, adding the default data to a list
            timestamp = float(parts[0])
            can_id = parts[4].rstrip('x')
        
            message_id = int(can_id, 16)
            if message_id not in target_ids:
                continue
            dlc = int(parts[8])
            data_bytes = [int(b, 16) for b in parts[9:9+dlc]]
            messages.append((message_id, timestamp, dlc, data_bytes))
    # Convert the list of messages to a Polars DataFrame
    return pl.DataFrame(messages, schema=["message_id", "timestamp", "dlc", "data_bytes"], orient="row")

def read_asc(file_path: str) -> pl.DataFrame:
    messages = []
    lineNum = 0
    with open(file_path, 'r') as file:
        for line in file:
            parts = line.strip().split()
            lineNum += 1
            if len(parts) < 8:
                continue
            timestamp = float(parts[0])
            can_id = parts[2].rstrip('x')
            messageID = int(can_id, 16)
            dlc = int(parts[5])
            data_bytes = [int(b, 16) for b in parts[6:6+dlc]]
            messages.append((messageID, timestamp, dlc, data_bytes))

    df = pl.DataFrame(messages, schema=['messageID', 'timestamp', 'dlc', 'data_bytes'], orient='row')
    return df