from django.shortcuts import render
from django.http import HttpResponse
from .forms import ip_random, new_ip_random, ip
from ularegistry.models import ips, ips2
from django.shortcuts import redirect
from ularegistry.checkip import *
from ularegistry.randomcreate import *
from django.shortcuts import render, redirect
from django.contrib.auth.forms import UserCreationForm, AuthenticationForm
from django.contrib.auth import login as auth_login
from django.contrib.auth import logout as auth_logout

# Create your views here.
def index(request):
    ips_list = ips.objects.values()
    if request.method == "POST":
        form = ip(request.POST)
        print(form)
        if form.is_valid():
            ips_list2 = list(ips_list)
            if not ips_list2:
                print("test")
                q = ips(inet=form.cleaned_data['inputIP'])
                q.save()
                
            overlapchk = 0    
            for value in ips_list2:
                if overlapip(form.cleaned_data['inputIP'],value['inet']):
                    overlapchk = 1
                else:
                    overlapchk = 0
                    break
                
            if overlapchk:
                print(overlapchk)
                q = ips(inet=form.cleaned_data['inputIP'])
                q.save()

            return redirect('index')

    else:
        form = ip()
        
    ctx = { 'form' : form,
            'ips_list' : ips_list,
            }
    return render(request, 'index.html', ctx)
    #return HttpResponse("Hello, world.")

def randomIP(request):
    ips_listr = ips2.objects.values()
    if request.method == "POST":
        form = ip_random(request.POST)
        print(form)
        if form.is_valid():
            while True:
                newIP = createIP(form.cleaned_data['inputNetwork'])
                ips_listr2 = list(ips_listr)
                if not ips_listr2:
                    print("test")
                    q = ips2(inet2=newIP, inet2_name=form.cleaned_data['NetworkName'], inet2_descrip=form.cleaned_data['NetworkDescription'])
                    q.save()
                    break
                
                overlapchk = 0
                                
                for value in ips_listr2:
                    if overlapip(newIP,value['inet2']):
                        overlapchk = 1
                        print(overlapchk)
                        q = ips2(inet2=newIP, inet2_name=form.cleaned_data['NetworkName'], inet2_descrip=form.cleaned_data['NetworkDescription'])
                        q.save()
                        break
                    else:
                        overlapchk = 0
                        
                if overlapchk:
                    break
            
            return redirect('randomIP')

    else:
        form = ip_random()

    ctx = { 'form' : form,
            'ips_listr' : ips_listr,
            }
    return render(request, 'randomIP.html', ctx)


def newrandomIP(request):
    ips_listr = ips2.objects.values()
    if request.method == "POST":
        form2 = new_ip_random(request.POST)
        print(form2)
        if form2.is_valid():
            while True:
                newIP = createIP("fd00::/8")
                print("test3")
                ips_listr2 = list(ips_listr)
                if not ips_listr2:
                    print("test2")
                    q = ips2(inet2=newIP, inet2_name=form2.cleaned_data['NetworkName'], inet2_descrip=form2.cleaned_data['NetworkDescription'])
                    q.save()
                    break

                overlapchk = 0

                for value in ips_listr2:
                    if overlapip(newIP,value['inet2']):
                        overlapchk = 1
                        print(overlapchk)
                        q = ips2(inet2=newIP, inet2_name=form2.cleaned_data['NetworkName'], inet2_descrip=form2.cleaned_data['NetworkDescription'])
                        q.save()
                        break
                    else:
                        overlapchk = 0

                if overlapchk:
                    break

            return redirect('newrandomIP')

    else:
        form2 = new_ip_random()

    ctx2 = { 'form2' : form2,
            'ips_listr' : ips_listr,
            }
    return render(request, 'newrandomIP.html', ctx2)


def signup(request):
    if request.user.is_authenticated:
        return redirect('newrandomIP')
    
    if request.method == 'POST':
        signup_form = UserCreationForm(request.POST)
        if signup_form.is_valid():
            user = signup_form.save()
            auth_login(request, user)
            return redirect('login')
        else:
            print("fail singup")
            
    else:
        signup_form = UserCreationForm()
    
    return render(request, 'signup.html', {'signup_form':signup_form})


def login(request):
    if request.user.is_authenticated:
        return redirect('newrandomIP')
    
    if request.method == 'POST':
        login_form = AuthenticationForm(request, request.POST)
        if login_form.is_valid():
            auth_login(request, login_form.get_user())
            return redirect('newrandomIP')
        else:
            print("fail login")
    
    else:
        login_form = AuthenticationForm()
    
    return render(request, 'login.html', {'login_form' : login_form})


def logout(request):
    auth_logout(request)
    return redirect('login')
