from django.shortcuts import render, get_list_or_404, get_object_or_404
from django.conf import settings
from .models import Networks, Channels, Users
from webgui.common import convert_units
db = settings.BOT_DB

def networks(request):
    networks = get_list_or_404(Networks.objects.all())
    channels = Channels.objects.all()
    users = Users.objects.all()
    messages = db.all("SELECT channel_network, messages, actions, notices FROM messages")
    
    totalnetworks = convert_units.shorten_number(len(networks))
    totalchannels = convert_units.shorten_number(len(channels))
    totalusers = convert_units.shorten_number(len(users))
    networkslist = {}
    for network in networks:
        networkslist[network.name] = {'channelcount': 0, 'usercount': 0, 'messagecount': 0}
    for user in users:
        networkslist[user.network.name]['usercount'] += 1
    for channel in channels:
        networkslist[channel.network.name]['channelcount'] += 1
    totalmessages = 0
    for record in messages:
        totalmessages += record[1] + record[2] + record[3]
        networkslist[record[0]]['messagecount'] += record[1] + record[2] + record[3]
    totalmessages = convert_units.shorten_number(totalmessages)
    
    context = {
        'networks': networks,
        'channels': channels,
        'users': users,
        'messages': messages,
        'totalnetworks': totalnetworks, 
        'totalchannels': totalchannels, 
        'totalusers': totalusers,
        'totalmessages': totalmessages,
        'networkslist': networkslist,
    }
    return render(request, 'stats/networks.html', context)

def network(request, network_name):
    network = get_object_or_404(Networks, name=network_name)
    channels = Channels.objects.filter(network=network_name)
    users = Users.objects.filter(network=network_name)
    messages = db.all("SELECT channel, \"user\", messages, actions, notices FROM messages WHERE channel_network='" + network_name + "'")
    joins = db.all("SELECT channel, \"user\", joins FROM joins WHERE channel_network='" + network_name + "'")
    kicks = db.all("SELECT channel, \"user\", given, received FROM kicks WHERE channel_network='" + network_name + "'")
    
    totalmessages = 0
    totalactions = 0
    totalnotices = 0
    for message in messages:
        totalmessages += message[2]
        totalactions += message[3]
        totalnotices += message[4]
    channelslist = {}
    for channel in channels:
        channelslist[channel.name] = {'joincount': 0, 'messagecount': 0, 'kickcount': 0}
    userslist = {}
    coinspent = 0
    coingiven = 0
    for user in users:
        userslist[user.name] = {'joincount': 0, 'messagecount': 0, 'kgivencount': 0, 'kreceivedcount': 0, 'xpspent': user.xp_spent, 'coin': user.coin}
        coinspent = user.coin_spent - user.coin_given
        coingiven = user.coin_given
    for record in joins:
        channelslist[record[0]]['joincount'] += 1
        userslist[record[1]]['joincount'] += 1
    for record in  messages:
        channelslist[record[0]]['messagecount'] += record[2] + record[3] + record[4]
        userslist[record[1]]['messagecount'] += record[2] + record[3] + record[4]
    for record in  kicks:
        channelslist[record[0]]['kickcount'] += record[2]
        userslist[record[1]]['kgivencount'] += record[2]
        userslist[record[1]]['kreceivedcount'] += record[3]
    
    print(channelslist)
    context = {
        'network': network,
        'channels': channels,
        'users': users,
        'totalmessages': totalmessages,
        'totalactions': totalactions,
        'totalnotices': totalnotices,
        'channelslist': channelslist,
        'userslist': userslist,
        'coinspent': coinspent, 
        'coingiven': coingiven, 
    }
    return render(request, 'stats/network.html', context)
