import unittest

from iiif_prezi3 import Annotation, AnnotationPage, Manifest


class AnnotationHelpersTests(unittest.TestCase):

    def test_manifest_add_annotation(self):
        anno_target = 'http://iiif.example.org/prezi/Canvas/0'
        anno = Annotation(target=anno_target)
        m = Manifest(id='http://iiif.example.org/prezi/Manifest/0', label={'en': ['Test Manifest']})
        m.add_annotation(anno)
        # test Manifest
        self.assertEqual(len(m.annotations), 1)
        anno_page = m.annotations[0]
        self.assertTrue(isinstance(anno_page, AnnotationPage), '`annotations` should contain an AnnotationPage')
        self.assertEqual(len(anno_page.items), 1)
        self.assertTrue(anno_page.items[0].id.startswith('http://example.org/iiif/'))  # autogenerated Annotation id
        self.assertEqual(anno_page.items[0].target, anno_target)

    def test_manifest_make_annotation(self):
        anno_target = 'http://iiif.example.org/prezi/Canvas/0'
        m = Manifest(id='http://iiif.example.org/prezi/Manifest/0', label={'en': ['Test Manifest']})
        anno = m.make_annotation(target=anno_target)
        # test returned Annotation
        self.assertTrue(isinstance(anno, Annotation), 'returned annotation should be an Annotation')
        self.assertTrue(anno.id.startswith('http://example.org/iiif/'))
        self.assertEqual(anno.target, anno_target)
        # test Manifest
        self.assertEqual(len(m.annotations), 1)
        anno_page = m.annotations[0]
        self.assertTrue(isinstance(anno_page, AnnotationPage), '`annotations` should contain an AnnotationPage')
        self.assertEqual(len(anno_page.items), 1)
        self.assertEqual(anno_page.items[0].target, anno_target)
        self.assertTrue(anno.id.startswith('http://example.org/iiif/'))  # autogenerated Annotation id
