import datetime
from django.shortcuts import render, get_object_or_404
from django.http import HttpResponseRedirect
from django.urls import reverse
from django.contrib.auth.decorators import login_required, permission_required
from rotbot.models import Network, Channel, User, Message, Action, Notice, TempChannelKey, Join, Kick
from rotbot.forms import ChannelForm
from .common import default_keywords, shorten_number, total_messages

def channel(request, channel_slug):
    channel = get_object_or_404(Channel, slug=channel_slug)
    network = Network.objects.get(id=channel.network.id)
    messages = Message.objects.filter(channel=channel)
    actions = Action.objects.filter(channel=channel)
    notices = Notice.objects.filter(channel=channel)
    joins = Join.objects.filter(channel=channel)
    kicks = Kick.objects.filter(channel=channel)

    context = {
        'parent_title': network.name,
        'parent_url': reverse('rotbot:network', args=(network.slug,)),
        'parent_icon': 'sitemap',
        'title': channel.name,
        'icon': 'hashtag',
        'description': 'Details of channel %s on network %s' % (channel.name, network.name),
        'keywords': default_keywords() + '%s, %s' % (network.name, channel.name),
        'channel': channel,
        '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(kicks.count()),
    }
    return render(request, 'rotbot/channel.html', context)

@login_required
@permission_required('rotbot.change_channel', raise_exception=True)
def channel_settings(request, channel_slug):
    channel = get_object_or_404(Channel, slug=channel_slug)
    network = Network.objects.get(id=channel.network_id)

    if request.method == 'POST':
        form = ChannelForm(request.POST, instance=channel)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect(reverse('rotbot:channel', args=(channel.slug,)))

    else:
        form = ChannelForm(instance=channel)

    context = {
        'parent_title': network.name,
        'parent_url': reverse('rotbot:network', args=(network.slug,)),
        'parent_icon': 'sitemap',
        'title': channel.name,
        'icon': 'hashtag',
        'description': 'Change settings for channel %s on network %s' % (channel.name, network.name),
        'keywords': default_keywords() + 'settings, %s, %s' % (network.name, channel.name),
        'channel_slug': channel.slug,
        'network_slug': network.slug,
        'form': form,
    }
    return render(request, 'rotbot/channel_settings.html', context)

def ircauth_channel_settings(request, temp_key):
    key = get_object_or_404(TempChannelKey, key=temp_key)
    channel = Channel.objects.get(id=key.channel_id)
    network = Network.objects.get(id=key.network_id)
    if key.created > datetime.datetime.now() - datetime.timedelta(minute=10):
        key_expired = False
    else:
        key_expired = True

    if request.method == 'POST':
        if recent_key:
            form = ChannelForm(request.POST, instance=channel)
            if form.is_valid():
                form.save()
                key.delete()
                return HttpResponseRedirect(reverse('rotbot:channel', args=(channel.slug,)))

    else:
        form = ChannelForm(instance=channel)
    context = {
        'parent_title': network.name,
        'parent_url': reverse('rotbot:network', args=(network.slug,)),
        'parent_icon': 'sitemap',
        'title': channel.name,
        'icon': 'hashtag',
        'description': 'Change settings for channel %s on network %s' % (channel.name, network.name),
        'keywords': default_keywords() + 'settings, %s, %s' % (network.name, channel.name),
        'channel_slug': channel.slug,
        'network_slug': network.slug,
        'form': form,
        'temp_key': temp_key,
        'key_expired': key_expired,
    }
    return render(request, 'rotbot/channel_settings.html', context)
