from django.contrib.auth.hashers import make_password
from django.contrib.auth.models import Group

from vbv_lernwelt.core.models import User


def create_default_users(user_model=User, group_model=Group, default_password=None):
    if default_password is None:
        default_password = 'test'

    admin_group, created = group_model.objects.get_or_create(name='admin_group')
    content_creator_grop, created = group_model.objects.get_or_create(name='content_creator_grop')
    student_group, created = group_model.objects.get_or_create(name='student_group')

    def _create_student_user(email, first_name, last_name, avatar_url=''):
        student_user, created = _get_or_create_user(
            user_model=user_model, username=email, password=default_password,
        )
        student_user.first_name = first_name
        student_user.last_name = last_name
        student_user.avatar_url = avatar_url
        student_user.groups.add(student_group)
        student_user.save()

    def _create_admin_user(email, first_name, last_name, avatar_url=''):
        admin_user, created = _get_or_create_user(
            user_model=user_model, username=email, password=default_password
        )
        admin_user.is_superuser = True
        admin_user.is_staff = True
        admin_user.first_name = first_name
        admin_user.last_name = last_name
        admin_user.avatar_url = avatar_url
        admin_user.groups.add(admin_group)
        admin_user.save()

    _create_admin_user(
        email='info@iterativ.ch',
        first_name='Info',
        last_name='Iterativ',
        avatar_url='/static/avatars/avatar_iterativ.png'
    )

    _create_admin_user(
        email='admin',
        first_name='Peter',
        last_name='Adminson',
        avatar_url='/static/avatars/avatar_iterativ.png'
    )

    _create_student_user(
        email='student',
        first_name='Student',
        last_name='Meier',
        avatar_url='/static/avatars/avatar_iterativ.png'
    )

    _create_student_user(
        email='daniel.egger@iterativ.ch',
        first_name='Daniel',
        last_name='Egger',
        avatar_url='/static/avatars/avatar_iterativ.png'
    )

    _create_student_user(
        email='axel.manderbach@lernetz.ch',
        first_name='Axel',
        last_name='Manderbach',
        avatar_url='/static/avatars/avatar_axel.jpg'
    )

    _create_student_user(
        email='christoph.bosshard@vbv-afa.ch',
        first_name='Christoph',
        last_name='Bosshard',
        avatar_url='/static/avatars/avatar_christoph.jpg'
    )


def _get_or_create_user(user_model, *args, **kwargs):
    username = kwargs.get('username', None)
    password = kwargs.get('password', None)
    created = False

    user = user_model.objects.filter(username=username).first()

    if not user:
        user = user_model.objects.create(
            username=username,
            password=make_password(password),
            email=username,
        )
        created = True
    return user, created
