from tklib.tkcrystal.tkvasp import tkVASP


def read_files(base_path, file_types = ['INCAR', 'POSCAR', 'POTCAR', 'KPOINTS', 'OUTCAR', 'EIGENVAL', 'DOSCAR'],
                    EF = None, normalize_E = True, unit = '/cm3', data_for_bandedges = None, 
                    exit_by_error = False, print_level = 1, terminate = None): 
    vasp = tkVASP()

    INCAR_path    = vasp.get_INCAR(base_path)
    POSCAR_path   = vasp.get_POSCAR(base_path)
    POTCAR_path   = vasp.get_POTCAR(base_path)
    CONTCAR_path  = vasp.get_CONTCAR(base_path)
    OUTCAR_path   = vasp.get_OUTCAR(base_path)
    EIGENVAL_path = vasp.get_VASPPath(base_path, 'EIGENVAL')
    EIGENVAL_OPT_path = vasp.get_VASPPath(base_path, 'EIGENVAL_OPT')
    DOSCAR_path   = vasp.get_VASPPath(base_path, 'DOSCAR')
    XML_path      = vasp.get_VASPPath(base_path, 'vasprun.xml')
    if print_level >= 1:
        print("")
        print("CAR dir   : ", base_path)
        print("  INCAR   : ", INCAR_path)
        print("  POSCAR  : ", POSCAR_path)
        print("  POTCAR  : ", POTCAR_path)
        print("  CONTCAR : ", CONTCAR_path)
        print("  OUTCAR  : ", OUTCAR_path)
        print("  EIGENVAL: ", EIGENVAL_path)
        print("  EIGENVAL_OPT: ", EIGENVAL_OPT_path)
        print("  DOSCAR  : ", DOSCAR_path)
        print("  XML     : ", XML_path)

    inf = {}
    if 'INCAR' in file_types:
        if print_level >= 1:
            print("*** Read INCAR from [{}]".format(INCAR_path))
        inf["INCAR"] = vasp.read_incar_inf(INCAR_path, print_level = print_level)
        if exit_by_error and inf["INCAR"] is None:
            terminate("Error in tkvasp.read_files: Can not read [{}]".format(INCAR_path))

    if 'POTCAR' in file_types:
        if print_level >= 1:
            print("*** Read POTCAR from [{}]".format(POTCAR_path))
        inf["POTCAR"] = vasp.read_potcar_inf(POTCAR_path, print_level = print_level)
        if exit_by_error and inf["POTCAR"] is None:
            terminate("Error in tkvasp.read_files: Can not read [{}]".format(POTCAR_path))

    if 'POSCAR' in file_types:
        if print_level >= 1:
            print("*** Read crystal structure from [{}]".format(POSCAR_path))
        inf["POSCAR"] = vasp.read_poscar(POSCAR_path, print_level = print_level)
        if exit_by_error and inf["POSCAR"] is None:
            terminate("Error in tkvasp.read_files: Can not read [{}]".format(POSCAR_path))

    if 'CONTCAR' in file_types:
        if print_level >= 1:
            print("*** Read crystal structure from [{}]".format(CONTCAR_path))
        inf["CONTCAR"] = vasp.read_poscar(CONTCAR_path, print_level = print_level)
        if exit_by_error and inf["POSCAR"] is None:
            terminate("Error in tkvasp.read_files: Can not read [{}]".format(CONTCAR_path))

    if 'OUTCAR' in file_types:
        if print_level >= 1:
            print("*** Read OUTCAR from [{}]".format(OUTCAR_path))
        inf["OUTCAR"] = vasp.read_outcar_inf(OUTCAR_path, print_level = print_level)
        if exit_by_error and inf["OUTCAR"] is None:
            terminate("Error in tkvasp.read_files: Can not read [{}]".format(OUTCAR_path))

    if 'DOSCAR' in file_types:
        if print_level >= 1:
            print("*** Read DOSCAR from [{}]".format(DOSCAR_path))
        inf["DOSCAR"] = vasp.read_doscar(DOSCAR_path, EF = EF, normalize_E = normalize_E, unit = unit, print_level = print_level)
        if exit_by_error and inf["DOSCAR"] is None:
            terminate("Error in tkvasp.read_files: Can not read [{}]".format(DOSCAR_path))

    if 'EIGENVAL' in file_types:
        if print_level >= 1:
            print("*** Read EIGENVAL from [{}]".format(EIGENVAL_path))
        inf["EIGENVAL"] = vasp.read_eigenval(EIGENVAL_path, normalize_E = normalize_E, print_level = print_level)
        if exit_by_error and inf["EIGENVAL"] is None:
            terminate("Error in tkvasp.read_files: Can not read [{}]".format(EIGENVAL_path))

    if 'EIGENVAL_OPT' in file_types:
        if print_level >= 1:
            print("*** Read EIGENVAL_OPT from [{}]".format(EIGENVAL_OPT_path))
        inf["EIGENVAL_OPT"] = vasp.read_eigenval_opt(EIGENVAL_OPT_path, normalize_E = normalize_E, print_level = print_level)
        if exit_by_error and inf["EIGENVAL_OPT"] is None:
            terminate("Error in tkvasp.read_files: Can not read [{}]".format(EIGENVAL_OPT_path))

    return inf


