from django.apps import AppConfig
from constance.apps import ConstanceConfig
from post_office.apps import PostOfficeConfig
from django.contrib.auth.apps import AuthConfig

from django.db import connection
from django.db.models.signals import post_migrate

from django.template.loader import render_to_string, get_template
from django.utils.html import strip_tags


class ConstanceBase(ConstanceConfig):
    verbose_name = "Configuration"


def create_admin(sender=None, **kwargs):

    from django.contrib.auth.models import User
    from django.contrib.auth.models import Group

    if ( User.objects.filter(username='admin').exists()  ):
        pass
    else:
        user = User.objects.create_superuser('admin', email='admin@server.com', password='admin_pass')
        user.save()

    Group.objects.update_or_create(name='submission')
    Group.objects.update_or_create(name='jury')


class project_base(AppConfig):
    name = 'project_base'
    def ready(self):
        # print("base ready")
        super().ready()
        post_migrate.connect(create_admin, sender=self)


class AuthConfigBase(AuthConfig):

    def ready(self):
        # print("base ready")
        super().ready()
        post_migrate.connect(create_admin, sender=self)

content_html = """
<table>
<tbody>
<tr>
<td>
<h2><a href="{{product.current_uri}}" target="_blank" rel="noopener"><span style="text-decoration: none;"><span style="color: #000000; text-decoration: underline;">{{product.current_uri}}</span></span></a></h2>
</td>
</tr>
<tr>
<td>
<h3>{{ product.name }}</h3>
    <p>Freie Plätze!</p>
{% if product.beschreibung %}
<h3>Beschreibung</h3>
    <p>{{ product.beschreibung }}</p>
{% endif %} {% if product.claim %}
<h3>Claim</h3>
    <p>{{ product.claim }}</p>
{% endif %} {% if product.learning %}
<h3>Learning</h3>
    <p>{{ product.learning }}</p>
{% endif %} {% if product.status %}
<h3>Status</h3>
    <p>{{ product.status }}</p>
{% endif %} {% if product.adresse %}
<h3>Adresse</h3>
    <p>{{product.adresse}}</p>
{% endif %} {% if product.adresse_zusatz %}
    <p>{{ product.adresse_zusatz }}</p>
{% endif %} {% if product.plz %}
<h3>plz</h3>
    <p>{{ product.plz }}</p>
{% endif %} {% if product.get_ort_display %}
<h3>ort</h3>
    <p>{{ product.ort }}</p>
{% endif %}

{% if product.website %}
<h3>website</h3>
    <p><a href="{{product.website}}" target="_blank" rel="noopener">{{product.website}}</a></p>
{% endif %}
{% if product.email %}
<h3>email</h3>
    <p><a href="mailto:{{product.email}}">{{product.email}}</a></p>
{% endif %}
  </td>
</tr>
<tr>
<td><a href="{{ product.current_uri }}">{{ product.current_uri }}</a>
<p><a href="{{ agent.delete_url }}">Agent Löschen</a>: {{ agent.delete_url }}</p>
</td>
</tr>
</tbody>
</table>
"""

content_txt = strip_tags(content_html)

def create_emails(sender=None, **kwargs):
    from post_office.models import EmailTemplate
    EmailTemplate.objects.update_or_create(
        name='generic',
        defaults={
        'subject'      :  'Neue Nachricht von gemeinschaffen.com',
        'description'  :  'Generic template',
        'html_content' :  content_html,
        'content'      :  content_txt}
        )
    pass


class PostOfficeBase(PostOfficeConfig):

    def ready(self):
        super().ready()
        post_migrate.connect(create_emails, sender=self)
