# Path: modules/mylib_create_banner.py
# def mylib_create_banner(game_name):
# 
#     nebula_dir = sys.path[0]
#     data_dir = os.getenv('HOME') + '/.games_nebula'
# 
#     banners_dir = data_dir + '/images/mylib/'
#     banner_path = banners_dir + game_name + '.jpg'
# 
#     if not os.path.exists(banners_dir):
#         os.makedirs(banners_dir)
# 
#     script_path_0 = data_dir + '/scripts/mylib/' + game_name + '/setup'
#     script_path_1 = nebula_dir + '/scripts/mylib/' + game_name + '/setup'
# 
#     if os.path.exists(script_path_0):
#         mylib_setup_script_path = script_path_0
#     else:
#         mylib_setup_script_path = script_path_1
# 
#     script_file = open(mylib_setup_script_path, 'r')
#     file_content = script_file.readlines()
#     raw_game_title = file_content[-1].replace('#', '').replace('\n', '')
# 
#     if sys.version_info[0] == 2:
#         game_title = raw_game_title.decode('utf-8')
#     elif sys.version_info[0] == 3:
#         game_title = raw_game_title
# 
#     script_file.close()
# 
#     banner = Image.new('1', (518, 240), 0)
#     draw = ImageDraw.Draw(banner)
#     font = ImageFont.truetype(nebula_dir + '/fonts/DejaVuSans.ttf', 24)
# 
#     game_title_width = font.getsize(game_title)[0]
#     game_title_height = font.getsize(game_title)[1]
# 
#     if game_title_width > 518:
# 
#         game_title_list = []
#         for i in range(0, len(game_title), 28):
#             game_title_list.append(game_title[i:i+28])
# 
#         all_lines_height = len(game_title_list) * game_title_height
#         full_title_y = 120 - all_lines_height/2
# 
#         n = 0
#         for line in game_title_list:
#             line_width = font.getsize(line)[0]
#             x = 259 - line_width/2
#             y = full_title_y + (game_title_height * n)
#             draw.text((x, y), line, fill=1, font=font)
#             n += 1
# 
#     else:
# 
#         x = 259 - game_title_width/2
#         y = 120 - game_title_height/2
#         draw.text((x, y), game_title, fill=1, font=font)
# 
#     banner.save(banner_path, 'JPEG')
# 
# Path: modules/goglib_get_banner.py
# def goglib_get_banner(banner_path, unavailable_path, game_id, *args):
# 
#     banner_height = 240
#     game_name = os.path.basename(banner_path).split('.jpg')[0]
#     print("Getting picture for: '" + game_name + "'")
# 
#     try:
#         token_path = os.getenv('HOME') + '/.config/lgogdownloader/galaxy_tokens.json'
#         token = Token.from_file(token_path)
#         if token.expired():
#             token.refresh()
#             token.save(token_path)
#         api = GogApi(token)
# 
#         prod = api.product(game_id)
#         prod.update_galaxy(expand=True)
#         banner_url = 'https:' + ''.join(prod.image_logo.split('_glx_logo'))
# 
#         banner_req = urllib_request(banner_url)
#         banner_data = urllib_urlopen(banner_req).read()
#         banner_file = open(banner_path, 'wb')
#         banner_file.write(banner_data)
#         banner_file.close()
# 
#         pic_src = Image.open(banner_path)
#         scale_lvl = banner_height/float(pic_src.size[1])
#         scaled_width = int(float(pic_src.size[0])*scale_lvl)
#         pic = pic_src.resize((scaled_width, banner_height), PIL.Image.ANTIALIAS)
#         pic.save(banner_path)
#         pic = pic.convert('L')
#         pic.save(unavailable_path)
# 
#     except urllib_urlerror as e:
#         print(e.reason)
#     except urllib_httperror as e:
#         print(e.code)
#         print(e.read())
#     except:
#         goglib_recreate_banner.goglib_recreate_banner(game_name, banner_path)


import os, sys
from modules import mylib_create_banner, goglib_get_banner

    if not os.path.exists(data_dir + '/images'):
        os.makedirs(data_dir + '/images')
    if not os.path.exists(data_dir + '/images/mylib'):
        os.makedirs(data_dir + '/images/mylib')
    if not os.path.exists(data_dir + '/images/goglib'):
        os.makedirs(data_dir + '/images/goglib')
    if not os.path.exists(data_dir + '/images/goglib/unavailable'):
        os.makedirs(data_dir + '/images/goglib/unavailable')

    if lib == 'goglib':
        color = args[0]
        if color == 'normal':
            path_0 = data_dir + '/images/goglib/' + game_name + '.jpg'
            path_1 = nebula_dir + '/images/goglib/' + game_name + '.jpg'
        elif color == 'gray':
            path_0 = data_dir + '/images/goglib/unavailable/' + game_name + '.jpg'
            path_1 = nebula_dir + '/images/goglib/unavailable/' + game_name + '.jpg'
    elif lib == 'mylib':
        path_0 = data_dir + '/images/mylib/' + game_name + '.jpg'
        path_1 = nebula_dir + '/images/mylib/' + game_name + '.jpg'

    if os.path.exists(path_0):
        return path_0
    elif os.path.exists(path_1):
        return path_1
    else:
        if lib == 'mylib':
            # FIX Hack to prevent crash in launchers (trying to create banner for game that not in lib)
            if len(args) == 0:

# Path: modules/mylib_create_banner.py
# def mylib_create_banner(game_name):
# 
#     nebula_dir = sys.path[0]
#     data_dir = os.getenv('HOME') + '/.games_nebula'
# 
#     banners_dir = data_dir + '/images/mylib/'
#     banner_path = banners_dir + game_name + '.jpg'
# 
#     if not os.path.exists(banners_dir):
#         os.makedirs(banners_dir)
# 
#     script_path_0 = data_dir + '/scripts/mylib/' + game_name + '/setup'
#     script_path_1 = nebula_dir + '/scripts/mylib/' + game_name + '/setup'
# 
#     if os.path.exists(script_path_0):
#         mylib_setup_script_path = script_path_0
#     else:
#         mylib_setup_script_path = script_path_1
# 
#     script_file = open(mylib_setup_script_path, 'r')
#     file_content = script_file.readlines()
#     raw_game_title = file_content[-1].replace('#', '').replace('\n', '')
# 
#     if sys.version_info[0] == 2:
#         game_title = raw_game_title.decode('utf-8')
#     elif sys.version_info[0] == 3:
#         game_title = raw_game_title
# 
#     script_file.close()
# 
#     banner = Image.new('1', (518, 240), 0)
#     draw = ImageDraw.Draw(banner)
#     font = ImageFont.truetype(nebula_dir + '/fonts/DejaVuSans.ttf', 24)
# 
#     game_title_width = font.getsize(game_title)[0]
#     game_title_height = font.getsize(game_title)[1]
# 
#     if game_title_width > 518:
# 
#         game_title_list = []
#         for i in range(0, len(game_title), 28):
#             game_title_list.append(game_title[i:i+28])
# 
#         all_lines_height = len(game_title_list) * game_title_height
#         full_title_y = 120 - all_lines_height/2
# 
#         n = 0
#         for line in game_title_list:
#             line_width = font.getsize(line)[0]
#             x = 259 - line_width/2
#             y = full_title_y + (game_title_height * n)
#             draw.text((x, y), line, fill=1, font=font)
#             n += 1
# 
#     else:
# 
#         x = 259 - game_title_width/2
#         y = 120 - game_title_height/2
#         draw.text((x, y), game_title, fill=1, font=font)
# 
#     banner.save(banner_path, 'JPEG')
# 
# Path: modules/goglib_get_banner.py
# def goglib_get_banner(banner_path, unavailable_path, game_id, *args):
# 
#     banner_height = 240
#     game_name = os.path.basename(banner_path).split('.jpg')[0]
#     print("Getting picture for: '" + game_name + "'")
# 
#     try:
#         token_path = os.getenv('HOME') + '/.config/lgogdownloader/galaxy_tokens.json'
#         token = Token.from_file(token_path)
#         if token.expired():
#             token.refresh()
#             token.save(token_path)
#         api = GogApi(token)
# 
#         prod = api.product(game_id)
#         prod.update_galaxy(expand=True)
#         banner_url = 'https:' + ''.join(prod.image_logo.split('_glx_logo'))
# 
#         banner_req = urllib_request(banner_url)
#         banner_data = urllib_urlopen(banner_req).read()
#         banner_file = open(banner_path, 'wb')
#         banner_file.write(banner_data)
#         banner_file.close()
# 
#         pic_src = Image.open(banner_path)
#         scale_lvl = banner_height/float(pic_src.size[1])
#         scaled_width = int(float(pic_src.size[0])*scale_lvl)
#         pic = pic_src.resize((scaled_width, banner_height), PIL.Image.ANTIALIAS)
#         pic.save(banner_path)
#         pic = pic.convert('L')
#         pic.save(unavailable_path)
# 
#     except urllib_urlerror as e:
#         print(e.reason)
#     except urllib_httperror as e:
#         print(e.code)
#         print(e.read())
#     except:
#         goglib_recreate_banner.goglib_recreate_banner(game_name, banner_path)


# Path: modules/paths.py
import os, sys
from modules import mylib_create_banner, goglib_get_banner


    if not os.path.exists(data_dir + '/images'):
        os.makedirs(data_dir + '/images')
    if not os.path.exists(data_dir + '/images/mylib'):
        os.makedirs(data_dir + '/images/mylib')
    if not os.path.exists(data_dir + '/images/goglib'):
        os.makedirs(data_dir + '/images/goglib')
    if not os.path.exists(data_dir + '/images/goglib/unavailable'):
        os.makedirs(data_dir + '/images/goglib/unavailable')

    if lib == 'goglib':
        color = args[0]
        if color == 'normal':
            path_0 = data_dir + '/images/goglib/' + game_name + '.jpg'
            path_1 = nebula_dir + '/images/goglib/' + game_name + '.jpg'
        elif color == 'gray':
            path_0 = data_dir + '/images/goglib/unavailable/' + game_name + '.jpg'
            path_1 = nebula_dir + '/images/goglib/unavailable/' + game_name + '.jpg'
    elif lib == 'mylib':
        path_0 = data_dir + '/images/mylib/' + game_name + '.jpg'
        path_1 = nebula_dir + '/images/mylib/' + game_name + '.jpg'

    if os.path.exists(path_0):
        return path_0
    elif os.path.exists(path_1):
        return path_1
    else:
        if lib == 'mylib':
            # FIX Hack to prevent crash in launchers (trying to create banner for game that not in lib)
            if len(args) == 0:

                mylib_create_banner.mylib_create_banner(game_name)