# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.shortcuts import render
from django.http import HttpResponse, JsonResponse, HttpResponseRedirect
from django.urls import reverse
from django.template.loader import render_to_string
from django.shortcuts import get_object_or_404
from django.core.paginator import PageNotAnInteger, EmptyPage

from .models import Book
from .forms import BookForm, BookCSVForm
from . import tables
from extras.admin import CustomFieldForm
from utilities.views import BulkImportView
from utilities.paginator import EnhancedPaginator

def book_list(request):
    book_list = Book.objects.all()
    paginator = EnhancedPaginator(book_list, 10)

    page = request.GET.get('page')
    try:
        books = paginator.page(page)
    except PageNotAnInteger:
        books = paginator.page(1)
    except EmptyPage:
        books = paginator.page(paginator.num_pages)

    return render(request, 'books/book_list.html', {'books': books, 'paginator': paginator})

def book_export(request):
    headers = Book.csv_headers
    output = ','.join(headers) + '\n'
    books = Book.objects.all()
    output += '\n'.join([book.to_csv() for book in books])
    response = HttpResponse(
        output,
        content_type='text/csv'
    )
    response['Content-Disposition'] = 'attachment; filename="books.csv"'
    return response

def save_book_form(request, form, template_name):
    data = dict()
    if request.method == "POST":
        if form.is_valid():
            form.save()
            data['form_is_valid'] = True
            books = Book.objects.all()
            data['html_book_list'] = render_to_string('books/includes/partial_book_list.html', {
                'books': books
            })
        else:
            data['form_is_valid'] = False
    context = {'form': form}
    data['html_form'] = render_to_string(template_name, context, request=request)
    return JsonResponse(data)

def book_create(request):
    if request.method == 'POST':
        form = BookForm(request.POST)
    else:
        form = BookForm()
    return save_book_form(request, form, 'books/includes/partial_book_create.html')

def book_detail(request, pk):
    book = get_object_or_404(Book, pk=pk)
    return render(request, 'books/book_detail.html', {"book": book})

def book_update(request, pk):
    book = get_object_or_404(Book, pk=pk)
    if request.method == 'POST':
        form = BookForm(request.POST, instance=book)
    else:
        form = BookForm(instance=book)
    return save_book_form(request, form, 'books/includes/partial_book_update.html')

def book_delete(request, pk):
    book = get_object_or_404(Book, pk=pk)
    data = dict()
    if request.method == 'POST':
        book.delete()
        data['form_is_valid'] = True  # This is just to play along with the existing code
        books = Book.objects.all()
        data['html_book_list'] = render_to_string('books/includes/partial_book_list.html', {
            'books': books
        })
    else:
        context = {'book': book}
        data['html_form'] = render_to_string('books/includes/partial_book_delete.html',
            context,
            request=request
        )
    return JsonResponse(data)

def add_custom_field(request):
    form = CustomFieldForm(request.POST or None)
    if request.method == 'POST':
        if form.is_valid():
            form.save()
            return HttpResponseRedirect(reverse('book_list'))
    return render(request, 'books/add_custom_field.html', {'form': form})

class BookImportView(BulkImportView):
    model_form = BookCSVForm
    default_return_url = 'book_list'
    table = tables.BookTable
