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

"""
Recalculate Mahjong Rankings...
"""

from django.core.management.base import BaseCommand
from datetime import date, datetime, time
from mahjong_ranking import models
from django.utils.dateparse import parse_date
from django.utils import timezone

class Command(BaseCommand):
    """ Recalculate all Kyu/Dan Rankings """

    help = "Recalculate all Kyu/Dan Rankings"

    def add_arguments(self, parser):
        parser.add_argument('--since', nargs='?', type=parse_date)
        parser.add_argument('--until', nargs='?', type=parse_date)
        parser.add_argument('--forcerecalc', action='store_true')

    def handle(self, *args, **options):
        since = options.get('since', None)
        until = options.get('until', None)
        force_recalc = options.get('forecerecalc', False)
        if isinstance(since, date):
            since = datetime.combine(since, time(0, 0, 0))
            since = timezone.make_aware(since)
        if isinstance(until, date):
            until = datetime.combine(until, time(23, 59, 59))
            until = timezone.make_aware(until)
        models.KyuDanRanking.objects.update(since=since, until=until, force_recalc=force_recalc)