from tools.pebble_sdk_platform import pebble_platforms, maybe_import_internal

def configure(conf):
    maybe_import_internal(conf.env)

    platform = pebble_platforms[conf.env.PLATFORM_NAME]
    define = 'MAX_FONT_GLYPH_SIZE={}'.format(platform['MAX_FONT_GLYPH_SIZE'])
    conf.env.append_value('DEFINES', [define])


def _autogen_applib_sources(bld):
    def applib_autogen(task):
        import applib_malloc
        # disable size checks for cutts
        if bld.is_cutts():
            disable_size_checks = True
        else:
            disable_size_checks = False
        applib_malloc.generate_files(task.inputs[0].abspath(), task.outputs[0].abspath(),
                                     task.outputs[1].abspath(), bld.env.MIN_SDK_VERSION,
                                     disable_size_checks)

    sources = ['applib_malloc.json']
    sources.extend(bld.path.parent.parent.parent.ant_glob('tools/applib_malloc*'))
    applib_malloc_h = bld.path.get_bld().make_node('applib_malloc.auto.h')
    applib_malloc_c = bld.path.get_bld().make_node('applib_malloc.auto.c')
    bld(rule=applib_autogen, source=sources, target=[applib_malloc_h, applib_malloc_c])

    return [applib_malloc_c]


def _get_bit_depth_excludes(bld):
    bit_depths = [1, 8]
    if bld.is_snowy_compatible() or bld.is_cutts() or bld.is_robert() or bld.is_obelix():
        depth = 8
    else:
        depth = 1
    return ['**/{}_bit/**'.format(x) for x in bit_depths if x != depth]


def build(bld):
    excludes = _get_bit_depth_excludes(bld)
    excludes += ['vendor']

    bld.recurse('vendor/tinflate')
    bld.recurse('vendor/uPNG')

    if bld.variant == 'prf':
        excludes += ['ui/dialogs/bt_conn_dialog.c',
                     'ui/dialogs/expandable_dialog.c',
                     'ui/option_menu_window.c',
                     'ui/progress_window.c',
                     'ui/selection_layer.c',
                     'ui/time_selection_window.c',
                     'ui/time_range_selection_window.c',
                     'voice/*']

    if not bld.capability('HAS_ROCKY_JS') or bld.variant == 'prf':
        excludes += ['rockyjs/*']

    if not bld.is_spalding():
        excludes.append('ui/window_stack_animation_round*')

    if not bld.capability('HAS_MICROPHONE'):
        excludes.append('voice/voice_window.c')

    if bld.capability('HAS_MAGNETOMETER'):
        excludes.append('compass_service_stub.c')
    else:
        excludes.append('compass_service.c')

    if bld.variant in ('applib', 'test_rocky_emx'):
        sources = bld.path.ant_glob('graphics/**/*.c', excl=excludes)
        sources.extend(bld.path.ant_glob('fonts/*.c', excl=excludes))
        sources.extend(bld.path.ant_glob('rockyjs/**/*.c', excl=excludes))
        sources.append('ui/layer.c')
        sources.append('ui/window.c')
    else:
        sources = bld.path.ant_glob('**/*.c', excl=excludes)
        sources.extend(_autogen_applib_sources(bld))
        # extract strings for i18n
        bld.gettext(source=sources, target='applib.pot')

    bld(export_includes=['.'], name='applib_includes')

    # Build the stlib
    bld.stlib(source=sources,
              target='applib',
              use=['target_includes',
                   'libbtutil_includes',
                   'fw_includes',
                   'upng',
                   'qr_code_generator',
                   'jerry_port_includes',
                   'jerry_runtime_config',
                   'jerry_common_config',
                   'jerry_core',
                   'freertos_includes',
                   'root_includes'])

# vim:filetype=python
