# Path: whyattend/model.py
# def init_db():
#     def __init__(self, wot_id, openid, member_since, name, clan, role, locked=False):
#     def is_recruit(self):
#     def battles_played(self):
#     def battles_reserve(self):
#     def __repr__(self):
#     def to_dict(self):
#     def player_role_value(self):
#     def __init__(self, player, battle, reserve=False):
#     def __init__(self, date, clan, enemy_clan, victory, draw, creator, battle_commander, map_name, map_province,
#                  duration, description='', paid=False):
#     def outcome_str(self):
#     def outcome_repr(self):
#     def has_player(self, player):
#     def has_reserve(self, player):
#     def get_players(self):
#     def get_reserve_players(self):
#     def __str__(self):
#     def __init__(self, title, description, clan, date):
#     def get_players(self):
#     def get_reserves(self):
#     def get_final_battle(self):
#     def get_representative_battle(self):
#     def __init__(self, replay_blob, replay_pickle):
#     def unpickle(self):
#     def get(cls):
# class Player(Base):
# class BattleAttendance(Base):
# class Battle(Base):
# class BattleGroup(Base):
# class Replay(Base):
# class WebappData(Base):


import datetime
import logging
from celery import Celery
from celery.utils.log import get_task_logger
from . import config, wotapi
from .model import Player, WebappData, db_session


@celery.task(rate_limit='5/s')
def get_player_info(player_wot_id):
    logger.info("get_player_info(" + player_wot_id + ")")
    return wotapi.get_player(player_wot_id)


@celery.task(rate_limit='1/s')
def get_clan_info(clan_id):
    logger.info("get_clan(" + clan_id + ")")
    return wotapi.get_clan(clan_id)


@celery.task(rate_limit='1/m')
def synchronize_players(clan_id):
    logger.info("synchronize_players(" + clan_id + ")")
    logger.info("Clan member synchronization triggered for " + str(clan_id))
    webapp_data = WebappData.get()
    webapp_data.last_sync_attempt = datetime.datetime.now()
    db_session.add(webapp_data)
    db_session.commit()
    db_session.remove()

    clan_info = get_clan_info.delay(str(clan_id)).wait()
    logger.info("Synchronizing " + clan_info['data'][str(clan_id)]['abbreviation'])
    processed = set()
    for player_id in clan_info['data'][str(clan_id)]['members']:
        player = clan_info['data'][str(clan_id)]['members'][player_id]
        player_data = get_player_info.delay(str(player['account_id'])).wait()

# Path: whyattend/model.py
# def init_db():
#     def __init__(self, wot_id, openid, member_since, name, clan, role, locked=False):
#     def is_recruit(self):
#     def battles_played(self):
#     def battles_reserve(self):
#     def __repr__(self):
#     def to_dict(self):
#     def player_role_value(self):
#     def __init__(self, player, battle, reserve=False):
#     def __init__(self, date, clan, enemy_clan, victory, draw, creator, battle_commander, map_name, map_province,
#                  duration, description='', paid=False):
#     def outcome_str(self):
#     def outcome_repr(self):
#     def has_player(self, player):
#     def has_reserve(self, player):
#     def get_players(self):
#     def get_reserve_players(self):
#     def __str__(self):
#     def __init__(self, title, description, clan, date):
#     def get_players(self):
#     def get_reserves(self):
#     def get_final_battle(self):
#     def get_representative_battle(self):
#     def __init__(self, replay_blob, replay_pickle):
#     def unpickle(self):
#     def get(cls):
# class Player(Base):
# class BattleAttendance(Base):
# class Battle(Base):
# class BattleGroup(Base):
# class Replay(Base):
# class WebappData(Base):


# Path: whyattend/tasks.py
import datetime
import logging
from celery import Celery
from celery.utils.log import get_task_logger
from . import config, wotapi
from .model import Player, WebappData, db_session



@celery.task(rate_limit='5/s')
def get_player_info(player_wot_id):
    logger.info("get_player_info(" + player_wot_id + ")")
    return wotapi.get_player(player_wot_id)


@celery.task(rate_limit='1/s')
def get_clan_info(clan_id):
    logger.info("get_clan(" + clan_id + ")")
    return wotapi.get_clan(clan_id)


@celery.task(rate_limit='1/m')
def synchronize_players(clan_id):
    logger.info("synchronize_players(" + clan_id + ")")
    logger.info("Clan member synchronization triggered for " + str(clan_id))
    webapp_data = WebappData.get()
    webapp_data.last_sync_attempt = datetime.datetime.now()
    db_session.add(webapp_data)
    db_session.commit()
    db_session.remove()

    clan_info = get_clan_info.delay(str(clan_id)).wait()
    logger.info("Synchronizing " + clan_info['data'][str(clan_id)]['abbreviation'])
    processed = set()
    for player_id in clan_info['data'][str(clan_id)]['members']:
        player = clan_info['data'][str(clan_id)]['members'][player_id]
        player_data = get_player_info.delay(str(player['account_id'])).wait()

        p = Player.query.filter_by(wot_id=str(player['account_id'])).first()