"""
Author: Michel Peltriaux
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
Contact: ksp-servicestelle@sgdnord.rlp.de
Created on: 01.09.23

"""
import json

from django.urls import reverse
from django.utils.translation import gettext_lazy as _

from ema.forms import NewEmaForm, EditEmaForm
from konova.forms import SimpleGeomForm
from konova.tests.test_views import BaseTestCase
from konova.utils.generators import generate_random_string
from user.models import UserAction


class NewEmaFormTestCase(BaseTestCase):

    def setUp(self) -> None:
        super().setUp()

    def test_init(self):
        form = NewEmaForm()
        self.assertEqual(form.form_title, str(_("New EMA")))
        self.assertEqual(form.action_url, reverse("ema:new"))
        self.assertEqual(form.cancel_redirect, reverse("ema:index"))
        self.assertIsNotNone(form.fields["identifier"].initial)
        self.assertEqual(form.fields["title"].widget.attrs["placeholder"], str(_("Compensation XY; Location ABC")))

    def test_save(self):
        cons_office_code = self.get_conservation_office_code()
        data = {
            "identifier": generate_random_string(length=20, use_numbers=True),
            "title": generate_random_string(length=20, use_letters_lc=True),
            "conservation_office": cons_office_code,
            "conservation_file_number": generate_random_string(length=10, use_numbers=True),
            "is_pik": True,
            "comment": generate_random_string(length=20, use_numbers=True, use_letters_uc=True),
        }
        form = NewEmaForm(data)

        test_geom = self.create_dummy_geometry()
        geom_form_data = self.create_geojson(
            test_geom
        )
        geom_form_data = json.loads(geom_form_data)
        geom_form_data = {
            "output": json.dumps(geom_form_data)
        }

        geom_form = SimpleGeomForm(geom_form_data)
        self.assertTrue(form.is_valid(), msg=form.errors)
        self.assertTrue(geom_form.is_valid(), msg=form.errors)

        obj = form.save(user=self.superuser, geom_form=geom_form)
        self.assertEqual(obj.title, data["title"])
        self.assertEqual(obj.is_pik, data["is_pik"])
        self.assertIsNotNone(obj.responsible)
        self.assertIsNotNone(obj.responsible.handler)
        self.assertEqual(obj.responsible.conservation_office, data["conservation_office"])
        self.assertEqual(obj.responsible.conservation_file_number, data["conservation_file_number"])
        self.assertEqual(obj.identifier, data["identifier"])
        self.assertEqual(obj.comment, data["comment"])

        self.assertIn(self.superuser, obj.shared_users)

        last_log = obj.log.first()
        self.assertEqual(obj.created, obj.modified)
        self.assertEqual(obj.created, last_log)
        self.assertEqual(last_log.action, UserAction.CREATED)
        self.assertEqual(last_log.user, self.superuser)
        self.assertTrue(test_geom.equals_exact(obj.geometry.geom, 0.000001))


class EditEmaFormTestCase(BaseTestCase):
    def test_init(self):
        form = EditEmaForm(instance=self.ema)
        self.assertEqual(form.form_title, str(_("Edit EMA")))
        self.assertEqual(form.action_url, reverse("ema:edit", args=(self.ema.id,)))
        self.assertEqual(form.cancel_redirect, reverse("ema:detail", args=(self.ema.id,)))
        self.assertEqual(form.fields["identifier"].widget.attrs["url"], reverse("ema:new-id"))
        self.assertEqual(form.fields["title"].widget.attrs["placeholder"], str(_("Compensation XY; Location ABC")))

        values = {
            "identifier": self.ema.identifier,
            "title": self.ema.title,
            "comment": self.ema.comment,
            "conservation_office": self.ema.responsible.conservation_office,
            "conservation_file_number": self.ema.responsible.conservation_file_number,
            "is_pik": self.ema.is_pik,
            "handler_type": self.ema.responsible.handler.type,
            "handler_detail": self.ema.responsible.handler.detail,
        }
        for k, v in values.items():
            self.assertEqual(form.fields[k].initial, v)

    def test_save(self):
        cons_office_code = self.get_conservation_office_code()
        data = {
            "identifier": generate_random_string(length=20, use_numbers=True),
            "title": generate_random_string(length=20, use_letters_lc=True),
            "conservation_office": cons_office_code,
            "conservation_file_number": generate_random_string(length=10, use_numbers=True),
            "is_pik": not self.ema.is_pik,
            "comment": generate_random_string(length=20, use_numbers=True, use_letters_uc=True),
        }
        form = EditEmaForm(data, instance=self.ema)
        self.assertTrue(form.is_valid(), msg=form.errors)

        test_geom = self.create_dummy_geometry()
        geom_form_data = self.create_geojson(
            test_geom
        )
        geom_form_data = json.loads(geom_form_data)
        geom_form_data = {
            "output": json.dumps(geom_form_data)
        }

        geom_form = SimpleGeomForm(geom_form_data)
        self.assertTrue(geom_form.is_valid())

        obj = form.save(self.superuser, geom_form)
        self.assertEqual(obj.id, self.ema.id)
        self.assertEqual(obj.title, data["title"])
        self.assertEqual(obj.is_pik, data["is_pik"])
        self.assertIsNotNone(obj.responsible)
        self.assertIsNotNone(obj.responsible.handler)
        self.assertEqual(obj.responsible.conservation_office, data["conservation_office"])
        self.assertEqual(obj.responsible.conservation_file_number, data["conservation_file_number"])
        self.assertNotEqual(obj.identifier, data["identifier"], msg="Identifier editable via form!")
        self.assertEqual(obj.comment, data["comment"])

        last_log = obj.log.first()
        self.assertEqual(obj.modified, last_log)
        self.assertEqual(last_log.action, UserAction.EDITED)
        self.assertEqual(last_log.user, self.superuser)
        self.assertTrue(test_geom.equals_exact(obj.geometry.geom, 0.000001))

