# Path: ngnotifier/management/utils.py
# def query_yes_no(question, default="yes"):
#     """Ask a yes/no question via raw_input() and return their answer.
# 
#     "question" is a string that is presented to the user.
#     "default" is the presumed answer if the user just hits <Enter>.
#         It must be "yes" (the default), "no" or None (meaning
#         an answer is required of the user).
# 
#     The "answer" return value is one of "yes" or "no".
#     """
#     valid = {"yes": True, "y": True, "ye": True,
#              "no": False, "n": False}
#     if default == None:
#         prompt = " [y/n] "
#     elif default == "yes":
#         prompt = " [Y/n] "
#     elif default == "no":
#         prompt = " [y/N] "
#     else:
#         raise ValueError("invalid default answer: '%s'" % default)
# 
#     while True:
#         stdout.write(question + prompt)
#         choice = input().lower()
#         if default is not None and choice == '':
#             return valid[default]
#         elif choice in valid:
#             return valid[choice]
#         else:
#             stdout.write("Please respond with 'yes' or 'no' "
#                          "(or 'y' or 'n').\n")
# 
# Path: ngnotifier/tasks.py
# def update_news(verbose=False, notif=True):
#     notif_count_before = Log.objects.filter(type='N').count()
#     for host in NGHost.objects.all():
#         if verbose:
#             print('Checking host {}:'.format(host.host), flush=False)
#         groups = NGGroup.objects.filter(host=host)
#         tmp_co = host.get_co()
#         new_news_list_all = []
#         for group in groups:
#             new_news_list = group.update_news(tmp_co, verbose=verbose)
#             new_news_list_all += new_news_list
#             if notif:
#                 send_notif(new_news_list, group)
# 
#         print("    Found {} news".format(len(new_news_list_all)))
# 
#         print("\    Updating kinship links...")
#         kinship_updater()
#         for nn in new_news_list_all:
#             nn.add_answer_count()
#     if verbose and notif:
#         notif_count_after = Log.objects.filter(type='N').count()
#         print('    Sent {} notifications!'.format(notif_count_after-notif_count_before))
#     Log.objects.create(type='UN')
# 
# def update_hosts(verbose=False):
#     for host in NGHost.objects.all():
#         host.update_groups(groups=hosts[host.host]['groups'], verbose=verbose)
#     Log.objects.create(type='UG')
# 
# def update_stats(verbose=False):
#     for host in NGHost.objects.all():
#         groups = NGGroup.objects.filter(host=host)
#         newss = pd.DataFrame.from_records(NGNews.objects.filter(groups__in=groups).values('date', 'father', 'has_children'))
#         newss['date'] = newss.date.dt.floor('D')
#         newss['father'] = newss.father.map(lambda x: 1 if x != '' else 0)
#         newss['has_children'] = newss.has_children.map(lambda x: 1 if x else 0)
#         count_by_day = newss.drop("has_children", axis=1).groupby('date').count()
#         count_by_day_root = newss.drop("has_children", axis=1)[newss.father == 0].groupby('date').count()
#         count_by_day_children = newss.drop("has_children", axis=1)[newss.father == 1].groupby('date').count()
#         tmp = newss[newss.father == 0]
#         count_root_answered = tmp.drop("father", axis=1)[tmp.has_children == 1].groupby('date').count()
#         count_root_never_answered = tmp.drop("father", axis=1)[tmp.has_children == 0].groupby('date').count()
# 
#         min_date = newss.date.min().floor('D')
#         max_date = newss.date.max().ceil('D')
#         t = pd.date_range(min_date, max_date, freq='D').to_frame()
# 
#         res = t.merge(count_by_day, left_on=0, right_on='date', how='left')\
#             .merge(count_by_day_root,  left_on=0, right_on='date', how='left')\
#             .merge(count_by_day_children, left_on=0, right_on='date', how='left')\
#             .merge(count_root_answered, left_on=0, right_on='date', how='left')\
#             .merge(count_root_never_answered, left_on=0, right_on='date', how='left')\
#             .fillna(0)
#         res.columns = ['date', 'total', 'root', 'children', 'answered', 'never_answered']
#         res.to_csv(os.path.join(STATIC_ROOT, 'stats_news_{}.csv'.format(host.host)))
# 
#         Log.objects.create(type='US')
# 
# Path: ngnotifier/utils.py
# class bcolors:
#     HEADER = '\033[95m'
#     OKBLUE = '\033[94m'
#     OKGREEN = '\033[92m'
#     WARNING = '\033[93m'
#     FAIL = '\033[91m'
#     ENDC = '\033[0m'
#     BOLD = '\033[1m'
#     UNDERLINE = '\033[4m'


import time
import os
from ngnotifier.management.utils import query_yes_no
from django.core.management.base import BaseCommand
from django.conf import settings
from apscheduler.schedulers.background import BackgroundScheduler
from ngnotifier.settings import SECONDS_DELTA_NEWS, SECONDS_DELTA_GROUP
from ngnotifier.tasks import update_news, update_hosts, update_stats
from ngnotifier.utils import bcolors



class Command(BaseCommand):
    help = 'Checker for new news and new groups'

    def add_arguments(self, parser):
        parser.add_argument('--notifs', dest='notifs', default='False',
                            help='Notify when new news are detected.')
        parser.add_argument('--verbose', dest='verbose', default='True',
                            help='Print more informations on STDOUT.')



# Path: ngnotifier/management/utils.py
# def query_yes_no(question, default="yes"):
#     """Ask a yes/no question via raw_input() and return their answer.
# 
#     "question" is a string that is presented to the user.
#     "default" is the presumed answer if the user just hits <Enter>.
#         It must be "yes" (the default), "no" or None (meaning
#         an answer is required of the user).
# 
#     The "answer" return value is one of "yes" or "no".
#     """
#     valid = {"yes": True, "y": True, "ye": True,
#              "no": False, "n": False}
#     if default == None:
#         prompt = " [y/n] "
#     elif default == "yes":
#         prompt = " [Y/n] "
#     elif default == "no":
#         prompt = " [y/N] "
#     else:
#         raise ValueError("invalid default answer: '%s'" % default)
# 
#     while True:
#         stdout.write(question + prompt)
#         choice = input().lower()
#         if default is not None and choice == '':
#             return valid[default]
#         elif choice in valid:
#             return valid[choice]
#         else:
#             stdout.write("Please respond with 'yes' or 'no' "
#                          "(or 'y' or 'n').\n")
# 
# Path: ngnotifier/tasks.py
# def update_news(verbose=False, notif=True):
#     notif_count_before = Log.objects.filter(type='N').count()
#     for host in NGHost.objects.all():
#         if verbose:
#             print('Checking host {}:'.format(host.host), flush=False)
#         groups = NGGroup.objects.filter(host=host)
#         tmp_co = host.get_co()
#         new_news_list_all = []
#         for group in groups:
#             new_news_list = group.update_news(tmp_co, verbose=verbose)
#             new_news_list_all += new_news_list
#             if notif:
#                 send_notif(new_news_list, group)
# 
#         print("    Found {} news".format(len(new_news_list_all)))
# 
#         print("\    Updating kinship links...")
#         kinship_updater()
#         for nn in new_news_list_all:
#             nn.add_answer_count()
#     if verbose and notif:
#         notif_count_after = Log.objects.filter(type='N').count()
#         print('    Sent {} notifications!'.format(notif_count_after-notif_count_before))
#     Log.objects.create(type='UN')
# 
# def update_hosts(verbose=False):
#     for host in NGHost.objects.all():
#         host.update_groups(groups=hosts[host.host]['groups'], verbose=verbose)
#     Log.objects.create(type='UG')
# 
# def update_stats(verbose=False):
#     for host in NGHost.objects.all():
#         groups = NGGroup.objects.filter(host=host)
#         newss = pd.DataFrame.from_records(NGNews.objects.filter(groups__in=groups).values('date', 'father', 'has_children'))
#         newss['date'] = newss.date.dt.floor('D')
#         newss['father'] = newss.father.map(lambda x: 1 if x != '' else 0)
#         newss['has_children'] = newss.has_children.map(lambda x: 1 if x else 0)
#         count_by_day = newss.drop("has_children", axis=1).groupby('date').count()
#         count_by_day_root = newss.drop("has_children", axis=1)[newss.father == 0].groupby('date').count()
#         count_by_day_children = newss.drop("has_children", axis=1)[newss.father == 1].groupby('date').count()
#         tmp = newss[newss.father == 0]
#         count_root_answered = tmp.drop("father", axis=1)[tmp.has_children == 1].groupby('date').count()
#         count_root_never_answered = tmp.drop("father", axis=1)[tmp.has_children == 0].groupby('date').count()
# 
#         min_date = newss.date.min().floor('D')
#         max_date = newss.date.max().ceil('D')
#         t = pd.date_range(min_date, max_date, freq='D').to_frame()
# 
#         res = t.merge(count_by_day, left_on=0, right_on='date', how='left')\
#             .merge(count_by_day_root,  left_on=0, right_on='date', how='left')\
#             .merge(count_by_day_children, left_on=0, right_on='date', how='left')\
#             .merge(count_root_answered, left_on=0, right_on='date', how='left')\
#             .merge(count_root_never_answered, left_on=0, right_on='date', how='left')\
#             .fillna(0)
#         res.columns = ['date', 'total', 'root', 'children', 'answered', 'never_answered']
#         res.to_csv(os.path.join(STATIC_ROOT, 'stats_news_{}.csv'.format(host.host)))
# 
#         Log.objects.create(type='US')
# 
# Path: ngnotifier/utils.py
# class bcolors:
#     HEADER = '\033[95m'
#     OKBLUE = '\033[94m'
#     OKGREEN = '\033[92m'
#     WARNING = '\033[93m'
#     FAIL = '\033[91m'
#     ENDC = '\033[0m'
#     BOLD = '\033[1m'
#     UNDERLINE = '\033[4m'


# Path: ngnotifier/management/commands/tasks.py
import time
import os
from ngnotifier.management.utils import query_yes_no
from django.core.management.base import BaseCommand
from django.conf import settings
from apscheduler.schedulers.background import BackgroundScheduler
from ngnotifier.settings import SECONDS_DELTA_NEWS, SECONDS_DELTA_GROUP
from ngnotifier.tasks import update_news, update_hosts, update_stats
from ngnotifier.utils import bcolors




class Command(BaseCommand):
    help = 'Checker for new news and new groups'

    def add_arguments(self, parser):
        parser.add_argument('--notifs', dest='notifs', default='False',
                            help='Notify when new news are detected.')
        parser.add_argument('--verbose', dest='verbose', default='True',
                            help='Print more informations on STDOUT.')



    def handle(self, *args, **options):