from typing import List
import re


def update_colour_lines(lines, mapping, tetra_mapping=None, is_sitet=False) -> List[str]:
    """
    Update the colour mapping for dotvesta for the ATOMT and SITET
    """
    new_lines = []
    for line in lines:
        tokens = line.split()
        # Use re to match sites like Li1, Fe2 etc.
        orig_name = tokens[1]
        match = re.match(r'([A-Za-z]+)\d*', tokens[1])
        # If not matching, just skip the line. The last line of the section will never match
        if not match:
            new_lines.append(line)
            continue

        atom_name = match.group(1)
        if atom_name in mapping:
            radius = tokens[2]
            r, g, b = mapping[atom_name]['rgb']

            # Assign the tetragonal mapping
            if tetra_mapping is not None and atom_name in tetra_mapping:
                tr, tg, tb = tetra_mapping[atom_name]['rgb']
            else:
                tr, tg, tb = r, g, b

            # Alpha for the tetrahedral
            if is_sitet:
                alpha = mapping[atom_name].get('alpha', int(tokens[-2]))
                line = f'{int(tokens[0]):>3d}{orig_name:>12}{radius:>8}{r:>4d}{g:>4d}{b:>4d}{tr:>4d}{tg:>4d}{tb:>4d}{alpha:>4d}  0'
            else:
                alpha = mapping[atom_name].get('alpha', int(tokens[-1]))
                line = f'{int(tokens[0]):>3d}{orig_name:>11}{radius:>8}{r:>4d}{g:>4d}{b:>4d}{tr:>4d}{tg:>4d}{tb:>4d}{alpha:>4d}'
            new_lines.append(line)
        else:
            new_lines.append(line)
    return new_lines