import os
from flask import Flask, render_template, request, redirect, url_for, abort, send_from_directory
from my_auth_module.flask_httpauth import HTTPBasicAuth
from werkzeug.utils import secure_filename
from werkzeug.security import generate_password_hash, check_password_hash
import glob

app = Flask(__name__)
auth = HTTPBasicAuth()
app.config['PLAYLIST_PATH'] = 'songs'

def get_music_files():
    files = []
    for file in glob.glob(f"{app.config['PLAYLIST_PATH']}/*.wav"):
        files.append(file)
    return files

users = {
    "root": generate_password_hash("password")
}

@auth.verify_password
def verify_password(username, password):
    if username in users and check_password_hash(users.get(username), password):
        return username

@app.route('/')
def index():
    all_files = os.listdir(app.config['PLAYLIST_PATH'])
    selected_files = []
    for file in all_files:
        if file.endswith(".wav"):
            selected_files.append(file)
    return render_template('playlist.html', files = selected_files)

@app.route('/', methods=['GET', 'POST'])
@auth.login_required
def upload_files():
    if request.method == "POST":
       if request.files:
          new_file = request.files['file']
          if new_file.filename.endswith(".wav") == False:
              return redirect(url_for('index'))
          new_file.save(os.path.join(app.config['PLAYLIST_PATH'], new_file.filename))
    return redirect(url_for('index'))

@app.route('/<filename>')
def download_files(filename):
   return send_from_directory(app.config['PLAYLIST_PATH'], filename, as_attachment=True)

@app.route('/delete/<filename>')
@auth.login_required
def delete_files(filename):
    try:
        os.remove(os.path.join(app.config['PLAYLIST_PATH'], filename))
        return redirect(url_for('index'))
    except Exception as e:
        return "Error!"

if __name__ == '__main__':
    app.run()