from __future__ import print_function, division, absolute_import

import defcon

from . import DATADIR
import pytest
import py

from cu2qu.ufo import CURVE_TYPE_LIB_KEY
from cu2qu.cli import main


TEST_UFOS = [
    py.path.local(DATADIR).join("RobotoSubset-Regular.ufo"),
    py.path.local(DATADIR).join("RobotoSubset-Bold.ufo"),
]


@pytest.fixture
def test_paths(tmpdir):
    result = []
    for path in TEST_UFOS:
        new_path = tmpdir / path.basename
        path.copy(new_path)
        result.append(new_path)
    return result


class MainTest(object):

    @staticmethod
    def run_main(*args):
        main([str(p) for p in args if p])

    def test_single_input_no_output(self, test_paths):
        ufo_path = test_paths[0]

        self.run_main(ufo_path)

        font = defcon.Font(str(ufo_path))
        assert font.lib[CURVE_TYPE_LIB_KEY] == "quadratic"

    def test_single_input_output_file(self, tmpdir):
        input_path = TEST_UFOS[0]
        output_path = tmpdir / input_path.basename
        self.run_main('-o', output_path, input_path)

        assert output_path.check(dir=1)

    def test_multiple_inputs_output_dir(self, tmpdir):
        output_dir = tmpdir / "output_dir"
        self.run_main('-d', output_dir, *TEST_UFOS)

        assert output_dir.check(dir=1)
        outputs = set(p.basename for p in output_dir.listdir())
        assert "RobotoSubset-Regular.ufo" in outputs
        assert "RobotoSubset-Bold.ufo" in outputs

    def test_interpolatable_inplace(self, test_paths):
        self.run_main('-i', *test_paths)
        self.run_main('-i', *test_paths)  # idempotent

    @pytest.mark.parametrize(
        "mode", ["", "-i"], ids=["normal", "interpolatable"])
    def test_copytree(self, mode, tmpdir):
        output_dir = tmpdir / "output_dir"
        self.run_main(mode, '-d', output_dir, *TEST_UFOS)

        output_dir_2 = tmpdir / "output_dir_2"
        # no conversion when curves are already quadratic, just copy
        self.run_main(mode, '-d', output_dir_2, *output_dir.listdir())
        # running again overwrites existing with the copy
        self.run_main(mode, '-d', output_dir_2, *output_dir.listdir())

    def test_multiprocessing(self, tmpdir, test_paths):
        self.run_main(*(test_paths + ["-j"]))

add cli_test.py