import streamlit as st
import math
import numpy as np
import psycopg2
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import train_test_split
import pandas as pd
from scipy.spatial import distance
import random

# Initialize connection.
# Uses st.experimental_singleton to only run once.


@st.experimental_singleton
def init_connection():
    return psycopg2.connect(**st.secrets["postgres"])


list_selected_songs = []
list_allsongs = []
emotion = []
q1_song = []
q2_song = []
q3_song = []
q4_song = []

conn = init_connection()
# number of recommendation of the song to be made
num_recommendation = 10

# Perform query.
# Uses st.experimental_memo to only rerun when the query changes or after 10 min.


@st.experimental_memo(ttl=600)
def run_query(query):
    with conn.cursor() as cur:
        cur.execute(query)
        return cur.fetchall()


def count_emotion(list_emotion):
    s1 = 'q1'
    s2 = 'q2'
    s3 = 'q3'
    s4 = 'q4'
    global count_q1
    global count_q2
    global count_q3
    global count_q4
    count_q1 = list_emotion.count(s1)
    count_q2 = list_emotion.count(s2)
    count_q3 = list_emotion.count(s3)
    count_q4 = list_emotion.count(s4)


def recommended_songs_q1_q2_q3_q4(num_recommendation_q1, num_recommendation_q2, num_recommendation_q3, num_recommendation_q4):
    global q1
    global q2
    global q3
    global g4
    q1 = np.array(q1_song[0:int(num_recommendation_q1)])
    q2 = np.array(q2_song[0:int(num_recommendation_q2)])
    q3 = np.array(q3_song[0:int(num_recommendation_q3)])
    q4 = np.array(q4_song[0:int(num_recommendation_q4)])
    return q1, q2, q3, q4


def get_number_quadrant_recommendation():
    total = count_q1+count_q2+count_q3+count_q4
    percentage_q1 = count_q1/total
    percentage_q2 = count_q2/total
    percentage_q3 = count_q3/total
    percentage_q4 = count_q4/total
    global num_recommendation_q1
    global num_recommendation_q2
    global num_recommendation_q3
    global num_recommendation_q4
    num_recommendation_q1 = (percentage_q1*num_recommendation)
    num_recommendation_q2 = (percentage_q2*num_recommendation)
    num_recommendation_q3 = (percentage_q3*num_recommendation)
    num_recommendation_q4 = (percentage_q4*num_recommendation)


def get_distance(e):
    # index 6 consists of euclidian distance between two songs, seleted song and song in the database
    return e[7]


def get_distance1(e):
    # index 0 consists of euclidian distance between two songs in the case of the repeated song
    return e[0]
   


rows = run_query("SELECT  song_title, artist from nepali_songs order by song_title")
st.title('Emotion Based Music Recommendation System')
st.image('music.jpg')

with st.form("my_form"):

    options = st.multiselect('Choose the song that you want to listen', [
                             ':'.join(map(str, x)) for x in rows])
    submitted = st.form_submit_button("Recommend song to me")

    # splitting artist and song title of the song that user have selected
    if submitted:
        artist_song_list = []
        for index, element in enumerate(options):
            artist_list = element.split(':')

            artist_song_list.append(artist_list)

        # storing the information of the audio features of the songs that the user have selected by retrieving through the database

        for x in artist_song_list:
            with conn.cursor() as cur:
                cur.execute(
                    "select * from nepali_songs where song_title=%s and artist=%s", [x[0], x[1]])
                row = cur.fetchall()
                list_selected_songs.append(row)
        print("selected songs")
        happy_songs=[]
        tensed_songs=[]
        sad_songs=[]
        calm_songs=[]
        
       
            
        # collecting the information about the emotion quadrant of the each song that the user have selected and storing in the list emotion
        for i in list_selected_songs:
            print(i[0][12],i[0][13])
            emotion.append(i[0][14])
            #categorizing the songs into 4 different emotions
            if(i[0][14]=='q1'):
                happy_songs.append(i[0][12])
            elif(i[0][14]=='q2'):
                tensed_songs.append(i[0][12])
            elif(i[0][14]=='q3'):
                sad_songs.append(i[0][12])
            else:
                calm_songs.append(i[0][12])
           

        # calculating the total number of emotion quadrant of the song that the user have selected
        count_emotion(emotion)
        total = count_q1+count_q2+count_q3+count_q4
        st.write("You have selected ", count_q1, " happy songs")
        st.write(happy_songs)
        st.write("You have selected ", count_q2, " tensed songs")
        st.write(tensed_songs)
        st.write("You have selected ", count_q3, " sad songs")
        st.write(sad_songs)
        st.write("You have selected ", count_q4, " calm songs")
        st.write(calm_songs)
        # retriving information of all songs in the database and storing in the list list_allsongs

        distance_info_song_all = []
        with conn.cursor() as cur:
            cur.execute("select * from nepali_songs")
            row = cur.fetchall()
            # print(type(row[0]))
            list_allsongs.append(row)

        # calculating the euclidean distance of selected songs with all songs in the database
        for i in list_selected_songs:
            valence_selected_songs = float(i[0][11])
            energy_selected_songs = float(i[0][2])
            list1 = [valence_selected_songs, energy_selected_songs]

            for lists in list_allsongs:
                # print("printing the list of songs\n")
                distance_info = []
                for tuples in lists:
                    valence_allsongs = float(tuples[11])
                    energy_allsongs = float(tuples[2])
                    list2 = [valence_allsongs, energy_allsongs]
                    dist = distance.euclidean(list1, list2)
                    # i[0][0] is the spotifyid of the selected song and tuples[0] is the spotifyid
                    # of the song in the database
                    distance_info.append(
                        [i[0][12], i[0][14], i[0][0], tuples[12], tuples[14], tuples[13], tuples[0], dist])
                distance_info_song_all.append(distance_info)
        # st.write("distance info")
        # st.write(distance_info_song_all)
        nearest_neighbour = []
        # distance_info_song_all consists of the euclidian distance of the selected song with all songs in the database
        # recommend_list consist of the top 10 songs having minimum distance with all the selected song
        for i in distance_info_song_all:
            i.sort(key=get_distance, reverse=False)
            #sorting the 10 nearest neighbour of the each selected song selected song
            for k in range(0,10):
                nearest_neighbour.append(i[k])
        # st.write(nearest_neighbour)

            # calculating the percentange of song associated with q1, q2, q3 and q4 that the user have selected

            # st.write("q1 recommendation number", num_recommendation_q1)
            # st.write("q2 recommendation number", num_recommendation_q2)
            # st.write("q3 recommendation number", num_recommendation_q3)
            # st.write("q4 recommendation numner", num_recommendation_q4)
            # st.write(num_recommendation)

        # selecting the song of the particular quadrant from the nearest neighbour and storing in the list

        for t in nearest_neighbour:
            
            if (t[4] == 'q2' and t[7]!=0.0):
                q2_song.append(t)

            elif (t[4] == 'q1' and t[7]!=0.0):
                q1_song.append(t)
            elif (t[4] == 'q3' and t[7]!=0.0):
                q3_song.append(t)
            elif (t[4] == 'q4' and t[7]!=0.0):
                q4_song.append(t)
            else:
                pass
       
        q1_song.sort(key=get_distance,reverse=False)
        q2_song.sort(key=get_distance,reverse=False)
        q3_song.sort(key=get_distance,reverse=False)
        q4_song.sort(key=get_distance,reverse=False)





        get_number_quadrant_recommendation()
        q1, q2, q3, q4 = recommended_songs_q1_q2_q3_q4(
            num_recommendation_q1, num_recommendation_q2, num_recommendation_q3, num_recommendation_q4)
        # st.write("q1")
        # st.write(q1)
        # st.write("q2")
        # st.write(q2)
        # st.write("q3")
        # st.write(q3)
        # st.write("q4")
        # st.write(q4)

        w = set()
        x = set()
        y = set()
        z = set()

        for i in q1:
            w.add(i[6])
        for j in q2:
            x.add(j[6])
        for k in q3:
            y.add(k[6])
        for l in q4:
            z.add(l[6])

    # Checking whether the set w,x,z which consists of the unique spotifyid of the song in the q1, q2, q3, q4 consists of at least one element
        if len(w) != 0:
            # a set that consists of the spotify id of the repeated song
            spotifyid=set()
            #list consist of one copy of the repeated song with minimum distance
            q1_=[]
           
            for i in w:
                lst1=[]
                solutions=np.argwhere(q1==i)
                #if the spotify id ofthat songs consists in the q1 more than one time
                if(len(solutions)>1):
                    
                    for i in range(0,len(solutions)):
                        # print(q1[solutions[i][0]][7])
                        # since that song is repeated more than once, collecing the info of the euclidian distance of that repeated songs in the lst1
                        lst1.append([q1[solutions[i][0]][7],q1[solutions[i][0]][0],q1[solutions[i][0]][1],q1[solutions[i][0]][3],q1[solutions[i][0]][2],q1[solutions[i][0]][5],q1[solutions[i][0]][4],q1[solutions[i][0]][6]])
                        spotifyid.add(q1[solutions[i][0]][6])
                    lst1.sort(key=get_distance1,reverse=False)
                    q1_.append(lst1[0])
                    #excluding all the repeated songs   
                    for i in spotifyid: 
                        q1 = [item for item in q1 if item[6]!= i]
                    #appending the only one copy of repeated song with minimum distance
                    for i in q1_:
                        q1.append(i)


        if len(x) != 0:
            # a set that consists of the spotify id of the repeated song
            spotifyid=set()
            q2_=[]
           
            for i in x:
                lst2=[]
                solutions=np.argwhere(q2==i)
                #if the spotify id ofthat songs consists in the q1 more than one time
                if(len(solutions)>1):
                    
                    for i in range(0,len(solutions)):
                        # print(q1[solutions[i][0]][7])
                        # since that song is repeated more than once, collecing the info of the euclidian distance of that repeated songs in the lst1
                        lst2.append([q2[solutions[i][0]][7],q2[solutions[i][0]][0],q2[solutions[i][0]][1],q2[solutions[i][0]][3],q2[solutions[i][0]][2],q2[solutions[i][0]][5],q2[solutions[i][0]][4],q2[solutions[i][0]][6]])
                        spotifyid.add(q2[solutions[i][0]][6])
                    lst2.sort(key=get_distance1,reverse=False)
                    q2_.append(lst2[0])   
                    for i in spotifyid: 
                        q2 = [item for item in q1 if item[6]!= i]
                    
                    for i in q2_:
                        q2.append(i)

        if len(y) != 0:
             # a set that consists of the spotify id of the repeated song
            spotifyid=set()
            q3_=[]
           
            for i in y:
                lst3=[]
                solutions=np.argwhere(q3==i)
                #if the spotify id ofthat songs consists in the q1 more than one time
                if(len(solutions)>1):
                    
                    for i in range(0,len(solutions)):
                        # print(q1[solutions[i][0]][7])
                        # since that song is repeated more than once, collecing the info of the euclidian distance of that repeated songs in the lst1
                        lst3.append([q3[solutions[i][0]][7],q3[solutions[i][0]][0],q3[solutions[i][0]][1],q3[solutions[i][0]][3],q3[solutions[i][0]][2],q3[solutions[i][0]][5],q3[solutions[i][0]][4],q3[solutions[i][0]][6]])
                        spotifyid.add(q3[solutions[i][0]][6])
                    lst3.sort(key=get_distance1,reverse=False)
                    q3_.append(lst3[0])   
                    for i in spotifyid: 
                        q3 = [item for item in q3 if item[6]!= i]
                   
                    for i in q3_:
                        q3.append(i)
            

        if len(z) != 0:
             # a set that consists of the spotify id of the repeated song
            spotifyid=set()
            q4_=[]
           
            for i in z:
                lst4=[]
                solutions=np.argwhere(q4==i)
                #if the spotify id ofthat songs consists in the q1 more than one time
                if(len(solutions)>1):
                    
                    for i in range(0,len(solutions)):
                        # print(q1[solutions[i][0]][7])
                        # since that song is repeated more than once, collecing the info of the euclidian distance of that repeated songs in the lst1
                        lst4.append([q4[solutions[i][0]][7],q4[solutions[i][0]][0],q4[solutions[i][0]][1],q4[solutions[i][0]][3],q4[solutions[i][0]][2],q4[solutions[i][0]][5],q4[solutions[i][0]][4],q4[solutions[i][0]][6]])
                        spotifyid.add(q4[solutions[i][0]][6])
                    lst4.sort(key=get_distance1,reverse=False)
                    q4_.append(lst4[0])   
                    for i in spotifyid: 
                        q4 = [item for item in q4 if item[6]!= i]
                    for i in q4_:
                        q4.append(i)
                    
        

        st.markdown(f'<h1 style="font-size:24px;">{"Recommened list of songs"}</h1>', unsafe_allow_html=True)
        print("Recommended list of songs")
        if(q1!=0):
            st.write("Happy Songs\n")
            for i in q1:
                print(i[3]," ",i[6])
                st.write(i[3],":",i[5])
        if(q2!=0):
            st.write("Tensed Songs\n")
            for j in q2:
                print(j[3]," ",j[6])
                st.write(j[3],":",j[5])

        if(q3!=0):
            st.write("Sad Songs\n")
            for k in q3:
                print(k[3]," ",k[6])
                st.write(k[3],":",k[5])
        if(q4!=0):
            st.write("Calm Songs\n")   
            for l in q4:
                print(l[3]," ",l[6])
                st.write(l[3],":",l[5])