from __future__ import print_function, division, absolute_import

import os
import tempfile

from fontTools.ttLib import TTFont

from .runner import main as runner
from .differ import main as differ

TOOL = 'sfntedit'
CMD = ['-t', TOOL]

LIGHT = 'light.otf'
ITALIC = 'italic.otf'

data_dir_path = os.path.join(os.path.split(__file__)[0], TOOL + '_data')


def _get_expected_path(file_name):
    return os.path.join(data_dir_path, 'expected_output', file_name)


def _get_input_path(file_name):
    return os.path.join(data_dir_path, 'input', file_name)


def _get_temp_file_path():
    file_descriptor, path = tempfile.mkstemp()
    os.close(file_descriptor)
    return path


def _generate_ttx_dump(font_path, tables=None):
    with TTFont(font_path) as font:
        temp_path = _get_temp_file_path()
        font.saveXML(temp_path, tables=tables)
        return temp_path


def _font_has_table(font_path, table_tag):
    with TTFont(font_path) as font:
        return table_tag in font


# -----
# Tests
# -----

# This test is commented because sfntedit does not exit with 0
# def test_no_options():
#     actual_path = runner(CMD + ['-r', '-f', LIGHT])
#     expected_path = _get_expected_path('list_sfnt.txt')
#     assert differ([expected_path, actual_path, '-l', '1'])


def test_extract_table():
    save_path = _get_temp_file_path()
    actual_path = runner(CMD + ['-o', 'x', '_GDEF={}'.format(save_path),
                                '-f', LIGHT, '-s', save_path])
    expected_path = _get_expected_path('GDEF_light.tb')
    assert differ([expected_path, actual_path, '-m', 'bin'])

# This test fails on Windows and passes on Mac
# https://ci.appveyor.com/project/adobe-type-tools/afdko/build/1.0.108
# https://travis-ci.org/adobe-type-tools/afdko/builds/372889493
# def test_delete_table():
#     actual_path = runner(CMD + ['-o', 'd', '_GDEF', '-f', LIGHT])
#     expected_path = _get_expected_path('light_n_GDEF.otf')
#     assert differ([expected_path, actual_path, '-m', 'bin'])


def test_add_table():
    input_path = _get_input_path('GDEF_italic.tb')
    assert _font_has_table(_get_input_path(ITALIC), 'GDEF') is False
    actual_path = runner(CMD + ['-o', 'a', '_GDEF={}'.format(input_path),
                                '-f', ITALIC])
    expected_path = _get_expected_path('italic_w_GDEF.otf')
    assert differ([expected_path, actual_path, '-m', 'bin']) is False
    assert _font_has_table(actual_path, 'GDEF')
    actual_ttx = _generate_ttx_dump(actual_path)
    expected_ttx = _generate_ttx_dump(expected_path)
    assert differ([expected_ttx, actual_ttx,
                   '-s', '    <checkSumAdjustment'])


def test_linux_ci_failure_bug570():
    input_path = _get_input_path('1_fdict.cff')
    actual_path = runner(CMD + ['-o', 'a', '_CFF={}'.format(input_path),
                                '-f', 'core.otf'])
    expected_path = _get_expected_path('1_fdict.otf')
    assert differ([expected_path, actual_path, '-m', 'bin'])
