from django.contrib.auth.mixins import LoginRequiredMixin
from django.db import transaction as db_transaction
from django.http import Http404
from django.http.response import HttpResponseRedirect
from django.shortcuts import get_object_or_404
from django.urls import reverse_lazy, reverse
from django.views import View
from django.views.generic import CreateView, ListView, DeleteView

from hordak.forms import SimpleTransactionForm, TransactionForm, LegFormSet
from hordak.forms.transactions import CurrencyTradeForm
from hordak.models import StatementLine, Leg, Transaction


class TransactionCreateView(LoginRequiredMixin, CreateView):
    """ View for creation of simple transactions.

    This functionality is provided by :class:`hordak.models.Account.transfer_to()`,
    see the method's documentation for additional details.

    Examples:

        .. code-block:: python

            urlpatterns = [
                ...
                url(r'^transactions/create/$', TransactionCreateView.as_view(), name='transactions_create'),
            ]
    """

    form_class = SimpleTransactionForm
    success_url = reverse_lazy("hordak:accounts_list")
    template_name = "hordak/transactions/transaction_create.html"


class CurrencyTradeView(LoginRequiredMixin, CreateView):
    form_class = CurrencyTradeForm
    success_url = reverse_lazy("hordak:accounts_list")
    template_name = "hordak/transactions/currency_trade.html"

    def get_form_kwargs(self):
        kwargs = super(CurrencyTradeView, self).get_form_kwargs()
        kwargs.pop("instance")
        return kwargs


class TransactionsListView(LoginRequiredMixin, ListView):
    """View for listing transactions

    """

    model = Transaction
    template_name = "hordak/transactions/transaction_list.html"
    context_object_name = "transactions"
    ordering = ["-date", "-pk"]


class LegsListView(LoginRequiredMixin, ListView):
    """View for listing legs

    """

    model = Leg
    template_name = "hordak/transactions/leg_list.html"
    context_object_name = "legs"
    ordering = ["-transaction__date", "-transaction__pk", "-pk"]


class TransactionDeleteView(LoginRequiredMixin, DeleteView):
    model = Transaction
    slug_url_kwarg = "uuid"
    slug_field = "uuid"
    template_name = "hordak/transactions/transaction_delete.html"
    context_object_name = "transaction"
    success_url = reverse_lazy("hordak:accounts_list")


class TransactionsReconcileView(LoginRequiredMixin, ListView):
    """ Handle rendering and processing in the reconciliation view

    Note that this only extends ListView, and we implement the form
    processing functionality manually.

    Examples:

        .. code-block:: python

            urlpatterns = [
                ...
                url(r'^transactions/reconcile/$', TransactionsReconcileView.as_view(), name='transactions_reconcile'),
            ]
    """

    template_name = "hordak/transactions/reconcile.html"
    model = StatementLine
    paginate_by = 50
    context_object_name = "statement_lines"
    ordering = ["-date", "-pk"]
    success_url = reverse_lazy("hordak:accounts_list")

    def get_uuid(self):
        return self.request.POST.get("reconcile") or self.request.GET.get("reconcile")

    def get_object(self, queryset=None):
        # Get any Statement Line instance that was specified
        if queryset is None:
            queryset = self.get_queryset()

        uuid = self.get_uuid()