# -*- encoding: utf-8 -*-
"""
License: MIT
Copyright (c) 2019 - present AppSeed.us
"""

from django.shortcuts import render

# Create your views here.
from django.shortcuts import render, redirect
from django.contrib.auth import authenticate, login
from django.contrib.auth.models import User
from django.forms.utils import ErrorList
from django.http import HttpResponse
from .forms import LoginForm, SignUpForm , ComplainForm
from app.models import complain,infrastructure

def login_view(request):
    form = LoginForm(request.POST or None)
    msg = None
    msgproblem = None
    complainform = ComplainForm(request.POST or None)

    if request.method == "POST":
        if 'complaining' in request.POST:
            if complainform.is_valid():
                afm = complainform.cleaned_data.get("afm")
                inf_id = complainform.cleaned_data.get("infrastructure")
                notes = complainform.cleaned_data.get("notes")
                try:
                    complain.objects.create(made_afm = afm, infrastructure_id = infrastructure.objects.get(UUID = inf_id ), notes = notes)
                    msgproblem = 'Yor Complain have been send'
                except Exception as e:
                    msgproblem =  e
            else:
                msgproblem = 'Yor Complain have NOT been send,check the info you have provide'
        else:
            if form.is_valid():
                username = form.cleaned_data.get("username")
                password = form.cleaned_data.get("password")
                user = authenticate(username=username, password=password)
                if user is not None:
                    login(request, user)
                    return redirect("/")
                else:
                    msg = 'Invalid credentials'
            else:
                msg = 'Error validating the form'

    return render(request, "accounts/login.html", {"complainform": complainform, "form": form, "msg" : msg,"msgproblem" : msgproblem})

def register_user(request):

    msg     = None
    success = False

    if request.method == "POST":
        form = SignUpForm(request.POST)
        if form.is_valid():
            form.save()
            username = form.cleaned_data.get("username")
            raw_password = form.cleaned_data.get("password1")
            user = authenticate(username=username, password=raw_password)

            msg     = 'User created.'
            success = True

            #return redirect("/login/")

        else:
            msg = 'Form is not valid'
    else:
        form = SignUpForm()

    return render(request, "accounts/register.html", {"form": form, "msg" : msg, "success" : success })
