from pathlib import Path

from rdflib import ConjunctiveGraph

from nanopub import Nanopub
from nanopub.utils import MalformedNanopubError
from tests.conftest import java_wrap, testsuite_conf


def test_testsuite_valid_plain():
    test_files = Path("./tests/testsuite/valid/plain").rglob('*')

    for test_file in test_files:
        print(f'☑️ Testing valid plain nanopub: {test_file}')
        if "/signed." in str(test_file):
            continue

        np_g = ConjunctiveGraph()
        if str(test_file).endswith(".xml"):
            np_g.parse(test_file, format="trix")
        else:
            np_g.parse(test_file)

        np = Nanopub(
            conf=testsuite_conf,
            rdf=np_g
        )
        assert np.is_valid


def test_testsuite_valid_signed():
    test_files = Path("./tests/testsuite/valid/signed").rglob('*')

    for test_file in test_files:
        print(f'☑️ Testing valid plain nanopub: {test_file}')
        if "/signed." in str(test_file):
            continue

        np = Nanopub(
            conf=testsuite_conf,
            rdf=test_file
        )
        assert np.is_valid
        assert np.metadata.trusty is not None
        assert np.metadata.signature is not None
        # TODO: we should be able to validate this signature?
        # assert np.has_valid_signature



def test_testsuite_valid_trusty():
    test_files = Path("./tests/testsuite/valid/trusty").rglob('*')

    for test_file in test_files:
        print(f'☑️ Testing valid trusty nanopub: {test_file}')
        np = Nanopub(
            conf=testsuite_conf,
            rdf=test_file
        )
        assert np.is_valid
        assert np.metadata.trusty is not None



def test_testsuite_sign_valid():
    """Test to sign various files from transform and valid folder.
    Compare the generated trusty URI to the one generated by nanopub-java"""
    test_files = [
        "./tests/testsuite/transform/signed/rsa-key1/simple1.in.trig",
        "./tests/testsuite/transform/trusty/aida1.in.trig",
        "./tests/testsuite/transform/trusty/simple1.in.trig",
        "./tests/testsuite/valid/plain/aida1.trig",
        "./tests/testsuite/valid/plain/simple1.nq",
        "./tests/testsuite/valid/plain/simple1.trig",
        "./tests/testsuite/valid/plain/simple1.xml",
    ]

    for test_file in test_files:
        print(f'✒️ Testing signing valid nanopub: {test_file}')
        np_g = ConjunctiveGraph()
        if test_file.endswith(".xml"):
            np_g.parse(test_file, format="trix")
        else:
            np_g.parse(test_file)

        np = Nanopub(
            conf=testsuite_conf,
            rdf=np_g
        )
        java_np = java_wrap.sign(np)
        np.sign()
        assert np.has_valid_signature
        assert np.has_valid_trusty
        assert np.is_valid
        assert np.source_uri == java_np


def test_testsuite_valid_signature():
    test_files = [
        "./tests/testsuite/valid/signed/simple1-signed-rsa.trig",
        # "./tests/testsuite/valid/signed/simple1-signed-dsa.trig",
    ]
    # java -jar lib/nanopub-1.38-jar-with-dependencies.jar sign tests/testsuite/transform/signed/rsa-key1/simple1.in.trig

    for test_file in test_files:
        print(f'✅ Testing validating signed valid nanopub: {test_file}')
        np = Nanopub(
            conf=testsuite_conf,
            rdf=Path(test_file)
        )
        assert np.is_valid
        assert np.has_valid_signature
        assert np.has_valid_trusty


def test_testsuite_invalid_plain():
    test_files = Path("./tests/testsuite/invalid/plain").rglob('*')

    for test_file in test_files:
        print(f'❎ Testing validating invalid nanopub: {test_file}')
        try:
            np = Nanopub(
                conf=testsuite_conf,
                rdf=Path(test_file)
            )
            np.is_valid
            assert False
        except MalformedNanopubError as e:
            print(e)
            assert True
