import os

from django.core.files.uploadedfile import SimpleUploadedFile
from django.test import TestCase

from .forms import JobForm, JobGroupForm, MemberForm
from .models import Job, JobGroup, Member


image_path = os.path.join(os.path.dirname(__file__), "tests/files/peter.jpg")


class MemberTestCase(TestCase):
    def setUp(self):
        member = Member()
        member.firstname = "Peter"
        member.surname = "Traunmüller"
        member.nickname = "Pet"
        member.mailaccount = "peter@fet.at"
        member.role = "A"
        member.save()

    def test_member(self):
        member = Member.objects.get(firstname="Peter")
        self.assertEqual(member.__str__(), "Peter Traunmüller")
        self.assertEqual(member.role, "A")


class MemberFormTestCase(TestCase):
    def test_basic_form(self):
        image = SimpleUploadedFile(
            name="peter.jpg",
            content=open(image_path, "rb").read(),
            content_type="image/jpeg",
        )

        form = MemberForm(
            data={
                "firstname": "Peter",
                "surname": "Traunmüller",
                "nickname": "Pet",
                "mailaccount": "peter@fet.at",
                "role": "A",
            },
            files={"image": image},
        )

        self.assertTrue(form.is_valid())
        form.save()

        member = Member.objects.get(firstname="Peter")
        self.assertEqual(member.__str__(), "Peter Traunmüller")

    def test_advanced_form(self):
        # test blank fields, like phone number
        image = SimpleUploadedFile(
            name="peter.jpg",
            content=open(image_path, "rb").read(),
            content_type="image/jpeg",
        )

        form = MemberForm(
            data={
                "firstname": "Peter",
                "surname": "Traunmüller",
                "nickname": "Pet",
                "mailaccount": "peter@fet.at",
                "role": "A",
                "phone": "+4364412345678",
            },
            files={"image": image},
        )

        self.assertTrue(form.is_valid())
        form.save()

        member = Member.objects.get(firstname="Peter")
        self.assertEqual(member.phone, "+4364412345678")

    def test_form_error_no_image(self):
        form = MemberForm(
            data={
                "firstname": "Peter",
                "surname": "Traunmüller",
                "nickname": "Pet",
                "mailaccount": "peter@fet.at",
                "role": "A",
            },
        )

        self.assertFalse(form.is_valid())
        self.assertEqual(
            form.errors.as_data()["__all__"][0].message, "Es fehlt das Profilbild."
        )

    def test_form_wrong_mailaccount(self):
        image = SimpleUploadedFile(
            name="peter.jpg",
            content=open(image_path, "rb").read(),
            content_type="image/jpeg",
        )

        form = MemberForm(
            data={
                "firstname": "Peter",
                "surname": "Traunmüller",
                "nickname": "Pet",
                "mailaccount": "peter@feet.at",
                "role": "A",
            },
            files={"image": image},
        )

        self.assertFalse(form.is_valid())
        self.assertEqual(
            form.errors.as_data()["mailaccount"][0].message,
            "In der Mailadresse fehlt die richtige Domäne.",
        )

    def test_form_wrong_domain(self):
        image = SimpleUploadedFile(
            name="peter.jpg",
            content=open(image_path, "rb").read(),
            content_type="image/jpeg",
        )

        form = MemberForm(
            data={
                "firstname": "Peter",
                "surname": "Traunmüller",
                "nickname": "Pet",
                "mailaccount": "peterfet.at",
                "role": "A",
            },
            files={"image": image},
        )

        self.assertFalse(form.is_valid())
        self.assertEqual(
            form.errors.as_data()["mailaccount"][0].message,
            "Gib eine gültige E-Mail Adresse an.",
        )

    def test_form_wrong_phone_number(self):
        image = SimpleUploadedFile(
            name="peter.jpg",
            content=open(image_path, "rb").read(),
            content_type="image/jpeg",
        )

        form = MemberForm(
            data={
                "firstname": "Peter",
                "surname": "Traunmüller",
                "nickname": "Pet",
                "mailaccount": "peter@fet.at",
                "role": "A",
                "phone": "+43644+12345678",
            },
            files={"image": image},
        )

        self.assertFalse(form.is_valid())
        self.assertEqual(
            form.errors.as_data()["phone"][0].message,
            "Telefonnummer muss in diesem Format +999999999999 eingegeben werden. "
            "Bis zu 15 Zahlen sind erlaubt.",
        )


class JobGroupFormTestCase(TestCase):
    def test_form(self):
        form = JobGroupForm(
            data={
                "name": "Studienkommission ET",
                "shortterm": "Stuko ET",
            },
        )

        self.assertTrue(form.is_valid())
        form.save()

        job_group = JobGroup.objects.get(name="Studienkommission ET")
        self.assertEqual(job_group.__str__(), "Studienkommission ET")
        self.assertEqual(job_group.slug, "stuko-et")

    def test_form_error_slug(self):
        form = JobGroupForm(
            data={
                "name": "Studienkommission ET",
                "shortterm": "Stuko ET",
            },
        )

        self.assertTrue(form.is_valid())
        form.save()

        job_group = JobGroup.objects.get(name="Studienkommission ET")
        self.assertEqual(job_group.__str__(), "Studienkommission ET")
        self.assertNotEqual(job_group.slug, "stuko et")


class JobFormTestCase(TestCase):
    def test_form(self):
        job_group = JobGroup()
        job_group.name = "Studienkommission ET"
        job_group.shortterm = "Stuko ET"
        job_group.save()

        form = JobForm(
            data={
                "name": "Studienkommission Master",
                "shortterm": "Stuko Master",
                "job_group": job_group,
            },
        )

        self.assertTrue(form.is_valid())
        form.save()

        job = Job.objects.get(name="Studienkommission Master")
        self.assertEqual(job.__str__(), "Studienkommission Master")
        self.assertEqual(job.slug, "stuko-master")
