import os
import progressbar
from pathlib import Path
from time import sleep
from tqdm import tqdm

class Cacher(object):

    def __init__(self):
        pass

    def process(self, options, jget, new_updates, ROOTDIR, URLS):
        SERIESCACHE = str(ROOTDIR) + '/cache/series/'
        EPISODECACHE = str(ROOTDIR) + '/cache/episodes/'
        CASTCACHE = str(ROOTDIR) + '/cache/cast/'
        CREWCACHE = str(ROOTDIR) + '/cache/crew/'
        TEMPDIR = ROOTDIR + '/temp/'
        available_updates = {}
        updateList = []
        if options['refreshcache'] == '1':
            print('Refreshing cache directory.')
            print('Checking cache directory for missing files.')
            for update in new_updates:
                available_updates[update[0]] = update[1]
            updateList = [str(key) for key in available_updates.keys()]
            for series in tqdm(range(len(new_updates)), desc='Checking cache', unit='series'):
                seriesid = new_updates[series][0]
                cachefile = str(seriesid) + '.json'
                seriescachefile = f'{SERIESCACHE}{cachefile}'
                episodecachefile = f'{EPISODECACHE}{cachefile}'
                castcachefile = f'{CASTCACHE}{cachefile}'
                crewcachefile = f'{CREWCACHE}{cachefile}'
                self.check_and_download(jget, URLS['showurl'], str(seriesid), new_updates[0], seriescachefile, 1500)
                sleep(1)
                self.check_and_download(jget, URLS['episodesurl'], str(seriesid), new_updates[0], episodecachefile, 1500)
                sleep(1)
                self.check_and_download(jget, URLS['casturl'], str(seriesid), new_updates[0], castcachefile, 500)
                sleep(1)
                self.check_and_download(jget, URLS['crewurl'], str(seriesid), new_updates[0], crewcachefile, 500)
                sleep(1)
        print('Refresh completed.')

    def check_and_download(self, jget, url, SERIESID, series_id, cachefile, size_limit):
        try:
            my_abs_path = Path(cachefile).resolve(strict=True)
            file_stats = os.stat(cachefile)
            if file_stats.st_size <= size_limit:
                #print(f'File Size for {cachefile} in Bytes is {file_stats.st_size}. File too small. Attempting to download.')
                jget.fetch_json(url.replace(SERIESID, str(series_id)), series_id, cachefile)
        except FileNotFoundError:
            #print(f'{cachefile} does not exist. Attempting to download.')
            jget.fetch_json(url.replace(SERIESID, str(series_id)), series_id, cachefile)
        except Exception as e:
            print(f'Unable to download {cachefile}. {e}')

    def update_cache(self, jget, updateList, ROOTDIR, URLS, options, lprint):
        if options['skipcache'] == '0':
            SERIESCACHE = str(ROOTDIR) + '/cache/series/'
            EPISODECACHE = str(ROOTDIR) + '/cache/episodes/'
            CASTCACHE = str(ROOTDIR) + '/cache/cast/'
            CREWCACHE = str(ROOTDIR) + '/cache/crew/'
            TEMPDIR = ROOTDIR + '/temp/'
            print('Now downloading updates.')
            updaterange = len(updateList)
            for seriesid in tqdm(range(updaterange), desc='Downloading updates', unit='seriesid'):
                cachefile = f'{updateList[seriesid]}.json'
                seriescachefile = SERIESCACHE + cachefile
                episodecachefile = EPISODECACHE + cachefile
                castcachefile = CASTCACHE + cachefile
                crewcachefile = CREWCACHE + cachefile
                self.download_json(jget, URLS['showurl'], updateList[seriesid], seriescachefile, lprint)
                sleep(1)
                self.download_json(jget, URLS['episodesurl'], updateList[seriesid], episodecachefile, lprint)
                sleep(1)
                self.download_json(jget, URLS['casturl'], updateList[seriesid], castcachefile, lprint)
                sleep(1)
                self.download_json(jget, URLS['crewurl'], updateList[seriesid], crewcachefile, lprint)
                sleep(1)
                
    def download_json(self, jget, url, seriesid, cachefile, lprint):
        try:
            jget.fetch_json(url.replace('<seriesid>', str(seriesid)), seriesid, cachefile)
        except Exception as e:
            lprint.logprint('info', f'Unable to acquire {cachefile}. {e}')

    def __del__(self):
        pass


