import eyed3
import os
import sys
import shutil
import yaml
from mako.template import Template


# TracksDir class definition
class Album:
    def __init__(self, track_files):
        self.trackFiles = track_files


# script usage function
def usage():
    print('USAGE : labelize.py [inputDirectory] [outputDirectory]')


# script beginning
arguments = len(sys.argv) - 1
if arguments != 2:
    usage()
    sys.exit(2)

input_directory = sys.argv[1]
output_directory = sys.argv[2]


def copy_directory(src, dest):
    try:
        shutil.copytree(src, dest)
    except shutil.Error as e:
        print('Directory not copied. Error: %s' % e)
    except OSError as e:
        print('Directory not copied. Error: %s' % e)


# removes existing build directory if exists
if os.path.isdir(output_directory):
    shutil.rmtree(output_directory)
#
# copies source files in the build directory
copy_directory(input_directory, output_directory + '/audio')

# copies assets in the build directory
copy_directory('assets', output_directory + '/assets')


with open('labelize.yaml') as f:
    data = yaml.load(f, Loader=yaml.FullLoader)
    title = data['band']
    cover_img = data['images']['cover']
    contact_img = data['images']['contact']

shutil.copyfile(input_directory + '/' + cover_img,  output_directory + '/assets/images/cover.png');
shutil.copyfile(input_directory + '/' + contact_img,  output_directory + '/assets/images/contact.png');

track_files = []
for root, dirs, files in os.walk(input_directory):
    for f in sorted(files):
        if f.lower().endswith(('.mp3', '.wav', '.ogg')):
            track_files.append(f)
            print(f)

    # td = TracksDir(trackFiles)
    # f = open(output_directory + "/index.html", "w+")
    # f.write(td.to_html())
    # f.close()
my_template = Template(filename='template.html')
f = open(output_directory + "/index.html", "w+")

# Write template data to the file created
f.write(my_template.render(
    title=title,
    tracks=track_files,
    albums_section=data['albums_section']))

# Close file
f.close()
