"""Mission Pinball Framework Media Controller (mpf-mc) setup.py.

Notes:
    See pyproject.toml for the rest of the setup config.
"""

# TODO: this setup has only been tested with the .c files already generated by cython. No idea if it works with the .pyx files yet.

import sys

if sys.platform != 'win32':
    import pkgconfig as pc

from collections import defaultdict
from os.path import abspath, exists, join, isdir
from setuptools import Extension, setup

sound_file_source = 'mpfmc/core/audio/sound_file.pyx',
track_source = 'mpfmc/core/audio/track.pyx',
track_standard_source = 'mpfmc/core/audio/track_standard.pyx',
track_sound_loop_source = 'mpfmc/core/audio/track_sound_loop.pyx',
audio_interface_source = 'mpfmc/core/audio/audio_interface.pyx',
playlist_controller_source = 'mpfmc/core/audio/playlist_controller.pyx',
bitmap_font_source = 'mpfmc/uix/bitmap_font/bitmap_font.pyx',

def members_appended(*ds):  # shoutout to the borg backup project
        result = defaultdict(list)
        for d in ds:
            for k, v in d.items():
                assert isinstance(v, list)
                result[k].extend(v)
        return result

def get_isolated_env_paths():  # shoutout to the kivy project
    try:
        # sdl2_dev is installed before setup.py is run, when installing from
        # source due to pyproject.toml. However, it is installed to a
        # pip isolated env, which we need to add to compiler
        import kivy_deps.sdl2_dev as sdl2_dev
    except ImportError:
        return [], []

    root = abspath(join(sdl2_dev.__path__[0], '../../../..'))
    includes = [join(root, 'Include')] if isdir(join(root, 'Include')) else []
    libs = [join(root, 'libs')] if isdir(join(root, 'libs')) else []
    return includes, libs

def determine_sdl2():  # shoutout to the kivy project
    flags = {}
    includes, _ = get_isolated_env_paths()

    flags['libraries'] = ['SDL2', 'SDL2_mixer', 'SDL2_image']

    sdl2_paths = []
    for include in includes + [join(sys.prefix, 'include')]:
        sdl_inc = join(include, 'SDL2')
        if isdir(sdl_inc):
            sdl2_paths.append(sdl_inc)

    flags['include_dirs'] = sdl2_paths
    flags['extra_link_args'] = []
    flags['extra_compile_args'] = []
    flags['library_dirs'] = sdl2_paths

    # ensure headers for all the SDL2 and sub libraries are available
    libs_to_check = ['SDL2', 'SDL2_mixer', 'SDL2_image']
    for lib in libs_to_check:
        found = False
        for d in flags['include_dirs']:
            fn = join(d, '{}.h'.format(lib))
            if exists(fn):
                found = True
                print('SDL2: found {} header at {}'.format(lib, fn))
                break

        if not found:
            print('SDL2: missing sub library {}'.format(lib))

    return flags

if sys.platform == 'win32':

    # Since Windows doesn't have pkgconfig, we have to manually find the SDL2 include location. :(

    sdl2_include_path = determine_sdl2()['include_dirs'][0]
    print("Setting SDL2 include path:", sdl2_include_path)

    general_include_path = sdl2_include_path.strip('\SDL2') # strip the SDL2 folder
    print("Setting general include path:", general_include_path)

    libs_include_path = general_include_path[:-8] # strip the \include
    libs_include_path = join(libs_include_path, 'libs')
    print("Setting libs include path:", libs_include_path)

    audio_kws = {'define_macros': [('_THREAD_SAFE', None)],
                 'include_dirs': [sdl2_include_path, general_include_path],
                'libraries': ['SDL2_mixer', 'SDL2', 'gstreamer-1.0', 'glib-2.0', 'gobject-2.0'],
                'library_dirs': [libs_include_path]}

    bitmap_font_kws = {'define_macros': [('_THREAD_SAFE', None)],
                 'include_dirs': [sdl2_include_path, general_include_path],
                 'libraries': ['SDL2', 'SDL2_image'],
                'library_dirs': [libs_include_path]}

else:
    audio_kws = members_appended(pc.parse('SDL2_mixer'), pc.parse('gstreamer-1.0'))

    try:
        bitmap_font_kws = members_appended(pc.parse('sdl2'), pc.parse('SDL2_image'))
    except pc.pkgconfig.PackageNotFoundError:
        # SDL2_image is not found sometimes on MacOS, not sure why, but it works with the mixer paths too
        bitmap_font_kws = members_appended(pc.parse('sdl2'), pc.parse('SDL2_mixer'))

ext_modules = [
    Extension('mpfmc.core.audio.sound_file', [*sound_file_source], **audio_kws),
    Extension('mpfmc.core.audio.track', [*track_source], **audio_kws),
    Extension('mpfmc.core.audio.track_standard', [*track_standard_source], **audio_kws),
    Extension('mpfmc.core.audio.track_sound_loop', [*track_sound_loop_source], **audio_kws),
    Extension('mpfmc.core.audio.audio_interface', [*audio_interface_source], **audio_kws),
    Extension('mpfmc.core.audio.playlist_controller', [*playlist_controller_source], **audio_kws),
    Extension('mpfmc.uix.bitmap_font.bitmap_font', [*bitmap_font_source], **bitmap_font_kws,)
]

setup(
    ext_modules=ext_modules,
)