from sys import prefix
from os.path import join
from fsUtils import isDir, isFile, setFile, mkDir
from fileIO import fileIO
from fsUtils import dirUtil
from timeUtils import timestat
from pandas import DataFrame, Series

class masterIgnoreID:
    def __init__(self, install=False, debug=False):
        print("{0} {1} {2}".format("="*25,"masterIgnoreID(install={0})".format(install),"="*25))
        self.debug = debug
        
        self.io = fileIO()
        self.musicDBDir = join(prefix, 'musicdb')
        self.initializeData() if install is False else self.installData()
    
    def initializeData(self):
        return
        self.summary(self.getData(fast=True, local=False))
        
    def installData(self):
        if not isDir(self.musicDBDir):
            print("Install: Making Prefix Dir [{0}]".format(self.musicDBDir))
            mkDir(self.musicDBDir)
        if not isFile(self.getFilename(fast=True, local=False)):
            print("Install: Creating Prefix Data From Local Data")
            self.writeToMainPickleFromLocalYAML()
                        
    def summary(self, manualDBIDs=None):
        return

            
    #########################################################################################################
    #
    # I/O
    #
    #########################################################################################################
    def setInternal(self, manualDBIDs=None):
        self.manualDBIDs = self.getData(fast=True, local=False) if manualDBIDs is None else manualDBIDs
        
    
        
    def getFilename(self, fast, local):
        basename="manualIgnoreDBIDs"
            
        self.pfname = join(self.musicDBDir, "{0}.p".format(basename))
        self.yfname = join(self.musicDBDir, "{0}.yaml".format(basename))

        self.localpfname = "{0}.p".format(basename)
        self.localyfname = "{0}.yaml".format(basename)
        
        if fast is True:
            if local is True:
                return self.localpfname
            else:
                return self.pfname
        else:
            if local is True:
                return self.localyfname
            else:
                return self.yfname
            
        raise ValueError("Somehow didn't get a filename!")
        
        
    def getIgnoreDBIDs(self, dbName):
        igDBIDs  = {}
        miidData = self.getData()
        for gType,gData in miidData["General"].items():
            for db,dbID in gData.items():
                if igDBIDs.get(db) is None:
                    igDBIDs[db] = {}
                igDBIDs[db][dbID] = True
        for db,dbData in miidData["Specific"].items():
            if igDBIDs.get(db) is None:
                igDBIDs[db] = {}
            for artistName,dbID in dbData.items():
                igDBIDs[db][dbID] = True

        igDBIDs = {db: list(dbIDs.keys()) for db,dbIDs in igDBIDs.items()}
        return igDBIDs.get(dbName, [])
        

    def getData(self, fast=True, local=False):
        ftype = {True: "Pickle", False: "YAML"}
        ltype = {True: "Local", False: "Main"}
        #ts = timestat("Getting Master Ignore IDs Data From {0} {1} File".format(ltype[local], ftype[fast]))
        fname = self.getFilename(fast, local)
        manualDBIDs = self.io.get(fname)
        manualDBIDs = manualDBIDs.to_dict() if isinstance(manualDBIDs, Series) else manualDBIDs
        #ts.stop()
    
        return manualDBIDs
    

    def writeToLocalYamlFromMainPickle(self):
        ts = timestat("Writing To Local YAML From Main Pickle")
        manualDBIDs = self.getData(fast=True, local=False)
        self.saveData(manualDBIDs, fast=False, local=True)
        ts.stop()

    def writeToMainPickleFromLocalYAML(self):
        ts = timestat("Writing To Main Pickle From Local YAML")
        manualDBIDs = self.getData(fast=False, local=True)
        self.saveData(manualDBIDs, fast=True, local=False)
        ts.stop()

    def saveData(self, manualDBIDs, fast=True, local=False):
        ftype = {True: "Pickle", False: "YAML"}
        ltype = {True: "Local", False: "Main"}
        ts = timestat("Saving Manual Entries Data To {0} {1} File".format(ltype[local], ftype[fast]))
        
        fname = self.getFilename(fast, local)
        if fast is False:
            toSave = manualDBIDs.to_dict() if isinstance(manualDBIDs,Series) else manualDBIDs
        else:
            toSave = Series(manualDBIDs) if isinstance(manualDBIDs,dict) else manualDBIDs
        
        self.io.save(idata=toSave, ifile=fname)
        
        ts.stop()