import re


def import_file(
    client, filename, file_type=None, dirname=None, new_name=None, new_model_type="asm"
):
    """Import a file as a model.

    Note: This function will not automatically display or activate
    the imported model. If you want that, you should take the file name
    returned by this function and pass it to file:open.
    Users of the old import_pv function should start using this
    function instead.

    Args:
        client (obj):
            creopyson Client.
        filename (str):
            Source file name.
        file_type (str, optional):
            File type.
            Valid values: "IGES", "NEUTRAL", "PV", "STEP".
            Defaults to None. Will analyse filename extension
            *.igs*|*.iges* => IGES
            *.stp*|*.step* => STEP
            *.neu* => NEUTRAL
            *.pvz* => PV
        dirname (str, optional):
            Source directory.
            Defaults is Creo's current working directory.
        new_name (str, optional):
            New model name. Any extension will be stripped off and replaced
            with one based on new_model_type.
            Defaults to None is the name of the file with an extension based
            on new_model_type..
        new_model_type (str, optional):
            New model type.
            Valid values: "asm", "prt"
            Defaults to "asm".

    Returns:
        str: Name of the model imported

    """
    data = {"filename": filename, "new_model_type": new_model_type}

    if file_type is None:
        if re.search(r".*\.(igs|iges).*", filename):
            data["type"] = "IGES"
        elif re.search(r".*\.(stp|step).*", filename):
            data["type"] = "STEP"
        elif re.search(r".*\.(neu).*", filename):
            data["type"] = "NEUTRAL"
        elif re.search(r".*\.(pvz).*", filename):
            data["type"] = "PV"
        else:
            raise TypeError(
                f"`{filename}` extension was not recognized, fill in file_type."
            )
    else:
        data["type"] = file_type

    if dirname is not None:
        data["dirname"] = dirname
    if new_name is not None:
        data["new_name"] = new_name
    return client._creoson_post("interface", "import_file", data, "file")