from uuid import uuid4
from fsUtils import setFile
from ioUtils import saveFile, getFile

class chartEntry:
    def __init__(self, key, entryName, entryType, chartInfo):
        self.key = key
        self.entryName = entryName
        self.entryType = entryType
        self.chartInfo = chartInfo
        self.chartEntries = list(chartInfo.keys())

class chartArtist:
    def __init__(self, key, artistName, debug=False):
        self.debug = debug
        self.key   = key
        self.artistName = artistName
        
        self.allMedia     = []
        self.artistSongs  = {}
        self.artistAlbums = {}
        self.artistSong   = {}
        
    def setAllMediaData(self, allMediaData):
        self.allMediaData = allMediaData
        
    def setSongsData(self, songsData):
        for songName,songNameData in songsData.items():
            key = uuid4().hex
            self.artistSongs[key] = chartEntry(key, entryName=songName, entryType="Songs", chartInfo=songNameData)        
        
    def setAlbumsData(self, albumsData):
        for albumName,albumNameData in albumsData.items():
            key = uuid4().hex
            self.artistAlbums[key] = chartEntry(key, entryName=albumName, entryType="Albums", chartInfo=albumNameData)        
        
    def setSongData(self, songData):
        for songName,songNameData in songData.items():
            key = uuid4().hex
            self.artistSong[key] = chartEntry(key, entryName=songName, entryType="Song", chartInfo=songNameData)     
        
        
class chartArtistData:
    def __init__(self, path, chart, debug=False):
        self.debug = debug
        self.chart = chart
        self.path  = path
        if chart is None:
            fullChartData = {}
            artistAlbumData = {}
            for chart in ["MusicVF", "Billboard", "BillboardYE", "RateYourMusic", "RateYourMusicSong", "RateYourMusicList", "RateYourMusicList2"]:
                print(chart)
                fullChartData.update(getFile(setFile(path, "current{0}FullChartArtistAlbumData.p".format(chart.lower()))))
                print("There are {0} artists in the full chart data".format(len(fullChartData)))
                artistAlbumData.update(getFile(setFile(path, "current{0}ArtistAlbumData.p".format(chart.lower()))))
                print("There are {0} artists in the artist album data".format(len(artistAlbumData)))
            self.fullChartData = fullChartData
            self.artistAlbumData = artistAlbumData
        else:
            self.fullChartData   = getFile(setFile(path, "current{0}FullChartArtistAlbumData.p".format(chart.lower())))
            print("There are {0} artists in the full chart data".format(len(self.fullChartData)))
            self.artistAlbumData = getFile(setFile(path, "current{0}ArtistAlbumData.p".format(chart.lower())))
            print("There are {0} artists in the artist album data".format(len(self.artistAlbumData)))
        
        self.artistData = {}
        
        self.artistKeyToNameMap = {}
        
    def analyze(self):
        for artistName,artistData in self.artistAlbumData.items():
            key = uuid4().hex
            
            cArtist = chartArtist(key, artistName)
            cArtist.setAllMediaData = artistData
            
            artistChartData = self.fullChartData[artistName]
            if artistChartData.get("Songs") is not None:
                songsData = artistChartData['Songs']
                cArtist.setSongsData(songsData)
            if artistChartData.get("Albums") is not None:
                albumsData = artistChartData["Albums"]
                cArtist.setSongsData(albumsData)
            if artistChartData.get("Song") is not None:
                songData = artistChartData["Song"]
                cArtist.setSongData(songData)
                
            self.artistData[key] = cArtist
            self.artistKeyToNameMap[key] = artistName
            
        print("Analyzed {0} artists".format(len(self.artistData)))
            
    def getArtistKeyMap(self):
        return self.artistKeyToNameMap
    
    def getArtistDataByKey(self, key):
        try:
            return self.artistData[key]
        except:
            raise ValueError("Key {0} does not exist".format(key))