from Moodipy.SpotifyAuthorization import Authorization
from random import sample
import requests.exceptions
import spotipy
import time

def songPredictions(loading_bar):
    try:
        user, client = Authorization()
        
        for i in range(10):
            time.sleep(0.01)
            loading_bar.setValue(i)
            
        top_songs = user.get_popular_songs()
        new_songs = user.get_newest_songs()
        for i in range(10, 35):
            time.sleep(0.01)
            loading_bar.setValue(i)
            
        prediction_songs = user.compare_audio_features(client=client, popular_songs=top_songs, new_songs=new_songs)

        for i in range(35, 70):
            time.sleep(0.01)
            loading_bar.setValue(i)
            
        if len(prediction_songs) == 0:
            return "NO SONGS"

        if len(prediction_songs) > 30:  # Show random 30 (average for Spotify playlist creators)
            prediction_songs = sample(prediction_songs, 30)


        tracks = {}
        for track in prediction_songs:
            tracks[track['name']] = track['artists'][0]['name']

        for i in range(70, 80):
            time.sleep(0.01)
            loading_bar.setValue(i)
            
        playlist_name = "Next Popular Songs"

        description = "Here are our predictions for the next popular songs! Generated by Moodipy, an app that uses sentiment analysis to create a playlist that matches someone's mood."
        playlist_id = user.create_playlist(playlist_name=playlist_name, description=description)

        for i in range(80, 90):
            time.sleep(0.01)
            loading_bar.setValue(i)
            
        user.add_to_playlist(playlist_id=playlist_id, playlist_tracks=prediction_songs)

        for i in range(90, 100):
            time.sleep(0.01)
            loading_bar.setValue(i)
            
        return tracks

    except(spotipy.exceptions.SpotifyException, requests.exceptions.HTTPError, spotipy.oauth2.SpotifyOauthError):
        return None
