from importlib.resources import files
from plone import api
from plone.namedfile.file import NamedBlobImage
from ploneintranet import api as pi_api
from ploneintranet.userprofile.adapters import ProfileAvatarTraverser
from ploneintranet.userprofile.interfaces import IMemberGroup
from ploneintranet.userprofile.tests.base import BaseTestCase
from slc.mailrouter.interfaces import IEmailToUser
from zope.component import getAdapter
from zope.interface import directlyProvides


class TestMemberGroupAdapter(BaseTestCase):
    def setUp(self):
        BaseTestCase.setUp(self)
        self.login_as_portal_owner()
        self.folder1 = api.content.create(self.portal, "Folder", "folder-1", "Folder 1")
        self.folder2 = api.content.create(
            self.folder1, "Folder", "folder-2", "Folder 2"
        )
        self.page = api.content.create(self.folder2, "Document", "page", "My Page")

    def test_portal(self):
        with self.assertRaises(TypeError):
            IMemberGroup(self.portal)

    def test_page_noadapter(self):
        with self.assertRaises(TypeError):
            IMemberGroup(self.page)

    def test_direct(self):
        directlyProvides(self.folder1, IMemberGroup)
        self.assertEqual(IMemberGroup(self.folder1), self.folder1)

    def test_indirect(self):
        directlyProvides(self.folder1, IMemberGroup)
        self.assertEqual(IMemberGroup(self.page), self.folder1)


class TestEmail2User(BaseTestCase):
    def test_email2user(self):
        adapter = getAdapter(self.portal, IEmailToUser, name="email2user_vs_membrane")
        pi_api.userprofile.create("johndoe", email="jd@example.com")

        # Test dummy queries
        self.assertIsNone(adapter(""))
        self.assertIsNone(adapter(None))
        # Test proper query
        self.assertEqual(adapter("jd@example.com").getUserId(), "johndoe")


class TestProfileAvatarTraverser(BaseTestCase):
    def setUp(self):
        super().setUp()
        self.traverser = ProfileAvatarTraverser(self.portal, self.request)
        self.default_avatar = (
            files("ploneintranet.userprofile") / "browser" / "defaultUser-168.png"
        ).read_bytes()

    def test_user_enabled_default_avatar(self):
        profile = pi_api.userprofile.create(
            "johndoe", email="jd@example.com", approve=True
        )
        uuid = profile.UID()
        avatar = self.traverser.traverse(f"{uuid}.jpg")
        self.assertEqual(avatar, self.default_avatar)

    def test_user_enabled_custom_avatar(self):
        img_data = (files("ploneintranet.tests") / "plone.jpg").read_bytes()
        profile = pi_api.userprofile.create(
            "johndoe", email="jd@example.com", approve=True
        )
        profile.portrait = NamedBlobImage(data=img_data, filename="foo.jpg")
        uuid = profile.UID()
        avatar = self.traverser.traverse(f"{uuid}.jpg")
        # not equal to img_data, since the original got scaled down
        self.assertTrue(avatar.startswith(b"\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01"))

    def test_user_disabled(self):
        profile = pi_api.userprofile.create("johndoe", email="jd@example.com")
        uuid = profile.UID()
        avatar = self.traverser.traverse(f"{uuid}.jpg")
        self.assertEqual(avatar, self.default_avatar)

    def test_invalid_uuid(self):
        avatar = self.traverser.traverse("foobar.jpg")
        self.assertEqual(avatar, self.default_avatar)
