from django.http import HttpResponseRedirect
from django.http import Http404
from django.shortcuts import render

from customer.models import Customer
from domain.models import Domain, DomainForm, EditDomainForm
from revision.models import Revision, RevisionState
from record.models import Record

# Create your views here.
def index(request, customer_id):
    try:
        customer = Customer.objects.get(pk=customer_id)
    except Customer.DoesNotExist:
        raise Http404

    domain_list = Domain.objects.filter(customer=customer).order_by('zone')[:10]
    return render( request, 'domain/index.html', {
        'customer': customer,
        'domain_list': domain_list,
    })

def add(request, customer_id):
    try:
        customer = Customer.objects.get(pk=customer_id)
    except Customer.DoesNotExist:
        raise Http404

    if request.method == 'POST':
        form = DomainForm(customer, request.POST)
        if form.is_valid():
            new_domain = form.save(commit=False)
            new_domain.customer_id = customer.id
            new_domain.save()
            form.save_m2m()

            ns_all = new_domain.nameservers.all()

# create the first revision
            first_revision = Revision(
                domain = new_domain,
                revisionstate = RevisionState.objects.filter(name='edit'),
                name = 'Revision #1'
            )
            first_revision.save()

# create the first record
# create SOA
#            soa = Record(
#                revision = first_revision,
#                host = '@',
#                recordtype = RecordType.objects.filter(label='SOA'),
#                data = models.CharField(max_length=100, null=True, blank=True)
#                ttl = 10800)
#                serial = models.BigIntegerField(null=True, blank=True)
#                refresh = models.PositiveIntegerField(null=True, blank=True)
#                retry = models.PositiveIntegerField(null=True, blank=True)
#                expire = models.PositiveIntegerField(null=True, blank=True)
#                minimum = models.PositiveIntegerField(null=True, blank=True)
#                resp_person = new_domain.adminc_id,
#                primary_ns = ns_all[0]
#            )
#            soa.save()

# create NS
#            for ns in ns_all
#                ns = Record(
#                    revision = first_revision,
#                    host = '@',
#                    recordtype = RecordType.objects.filter(label='NS'),
#                    data = ns.hostname,
#                    ttl = default=10800,
#                )
#                ns.save()

            return HttpResponseRedirect('/domain/%d' % customer.id)
    else:
        form = DomainForm(customer)

    return render( request, 'domain/add.html', {
        'customer': customer,
        'form': form,
    })

def detail(request, customer_id, domain_id, revision_id=False):
    try:
        customer = Customer.objects.get(pk=customer_id)
    except Customer.DoesNotExist:
        raise Http404

    try:
        domain = Domain.objects.get(pk=domain_id, customer=customer)
    except Domain.DoesNotExist:
        raise Http404

    try:
        revision_list = Revision.objects.filter(domain=domain).order_by('name')
    except Revision.DoesNotExist:
        raise Http404

    try:
        if ( revision_id == False ):
            selected_revision = Revision.objects.get(domain=domain, is_activated=True)
        else:
            selected_revision = Revision.objects.get(pk=revision_id, domain=domain)
        
    except Revision.DoesNotExist:
        raise Http404

    try:
        record_list = Record.objects.filter(revision=selected_revision)
    except Revision.DoesNotExist:
        raise Http404

    return render( request, 'domain/detail.html', {
        'customer': customer,
        'domain': domain,
        'revision_list': revision_list,
        'selected_revision': selected_revision,
        'record_list': record_list,
    })

def edit(request, customer_id, domain_id):
    try:
        customer = Customer.objects.get(pk=customer_id)
    except Customer.DoesNotExist:
        raise Http404

    try:
        domain = Domain.objects.get(pk=domain_id)
    except Domain.DoesNotExist:
        raise Http404

    if request.method == 'POST':
        form = EditDomainForm(customer, request.POST, instance=domain)
        if form.is_valid():
            new_domain = form.save(commit=False)
            new_domain.customer_id = customer.id
            new_domain.save()
            form.save_m2m()
            return HttpResponseRedirect('/domain/%d' % customer.id)
    else:
        form = EditDomainForm(customer, instance=domain)

    return render( request, 'domain/edit.html', {
        'customer': customer,
        'domain': domain,
        'form': form,
    })

def delete(request, customer_id, domain_id):
    try:
        customer = Customer.objects.get(pk=customer_id)
    except Customer.DoesNotExist:
        raise Http404

    try:
        domain = Domain.objects.get(pk=domain_id)
    except User.DoesNotExist:
        raise Http404

    domain.delete()
    return HttpResponseRedirect('/domain/%d' % customer.id)
