import substance_sampler as ssa 
import os
import shutil
import time
# it is important to run this once you run the import_images_gen_mats_and_export.py as this does a couple of little clean jobs to make the Trainz Asset correct.

# TODO: change this directory to your own path where all your albedo / diffuse textures live 
parent_dir = 'D:\\Trainz\\substance_processing_area\\staging\\'

# make a child directory to export the files
if not os.path.exists(parent_dir):
    print('Parent directory is invalid: ' + parent_dir + ', aborting...')
    exit()

# export directory
parent_export_dir = parent_dir + 'export\\'

if not os.path.exists(parent_dir):
    print('Export directory is invalid: ' + parent_dir + ', aborting...')
    exit()

# iterate over files
root_dir_found = ''
for subdir, dirs, files in os.walk(parent_export_dir):
    snow_dir_found = ''
    for filename in files:
        texture_path = ''
        file_path = os.path.join(subdir, filename)
        # checking if it is a file
        if os.path.isdir(file_path):
            if '\\snow' in file_path:
                snow_dir_found = file_path
        if os.path.isfile(file_path):
            if '\\snow\\' in file_path:
                print(' *** in snow dir ***')
                snow_dir_found = subdir
                if '_opacity.png' in filename.lower():
                    os.remove(file_path)
                    print(file_path + ': DELETED')
                elif '_specularlevel.png' in filename.lower():
                    os.remove(file_path)
                    print(file_path + ': DELETED')
                elif '_albedo.png' in filename:
                    source_path = os.path.join(subdir, 'albedo_snow.png')
                    os.rename(file_path, source_path)
                    print(file_path + ': RENAMED')
                    print(source_path + ': source_path')
                    dest_path = os.path.join(root_dir_found, 'albedo_snow.png')
                    print(dest_path + ': dest_path')    
                    shutil.copyfile(source_path, dest_path)                    
                    print('copyfile')    
                elif '_normal.png' in filename:
                    source_path = os.path.join(subdir, 'normal_snow.png')
                    os.rename(file_path, source_path)
                    print(file_path + ': RENAMED')    
                    dest_path = os.path.join(root_dir_found, 'normal_snow.png')
                    shutil.copyfile(source_path, dest_path)                    
                elif '_parameters.png' in filename:
                    source_path = os.path.join(subdir, 'parameters_snow.png')
                    os.rename(file_path, source_path)
                    print(file_path + ': RENAMED')                
                    dest_path = os.path.join(root_dir_found, 'parameters_snow.png')
                    shutil.copyfile(source_path, dest_path)
            else:
                print(' *** in root dir ***')
                root_dir_found = subdir
                print('found file: ' + file_path)
                # get filename
                #filename = get_file_name(file_path, False)
                print(filename)
                if '_opacity.png' in filename.lower():
                    os.remove(file_path)
                    print(file_path + ': DELETED')
                elif '_specularlevel.png' in filename.lower():
                    os.remove(file_path)
                    print(file_path + ': DELETED')
                elif '_albedo.png' in filename:
                    file_path_2 = os.path.join(subdir, 'albedo.png')
                    if os.path.isfile(file_path_2):
                        os.remove(file_path_2)
                        print(file_path_2 + ': REMOVED FIRST')    
                    os.rename(file_path, file_path_2)
                    print(file_path + ': RENAMED')    
                elif '_normal.png' in filename:
                    file_path_2 = os.path.join(subdir, 'normal.png')
                    if os.path.isfile(file_path_2):
                        os.remove(file_path_2)
                        print(file_path_2 + ': REMOVED FIRST')    
                    os.rename(file_path, file_path_2)
                    print(file_path + ': RENAMED')    
                elif '_parameters.png' in filename:
                    file_path_2 = os.path.join(subdir, 'parameters.png')
                    if os.path.isfile(file_path_2):
                        os.remove(file_path_2)
                        print(file_path_2 + ': REMOVED FIRST')    
                    os.rename(file_path, file_path_2)
                    print(file_path + ': RENAMED')    
                                
    if not snow_dir_found == '':
        if os.path.isdir(snow_dir_found):
            print('BEFORE ' + snow_dir_found + ': DELETED')
            shutil.rmtree(snow_dir_found)
            print('AFTER ' + snow_dir_found + ': DELETED')

# remove all snow directories
#for subdir, dirs, files in os.walk(parent_export_dir):
#    for filename in files:
#        file_path = os.path.join(subdir, filename)
#        if os.path.isdir(subdir):
#            if '\\snow' in subdir:        
#                print('BEFORE ' + subdir + ': DELETED')
#                shutil.rmtree(subdir)
#                print('AFTER ' + subdir + ': DELETED')