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, 'xpspentcount': 0, 'coinspentcount': 0, 'coingivencount': 0}
        networkslist[network.name]['host'] = network.host
        networkslist[network.name]['port'] = network.port
        networkslist[network.name]['usessl'] = network.use_ssl
        if network.password:
            networkslist[network.name]['password'] = True
    isxpspent = False
    iscoinspent = False
    for user in users:
        networkslist[user.network.name]['usercount'] += 1
        if user.xp_spent > 1:
            isxpspent = True
        if user.coin_spent > 1:
            iscoinspent = True
        networkslist[user.network.name]['xpspentcount'] += user.xp_spent
        networkslist[user.network.name]['coinspentcount'] += user.coin_spent
        networkslist[user.network.name]['coingivencount'] += user.coin_given
        #networkslist[user.network.name]['coingivenperspentcount'] += user.coin_given / (user.coin_spent + 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,
        'isxpspent': isxpspent,
        'iscoinspent': iscoinspent,
    }
    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 + "'")
    
    totalchannelstats = convert_units.shorten_number(len(channels))
    totaluserstats = convert_units.shorten_number(len(users))
    totalmessagestats = 0
    totalactionstats = 0
    totalnoticestats = 0
    for message in messages:
        totalmessagestats += message[2]
        totalactionstats += message[3]
        totalnoticestats += message[4]
    totalmessagestats = convert_units.shorten_number(totalmessagestats)
    totalactionstats = convert_units.shorten_number(totalactionstats)
    totalnoticestats = convert_units.shorten_number(totalnoticestats)
    channelslist = {}
    for channel in channels:
        channelslist[channel.name] = {'joincount': 0, 'messagecount': 0, 'kickcount': 0}
    userslist = {}
    totalcoinspent = 0
    totalcoingiven = 0
    totalxpstats = 0
    totalcoinstats = 0
    isxpspent = False
    for user in users:
        userslist[user.name] = {'joincount': 0, 'messagecount': 0, 'kgivencount': 0, 'kreceivedcount': 0, 'xpspent': user.xp_spent, 'coin': user.coin}
        print(user.level)
        if user.level == "0":
            totalcoinspent += user.coin_spent - user.coin_given
            totalcoingiven += user.coin_given
            totalcoinstats += user.coin
        print(user.xp_spent)
        if not user.xp_spent == 0:
            isxpspent = True
        totalxpstats += user.xp_spent
    print(isxpspent)
    
    totalcoinstats = convert_units.shorten_number(totalcoinstats)
    totalxpstats = convert_units.shorten_number(totalxpstats)
    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]
        
    context = {
        'network': network,
        'channels': channels,
        'users': users,
        'totalchannelstats': totalchannelstats,
        'totaluserstats': totaluserstats,
        'totalmessagestats': totalmessagestats,
        'totalactionstats': totalactionstats,
        'totalnoticestats': totalnoticestats,
        'channelslist': channelslist,
        'userslist': userslist,
        'totalcoinspent': totalcoinspent, 
        'totalcoingiven': totalcoingiven,
        'kicks': kicks,
        'isxpspent': isxpspent,
        'totalxpstats': totalxpstats,
        'totalcoinstats': totalcoinstats,
    }
    return render(request, 'stats/network.html', context)
