import re
import warnings


def parse_psp_name(psp_name):
    """
    Parse the name of vasp's psp

    Parameter
        psp_name: str
            The name of vasp's psp, e.g. GGA, LDA, potpaw_LDA
    Return
        psp_name_norm: str
            The normalized psp name
    """
    psp_name = psp_name.upper()
    psp_name_list = re.split(r'\.|\_|\-|\=|\+|\*|\s',psp_name)
    flag_us = False
    for psp_name_i in psp_name_list:
        if "US" in psp_name_i:
            flag_us = True
            break
    if "LDA" in psp_name_list:
        if "52" in psp_name_list:
            psp_name_norm = "POT_LDA_PAW_52"
        elif "54" in psp_name_list:
            psp_name_norm = "POT_LDA_PAW_54"
        elif flag_us:
            psp_name_norm = "POT_LDA_US"
        else:
            psp_name_norm = "POT_LDA_PAW"
    elif "PBE" in psp_name_list:
        if "52" in psp_name_list:
            psp_name_norm = "POT_GGA_PAW_PBE_52"
        elif "54" in psp_name_list:
            psp_name_norm = "POT_GGA_PAW_PBE_54"
        else:
            psp_name_norm = "POT_GGA_PAW_PBE"
    elif "GGA" in psp_name_list:
        if flag_us:
            psp_name_norm = "POT_GGA_US_PW91"
        else:
            psp_name_norm = "POT_GGA_PAW_PW91"
    else:
        warnings.warn("{} is not a proper name of vasp's pseudopotential, please ref \
            https://github.com/PhasesResearchLab/dfttk/blob/master/docs/Configuration.md. \
            This folder will be ignored.".format(psp_name))
        psp_name_norm = None
    return psp_name_norm