from sqlalchemy.sql.expression import false

class cast():
    
    def __init__(self):
        pass
        
    def process(self, castdata, seriesid):
        datalist = {}
        linkdata = {}
        datalist['actors'] = []
        datalist['characters'] = []
        linklist = []
        for castmember in castdata:
            actordata = {}
            characterdata = {}
            tcharacterdata = castmember['character']
            tactordata = castmember['person']
            tselfdata = castmember['self']
            tvoicedata = castmember['voice']
            try:
                for key, value in tactordata.items():    
                    if tactordata[key] is None:
                        pass
                    else:
                        if key == 'image':
                            for key, value in tactordata['image'].items():
                                newkey = key + 'image' 
                                actordata[newkey] = tactordata['image'][key]
                        elif key == '_links':
                            for key, value in tactordata['_links']['self'].items():
                                actordata['apilink'] = tactordata['_links']['self'][key]    
                        elif key == 'country':
                            for key, value in tactordata['country'].items():
                                newkey = 'country' + key
                                actordata[newkey] = tactordata['country'][key]    
                        else:
                            actordata[key] = tactordata[key]
                        actordata['actorid'] = tactordata['id']
                        actordata['actorname'] = tactordata['name']
                    if tactordata['country'] == None:
                        actordata['countryname'] = None
                        actordata['countrytimezone'] = None
                        actordata['countrycode'] = None
                    if tactordata['birthday'] == None:
                        actordata['birthday'] = None
                    if tactordata['deathday'] == None:
                        actordata['deathday'] = None
                    if tactordata['gender'] == None:
                        actordata['gender'] = None
                    if tactordata['image'] == None:
                        actordata['mediumimage'] = None
                        actordata['originalimage'] = None
                    if tactordata['_links'] == None:
                        actordata['apilink'] = None
            except:
                print('Unable to process actor information.')
            else:
                actordata.pop('id')
            
            try:
                if tcharacterdata:
                    for key, value in tcharacterdata.items():    
                        if key is None:
                            pass
                        else:
                            if tcharacterdata['image']:
                                characterdata['mediumimage'] = tcharacterdata['image']['medium']
                                characterdata['originalimage'] = tcharacterdata['image']['original']
                            characterdata['apilink'] = tcharacterdata['_links']['self']['href']    
                            characterdata['tvmazeilink'] = tcharacterdata['url']
                            characterdata['characterid'] = tcharacterdata['id']
                            characterdata['charactername'] = tcharacterdata['name']    
                            characterdata['seriesid'] = seriesid
                            characterdata['actorid'] = actordata['actorid']
                        if tcharacterdata['image'] == None:
                            characterdata['mediumimage'] = None
                            characterdata['originalimage'] = None
                        if tactordata['_links'] == None:
                            actordata['apilink'] = None
                        if not tselfdata:
                            characterdata['self'] = 0
                        else:
                            characterdata['self'] = 1
                        if not tvoicedata:
                            characterdata['voice'] = 0
                        else:
                            characterdata['voice'] = 1                                              
            except Exception as e:
                print('Unable to process character information. -> ' + str(e))
            datalist['characters'].append(characterdata)
            datalist['actors'].append(actordata)
            linkdata = {'actorid': actordata['actorid'], 'characterid': characterdata['characterid']}
            linklist.append(linkdata)
        return(datalist, linklist)
        # except Exception as e:
        #     print('Unable to process cast data: ' + str(e))
        # else:
        #     return(datalist, linklist)
    
    def listcredits(self, seriesid):
        pass
    
    def __del__(self):
        pass