from django.contrib.auth.decorators import login_required
from django.contrib.sites.shortcuts import get_current_site
from django.template.loader import render_to_string
from django.utils.http import urlsafe_base64_encode
from django.utils.http import urlsafe_base64_decode
from django.utils.encoding import force_bytes, force_text
from django.shortcuts import redirect, render, get_object_or_404
from django.template import RequestContext
from django.views.generic.list import ListView
from django.views.generic.detail import DetailView
from django.contrib.auth import login, authenticate
from django.contrib.auth.models import User
from django.core.mail import send_mail

from .models import Post, Entry
from .forms import PostForm, RegisterForm
from .tokens import account_activation_token

def test_mail(request):
    send_mail(
        'Subject here',
        'Here is the message.',
        'hi@arlefreak.com',
        ['arlefreak@gmail.com'],
        fail_silently=False,
    )
    return redirect('home')


def account_activation_sent(request):
    return redirect('home')

def activate(request, uidb64, token):
    try:
        uid = force_text(urlsafe_base64_decode(uidb64))
        user = User.objects.get(pk=uid)
    except (TypeError, ValueError, OverflowError, User.DoesNotExist):
        user = None

    if user is not None and account_activation_token.check_token(user, token):
        user.is_active = True
        user.profile.email_confirmed = True
        user.save()
        login(request, user)
        return redirect('home')
    else:
        return render(request, 'account_activation_invalid.html')

def Register(request):
    if request.method == 'POST':
        form = RegisterForm(request.POST)
        if form.is_valid():
            user = form.save(commit=False)
            user.is_active = False
            user.save()
            current_site = get_current_site(request)
            subject = 'Activate Your MySite Account'
            message = render_to_string('registration/account_activation_email.html', {
                'user': user,
                'domain': current_site.domain,
                'uid': urlsafe_base64_encode(force_bytes(user.pk)),
                'token': account_activation_token.make_token(user),
            })
            user.email_user(subject, message)
            return redirect('account_activation_sent')
    else:
        form = RegisterForm()
    return render(request, 'registration/register.html', {'form': form})

class EntryListView(ListView):
    model = Entry
    template_name = 'writing/entry_list.html'

    def get_context_data(self, **kwargs):
        context = super(EntryListView, self).get_context_data(**kwargs)
        return context

class EntryDetailView(DetailView):
    model = Entry
    template_name = 'writing/entry_single.html'

    def get_context_data(self, **kwargs):
        context = super(EntryDetailView, self).get_context_data(**kwargs)
        context['object_list'] = Post.objects.filter(entry=self.object)
        return context

@login_required
def add_post(request):
    form = PostForm(request.POST or None)
    if form.is_valid():
        post = form.save(commit=False)
        post.author = request.user
        post.save()
        return redirect(post)
    return render(request, 'writing/add_post.html', 
                              { 'form': form })

def view_post(request, slug):
    post = get_object_or_404(Post, slug=slug)
    return render(request, 'writing/post_single.html',
                              {
                                  'post': post,
                              })
