# -*- coding: utf-8 -*-

"""
Generate Randum Mahjong Hanchans to the the Raning System
"""

import random
from datetime import timedelta

from django.contrib import auth
from django.core.management.base import BaseCommand

from events.models import Event
from mahjong_ranking import models


class Command(BaseCommand):
    help = "Deletes all expired user registrations from the database"

    def add_players(self, hanchan):
        user_list = set()
        while len(user_list) < 4:
            random_user = random.choice(self.user_list)
            user_list.add(random_user)

        player_list = list()
        ostwind_list = list()
        for user in user_list:
            player_list.append(
                models.Player(user=user, hanchan=hanchan, score=25000))
        for player in player_list:
            player.save()

        end_of_game = False
        ostwind_list.extend(player_list)
        ostwind_list.extend(player_list)
        ostwind = ostwind_list.pop()
        while not end_of_game:
            score = random.randrange(1300, 8000, 100)
            loser = player_list[random.randrange(0, 4, 1)]
            winner = player_list[random.randrange(0, 4, 1)]
            winner.score += score

            print 'Ostwind: %s, Gewinner: %s, Verlierer: %s, %d Punkte' % (
                ostwind.user,
                winner.user,
                loser.user,
                score,
            )

            if winner == loser:
                # Player wins with Tsumo: Everybody pays a third of the score.
                print "Tsumo!"
                for player in player_list:
                    if player != winner:
                        player.score -= score / 3
            else:
                loser.score -= score

            for player in player_list:
                if player.score <= 0:
                    player.score = 0
                    end_of_game = True

            if winner == ostwind:
                print "Wind bleibt"
            else:
                print "Wind wird gewechselt."
                try:
                    ostwind = ostwind_list.pop()
                except IndexError:
                    end_of_game = True
        print '---------------------------------------------------------------------'
        for player in player_list:
            print "%s: %s" % (player.user, player.score)
            player.save()
        print ""

    def create_hanchan(self, event):
        start = event.start + timedelta(minutes=random.randrange(00, 300, 15))
        print event.name, start
        print '=' * 80
        hanchan = models.Hanchan(event=event, start=start)
        hanchan.save()
        self.add_players(hanchan)
        hanchan.save()


    def handle(self, *args, **options):
        num_hanchans = int(options.get('hanchans', 4))
        self.user_list = list(auth.get_user_model().objects.all())

        for event in Event.objects.all():
            for i in range(random.randrange(2, 8)):
                self.create_hanchan(event)


