import sys
import os
import os.path

sp_dir = os.environ.get('SP_DIR', None)
cython_dir_opt = ''
if sp_dir:
    print(sp_dir)
    cython_dir_opt = ' --cython-dir={}'.format(os.path.join(sp_dir, 'cython'))

def get_c_flags():
    if 'linux' in sys.platform:
        return '-std=c++11 ' + os.environ.get('CFFLAGS', '')
    elif sys.platform.startswith('win'):
        return '/std:c++latest ' + os.environ.get('CFFLAGS', '')
    else:
        return os.environ.get('CFFLAGS', '')

CONFIG = {
    'framework' : 'other',
    'arg' : 'PYCMD runtests.py -vv' + cython_dir_opt,
    'env' : [{'CFLAGS' : get_c_flags(), 'PYTHONHOME': sys.prefix}],
    'expect_except' : None,
    'need_sources' : True,
    'location' : None,
    'set_cl_path': True,
    'use_re' : False,
    'should_subst' : True,
    'errors' : {
        '2.7' : {
            'Win' : {'runTest (__main__.CythonRunTestCase)',
                     'runTest (__main__.CythonUnitTestCase)' #this is caused due to new test-suite's inability to find stdint.h
                    },
            'Lin' : set(),
            'Mac' : {'runTest (__main__.CythonRunTestCase)'}
        },
        '3.5' : {
            'Win' : {'runTest (__main__.CythonCompileTestCase)', 'runTest (__main__.CythonRunTestCase)'},
            'Lin' : set(),
            'Mac' : {'runTest (__main__.CythonRunTestCase)'}
        },
        '3.6' : {
            'Win' : {'runTest (__main__.CythonCompileTestCase)', 'runTest (__main__.CythonRunTestCase)'},
            'Lin' : set(),
            'Mac' : {'runTest (__main__.CythonRunTestCase)'},
        },
    },
    'failures' : {
        '2.7' : {
            'Win' : { 'test_transpose (numpy_memoryview.__test__)',
                      'test_transpose (numpy_meALL DONE', #https://github.intel.com/SAT/cython-recipe/pull/19
                      'numpy_test (numpy_test.__test__)', #SAT-1338
                      'numpy_test ()',                    #SAT-1338
                      'test_coerce_to_numpy (numpy_memoryview)',
                      'test_coerce_to_numpy (numpy_memoryview.__test__)',
                      'test_memslice_getbuffer (numpy_memoryview)',
                      'test_memslice_getbuffer (numpy_memoryview.__test__)',
                      'test_deque_iterator_subtraction (line 26) (cpp_iterators.__test__)',
                      'test_deque_iteraALL DONE',  # same test as above, but works around tester quirk
                      'test_vector_iterator_subtraction (line 39) (cpp_iterators.__test__)',
            },
            'Lin' : { 'test_embed (__main__.EmbedTest)',
                      'numpy_test (numpy_test.__test__)', #SAT-1338
                      'numpy_test ()',                    #SAT-1338
                      'test_coerce_to_numpy (numpy_memoryview)',
                      'test_coerce_to_numpy (numpy_memoryview.__test__)',
                      'test_memslice_getbuffer (numpy_memoryview)',
                      'test_memslice_getbuffer (numpy_memoryview.__test__)',
            },
            'Mac' : { 'test_embed (__main__.EmbedTest)',
                      'numpy_test (numpy_test.__test__)', #SAT-1338
                      'numpy_test ()',                    #SAT-1338
                      'test_coerce_to_numpy (numpy_memoryview)',
                      'test_coerce_to_numpy (numpy_memoryview.__test__)',
                      'test_memslice_getbuffer (numpy_memoryview)',
                      'test_memslice_getbuffer (numpy_memoryview.__test__)',
            },
        },
        '3.5' : {
            'Win' : { 'runTest (__main__.EndToEndTest)',
                      'numpy_test (numpy_test.__test__)', #SAT-1338
                      'numpy_test ()',                    #SAT-1338
            },
            'Lin' : { 'runTest (__main__.EndToEndTest)',  # SAT-938
                      'test_embed (__main__.EmbedTest)',  # SAT-938
                      'numpy_test (numpy_test.__test__)', #SAT-1338
                      'numpy_test ()',                    #SAT-1338
            },
            'Mac' : { 'runTest (__main__.EndToEndTest)',
                      'test_embed (__main__.EmbedTest)',
                      'numpy_test (numpy_test.__test__)', #SAT-1338
                      'numpy_test ()',                    #SAT-1338
            },
        },
        '3.6' : {
            'Win' : {'runTest (__main__.EndToEndTest)',
                     'numpy_test (numpy_test.__test__)', #SAT-1338
                     'numpy_test ()',                    #SAT-1338
                     'test_coerce_to_numpy (numpy_memoryview)',
                     'test_coerce_to_numpy (numpy_memoryview.__test__)',
                     'test_memslice_getbuffer (numpy_memoryview)',
                     'test_memslice_getbuffer (numpy_memoryview.__test__)',
                     'test_struct_attributes (numpy_memoryview.__test__)',
                     'test_structarray_errors (numpy_memoryview.__test__)',
            },
            'Lin' : {#'test_embed (__main__.EmbedTest)', # SAT-938
                     'numpy_test (numpy_test.__test__)', #SAT-1338
                     'numpy_test ()',                    #SAT-1338
                     'test_coerce_to_numpy (numpy_memoryview)',
                     'test_coerce_to_numpy (numpy_memoryview.__test__)',
                     'test_memslice_getbuffer (numpy_memoryview)',
                     'test_memslice_getbuffer (numpy_memoryview.__test__)',
            },
            'Mac' : {'runTest (__main__.EndToEndTest)',
                     'test_embed (__main__.EmbedTest)',
                     'numpy_test (numpy_test.__test__)', #SAT-1338
                     'numpy_test ()',                    #SAT-1338
                     'test_coerce_to_numpy (numpy_memoryview)',
                     'test_coerce_to_numpy (numpy_memoryview.__test__)',
                     'test_memslice_getbuffer (numpy_memoryview)',
                     'test_memslice_getbuffer (numpy_memoryview.__test__)',
            },
        },
    },
    'validation' : {
        'errors' : {
            '2.7' : {
                'Win' : set(),
                'Lin' : { 'runTest (__main__.CythonRunTestCase)', },
                'Mac' : { 'test_cython3_pgo (Cython.Build.Tests.TestIpythonMagic.TestIPythonMagic)', },
            },
            '3.6' : {
                'Win' : set(),
                'Lin' : set(),
                'Mac' : { 'test_cython3_pgo (Cython.Build.Tests.TestIpythonMagic.TestIPythonMagic)', },
            },
        },
        'failures' : {
            '2.7' : {
                'Win' : set(),
                'Lin' : { 'runTest (__main__.EndToEndTest)', },
                'Mac' : set()
            },
            '3.6' : {
                'Win' : set(),
                'Lin' : { 'test_embed (__main__.EmbedTest)', },
                'Mac' : set()
            },
        },
    },
}


def fetch_config(pyver):
    from platform import system
    if system() == 'Windows':
        #remove hanging tests
        if pyver == '2.7':
            CONFIG['arg'] += ' -x common_include_dir -x cythonize_script_package -x lvalue_refs'
        else:
            CONFIG['arg'] += ' -x common_include_dir --no-code-style'

    return CONFIG
