from django.shortcuts import render
from home.forms import UserForm, UserProfileInfoForm, ReportForm
from django.contrib.auth import authenticate, login, logout
from django.http import HttpResponseRedirect, HttpResponse
from django.urls import reverse
from django.contrib.auth.decorators import login_required
from .models import *

def index(request):
    return render(request, 'home/index.html')


@login_required
def special(request):
    return HttpResponse("You are logged in !")


@login_required
def user_logout(request):
    logout(request)
    return HttpResponseRedirect(reverse('index'))


def register(request):
    registered = False
    if request.method == 'POST':
        user_form = UserForm(data=request.POST)
        profile_form = UserProfileInfoForm(data=request.POST)
        if user_form.is_valid() and profile_form.is_valid():
            user = user_form.save()
            user.set_password(user.password)
            user.save()
            profile = profile_form.save(commit=False)
            profile.user = user
            if 'profile_pic' in request.FILES:
                print('found it')
                profile.profile_pic = request.FILES['profile_pic']
            profile.save()
            registered = True
        else:
            print(user_form.errors, profile_form.errors)
    else:
        user_form = UserForm()
        profile_form = UserProfileInfoForm()
    return render(request, 'home/registration.html',
                           {'user_form': user_form,
                            'profile_form': profile_form,
                            'registered': registered})


def user_login(request):
    if request.method == 'POST':
        username = request.POST.get('username')
        password = request.POST.get('password')
        user = authenticate(username=username, password=password)
        if user:
            if user.is_active:
                login(request, user)
                return HttpResponseRedirect(reverse('index'))
            else:
                return HttpResponse("Your account was inactive.")
        else:
            print("Someone tried to login and failed.")
            print("They used username: {} and password: {}".format(username, password))
            return HttpResponse("Invalid login details given")
    else:
        return render(request, 'home/login.html', {})

@login_required
def user_status(request):
    print(request.user)
    user_instance = UserProfileInfo.objects.get(user=request.user)
    city=user_instance.city
    # alert_instance=Alert.objects.get(city_name=city)
    alert={}
    for item in Alert.objects.filter(city_name__city_name=city):
        alert[item.alert_name] = item.unsafe_peoples
    print(alert)
    return render(request, 'home/user_status.html',
                           {'alert': alert})


@login_required
def report(request):
    if request.method == 'POST':
        report_form = ReportForm(data=request.POST)
        if report_form.is_valid():
            item = report_form.save(commit=False)
            item.save()
        else:
            print(report_form.errors)
    else:
        report_form = ReportForm()
    return render(request, 'home/report.html',
                  {'report_form': report_form})


def disaster(request):
    return render(request, 'home/disaster.html')

@login_required
def payment(request):
    return render(request, 'home/payment.html',
                  {'affected': Alert.objects.all()})