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


def get_user_playlists():
    try:
        user, client = Authorization()
        """
        Step 0: Get Users Playlists
        """
        # Get Playlists Data
        user_playlists = user.get_user_playlists()
        if len(user_playlists) == 0:
            return "NO PLAYLISTS"
        user_playlist_data = {} # Get Playlist Names and IDs
        for playlist in user_playlists:
            user_playlist_data[playlist['name']] = playlist['id']

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


def generate_playlist_from_another(playlist_names, user_playlist_data, loading_bar):
    try:
        """
        Step 1: Get Desired Playlist
        """
        user, client = Authorization()
        user_songs = []
        for i in range(10):
            time.sleep(0.01)
            loading_bar.setValue(i)
            
        for playlist in playlist_names:
            playlist_id = user_playlist_data[playlist]
            user_songs.extend(user.get_user_playlist_tracks(playlist_id))

        for i in range(10, 25):
            time.sleep(0.01)
            loading_bar.setValue(i)
            
        if len(user_songs) == 0:
            return "ERROR NO SONGS"

        if len(Person.currentmood) == 1:
            first_emotion = Person.currentmood[0]
            second_emotion = None
        else:
            first_emotion = Person.currentmood[0]
            second_emotion = Person.currentmood[1]

        for i in range(25, 30):
            time.sleep(0.01)
            loading_bar.setValue(i)
            
        emotion_tracks = user.get_user_emotion_tracks(client=client, user_tracks=user_songs, base_emotion=first_emotion,
                                                    second_emotion=second_emotion)

        for i in range(30, 60):
            time.sleep(0.01)
            loading_bar.setValue(i)
            
        if len(emotion_tracks) == 0:
            return "NO SONGS"
        """
        Step 2: Create a Playlist for the User
        """
        if len(emotion_tracks) > 30:  # Show random 30 (average for Spotify playlist creators)
            emotion_tracks = sample(emotion_tracks, 30)

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

        for i in range(60, 70):
            time.sleep(0.01)
            loading_bar.setValue(i)
            
        Person.playlistName = Person.setPlaylistName(Person, Person.moodLabel, Person.currentmood)
        playlist_name = Person.playlistName
        description = "Enjoy your \"" + Person.moodLabel + "\" Playlist 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(70, 80):
            time.sleep(0.01)
            loading_bar.setValue(i)
            
        """
        Step 3: Add Songs to a Playlist
        """
        user.add_to_playlist(playlist_id=playlist_id, playlist_tracks=emotion_tracks)

        for i in range(80, 100):
            time.sleep(0.01)
            loading_bar.setValue(i)
            
        return tracks
    except(spotipy.exceptions.SpotifyException, requests.exceptions.HTTPError, spotipy.oauth2.SpotifyOauthError):
        return None
