from django.shortcuts import render, get_object_or_404
from django.http import HttpResponseRedirect
from django.urls import reverse
from django.forms import modelformset_factory
#from django.views import generic
from django.contrib.auth.decorators import login_required, permission_required
from website.settings import APPLICATION_NAME
from rotbot.models import Network, Host, Channel, User, Message, Action, Notice, Join, Kick, CurseWord, CurseAdjective
from rotbot.forms import CurseWordForm, CurseAdjectiveForm
from rotbot.views.common import default_keywords, shorten_number, total_messages

def index(request):
    networks = Network.objects.all()
    hosts = Host.objects.all()
    channels = Channel.objects.all()
    users = User.objects.all()
    joins = Join.objects.all()
    kicks = Kick.objects.all()
    messages = Message.objects.all()
    actions = Action.objects.all()
    notices = Notice.objects.all()
    cursewords = CurseWord.objects.all()

    context = {
        'title': 'RotBot',
        'icon': 'hashtag',
        'description': 'Index of RotBot',
        'keywords': default_keywords('index'),
        'total_networks':shorten_number(networks.count()),
        'total_hosts':shorten_number(hosts.count()),
        'total_channels':shorten_number(channels.count()),
        'total_users':shorten_number(users.count()),
        'total_joins':shorten_number(joins.count()),
        'total_kicks':shorten_number(kicks.count()),
        'total_messages':shorten_number(messages.count()),
        'total_actions':shorten_number(actions.count()),
        'total_notices':shorten_number(notices.count()),
        'total_cursewords':shorten_number(cursewords.count()),
    }
    return render(request, 'rotbot/index.html', context)

def user(request, user_slug):
    user = get_object_or_404(User, slug=user_slug)
    network = Network.objects.get(id=user.network.id)
    messages = Message.objects.filter(user=user)
    actions = Action.objects.filter(user=user)
    notices = Notice.objects.filter(user=user)
    joins = Join.objects.filter(user=user)
    kicks = Kick.objects.filter(kicker=user)
    kicked = Kick.objects.filter(kicked=user)

    context = {
        'parent_title': network.name,
        'parent_url': reverse('rotbot:network', args=(network.slug,)),
        'parent_icon': 'sitemap',
        'title': user.name,
        'icon': 'hashtag',
        'description': 'Details of user %s on network %s' % (user.name, network.name),
        'keywords': default_keywords() + '%s, %s' % (network.name, user.name),
        'user': user,
        'network': network,
        'total_messages': shorten_number(total_messages(messages)),
        'total_actions': shorten_number(total_messages(actions)),
        'total_notices': shorten_number(total_messages(notices)),
        'total_joins': shorten_number(joins.count()),
        'total_kicks': shorten_number(kicked.count()),
        'total_kicked': shorten_number(kicked.count()),
    }
    return render(request, 'rotbot/user.html', context)

@login_required
@permission_required('rotbot.add_curseword', raise_exception=True)
def add_curseword(request):
    CurseWordFormSet = modelformset_factory(CurseWord, form=CurseWordForm, extra=4)
    CurseAdjectiveFormSet = modelformset_factory(CurseAdjective, form=CurseAdjectiveForm, extra=4)
    if request.method == 'POST':
        curseword_formset = CurseWordFormSet(request.POST)
        curseadjective_formset = CurseAdjectiveFormSet(request.POST)
        if curseword_formset.is_valid() and curseadjective_formset.is_valid():
            new_cursewords = curseword_formset.save(commit=False)
            new_curseadjectives = curseadjective_formset.save(commit=False)
            for record in new_cursewords:
                record.word = record.word.lower()
                record.save()
            for record in new_curseadjectives:
                record.word = record.word.lower()
                record.save()
            return HttpResponseRedirect(reverse('rotbot:networks'))
        else:
            curseword_formset = CurseWordFormSet(queryset=CurseWord.objects.none())
            curseadjective_formset = CurseWordFormSet(queryset=CurseAdjective.objects.none())
    else:   # Not a POST request.
        curseword_formset = CurseWordFormSet(queryset=CurseWord.objects.none())
        curseadjective_formset = CurseAdjectiveFormSet(queryset=CurseAdjective.objects.none())

    context = {
        'parent_title': 'RotBot',
        'parent_url': reverse('rotbot:networks'),
        'parent_icon': 'sitemap',
        'title': 'Add curseword',
        'icon': 'book dead',
        'description': 'Add a curseword to RotBot\'s vocabulary.',
        'keywords': default_keywords(),
        'curseword_formset': curseword_formset,
        'curseadjective_formset': curseadjective_formset,
    }
    return render(request, 'rotbot/add_curseword.html', context)
