import requests
import json

class Processor():
    def __init__(self):
        global loginurl
        global refreshurl
        global seriesurl
        global actorurl
        global episodeurl
        global episodequeryurl
        global episodeparamurl
        global episodesummaryurl
        global filterurl
        global filterparamurl
        global imageurl
        global imagequeryurl
        global imageparamurl
        global username
        global apikey
        global userkey
        global jwtoken
        global refreshtoken
        loginurl = 'https://api.thetvdb.com/login'
        refreshurl = 'https://api.thetvdb.com/refresh_token'
        seriesurl = 'https://api.thetvdb.com/series/{id}'
        episodeidurl = 'https://api.thetvdb.com/episodes/{id}'
        actorurl='https://api.thetvdb.com/series/{id}/actors'
        episodeurl='https://api.thetvdb.com/series/{id}/episodes'
        episodequeryurl='https://api.thetvdb.com/series/{id}/episodes/query'
        episodeparamurl='https://api.thetvdb.com/series/{id}/episodes/query/params'
        episodesummaryurl='https://api.thetvdb.com/series/{id}/episodes/summary'
        filterurl='https://api.thetvdb.com/series/{id}/filter'
        filterparamurl='https://api.thetvdb.com/series/{id}/filter/params'
        imageurl='https://api.thetvdb.com/series/{id}/images'
        imagequeryurl='https://api.thetvdb.com/series/{id}/images/query'
        imageparamurl='https://api.thetvdb.com/series/{id}/images/query/params'
        username = 'wpjonesnhcjc'
        apikey = 'NACCS2LUWCFEFABX'
        userkey = 'UUSZHUQ8CY7N31CF'
        #jwtoken = login.run(self)
        
    def __del__(self):
        pass

class login():
    def __init__(self):
        pass
    
    def run(self):    
        with requests.post(loginurl,  json={"username": username, "apikey": apikey, "userkey": userkey}) as r:
            response = r.json()
        jwtoken = response['token']
        #refreshtoken = self.refresh(jwtoken)
        return(jwtoken)
    
    def refresh(self, token):
        token= "Bearer " + token
        with requests.get(refreshurl, headers={'Authorization': token}) as r:
            response = r.json()
        refreshtoken = response['token']
        return(refreshtoken)
    
class loadjson():
    def __init__(self):
        pass
    
    def loadseries(seriesid, token):
        url = seriesurl.replace('{id}', seriesid)
        #print(str(url))
        seriesdata = loadjson.load(url, token)
        return(seriesdata)

    def loadepisodes(seriesid, token):
        url = episodeurl.replace('{id}', seriesid)
        #print(str(url))
        seriesdata = loadjson.load(url, token)
        return(seriesdata)
    
    def load(url, token):
        token= "Bearer " + token
        with requests.get(url, headers={'Authorization': token}) as r:
            response = r.json()
        return(response)
        
    def insert(self, updater, tablename):
        for key, value in updater.items():
            ins = tablename.insert().values( key = value )
            print(str(ins))
        result = engine.execute(ins)
    
    def populate(self, jsonurl, seriesid):
        try:
            with urllib.request.urlopen(jsonurl) as response:
                html = response.read()
            jsoninput = html
            return jsoninput
        except:
           pass
        
    def __del__(self):
        pass

class showprocess():
    
    def __init__(self):
        pass
    
    def process(self, metadata, tempshowload):
        dataresult = []
        tableresult = []
        if tempshowload == None:
            pass
        else:
            showload = json.loads(tempshowload)
            networkid = None
            try:
                network = {}
                for key, value in showload['network'].items():
                    if key == 'country':
                        for key, value in showload['network']['country'].items():
                            if key == 'name':
                                newkey = 'country'
                                network[newkey] = showload['network']['country'][key]
                            else:
                                newkey = 'country' + key
                                network[newkey] = showload['network']['country'][key]
                    else:
                        newkey = 'network' + key
                        network[newkey] = showload['network'][key]
            except:
                #print('Failed to insert network information.')
                showload['networkid'] = '0'
            else:    
                showload.pop('network')
                showload['networkid'] = network['networkid']
                dataresult.append(network)
                tableresult.append(metadata.tables['[updates].tvnetworknewvals'])
                #insertresult = insert_data(network, tables[4])
            try:
                webchannel = {}
                for key, value in showload['webChannel'].items():
                    if key == 'country':
                            if showload['webChannel'][key] is None:
                                pass
                            else:
                                for key, value in showload['webChannel']['country'].items():
                                    newkey = 'webChannelcountry'+ key
                                    webchannel[newkey] = value 
                    else:
                        webchannel[key] = value
                if key == 'name':
                    newkey = 'webChannel' + key
                    webhchannel[newkey] = value 
                if key == 'timezone':
                    newkey = 'webChannelcountry'+ key
                    webchannel[newkey] = value
            except:
                #print('Failed to insert webchannel information.')
                showload['webchannelid'] = '0'
            else:
                showload.pop('webChannel')
                showload['webchannelid'] = webchannel['id']
                webchannel['webchannelname'] = webchannel['name']
                try:
                    dataresult.append(webchannel)
                    tableresult.append(metadata.tables['[updates].tvwebchannelnewvals'])
                    #insertresult = insert_data(webchannel, tables[5])
                except sqlalchemy.exc.IntegrityError as e:
                    pass
                except Exception as e:
                    print('Failed to insert webchannel information.')
            try:
                showload['seriesid'] = showload['id']
            except:
                pass
            
            #print('TV Show: ' + showload['seriesname'])
            
            if showload['image']:
                for key, value in showload['image'].items():
                    newkey = key + 'image' 
                    showload[newkey] = value
                showload.pop('image')
            else:
                showload.pop('image')
            
            if showload['externals']:
                for key, value in showload['externals'].items():
                    showload[key] = value
                showload.pop('externals')
            else:
                showload.pop('externals')
            
            if showload['rating']:
                for key, value in showload['rating'].items():
                    showload['rating'] = value
            
            if showload['schedule']:
                showload['time'] = showload['schedule']['time']
                showload['days'] = ", ".join(showload['schedule']['days'])
                showload.pop('schedule')
                
            for key, value in showload['_links']['self'].items():
                showload[key] = value    
            showload.pop('_links')
            
            showload.pop('id')
            
            showload['genres'] = ", ".join(showload['genres'])
            
            # Finish transforming to match table columns
            showload['seriesname'] = showload.pop('name')
            showload['tvrageid'] = showload.pop('tvrage')
            showload['tvdbid'] = showload.pop('thetvdb')
            showload['imdbid'] = showload.pop('imdb')
            showload['serieslanguage'] = showload.pop('language')
            showload['seriesstatus'] = showload.pop('status')
            showload['airdays'] = showload.pop('days')
            showload['airtime'] = showload.pop('time')
            showload['showtype'] = showload.pop('type')
            showload['overview'] = showload.pop('summary')
            showload['showurl'] = showload.pop('url')
            showload['apilink'] = showload.pop('href')
            showload['officialsite'] = showload.pop('officialSite')
            
            results = []
            results.append(dataresult)
            results.append(tableresult)
            results.append(showload)
            return (results)
