# Path: app/decorators.py
# def threaded(fn):
#     """
#     Decorator to make a function run in his own thread.
#     """
#     def wrapper(*args, **kwargs):
#         Thread(target=fn, args=args, kwargs=kwargs).start()
#     return wrapper
# 
# Path: app/models.py
# class ResponseLog(models.Model):
#     """
#     Model for logging info about the device response.
#     """
#     platform = models.CharField(choices=PLATFORM_CHOICES, max_length=10)
#     roundtrip_time = models.FloatField()
#     available = models.BooleanField()
#     date = models.DateTimeField(auto_now_add=True)
# 
# Path: app/push.py
# def send_call_message(device, unique_key, phonenumber, caller_id, attempt):
#     """
#     Function to send the call push notification.
# 
#     Args:
#         device (Device): A Device object.
#         unique_key (string): String with the unique_key.
#         phonenumber (string): Phonenumber that is calling.
#         caller_id (string): ID of the caller.
#         attempt (int): The amount of attempts made.
#     """
#     data = {
#         'unique_key': unique_key,
#         'phonenumber': phonenumber,
#         'caller_id': caller_id,
#         'attempt': attempt,
#     }
#     if device.app.platform == APNS_PLATFORM:
#         send_apns_message(device, device.app, TYPE_CALL, data)
#     elif device.app.platform == GCM_PLATFORM:
#         send_gcm_message(device, device.app, TYPE_CALL, data)
#     elif device.app.platform == ANDROID_PLATFORM:
#         send_fcm_message(device, device.app, TYPE_CALL, data)
#     else:
#         log_middleware_information(
#             '{0} | Trying to sent \'call\' notification to unknown platform:{1} device:{2}',
#             OrderedDict([
#                 ('unique_key', unique_key),
#                 ('platform', device.app.platform),
#                 ('token', device.token),
#             ]),
#             logging.WARNING,
#             device=device,
#         )
# 
# def send_text_message(device, app, message):
#     """
#     Function to send a push notification with a message.
# 
#     Args:
#         device (Device): A Device object.
#         message (string): The message that needs to be send to the device.
#     """
#     if app.platform == APNS_PLATFORM:
#         send_apns_message(device, app, TYPE_MESSAGE, {'message': message})
#     elif app.platform == GCM_PLATFORM:
#         send_gcm_message(device, app, TYPE_MESSAGE, {'message': message})
#     elif app.platform == ANDROID_PLATFORM:
#         send_fcm_message(device, app, TYPE_MESSAGE, {'message': message})
#     else:
#         log_middleware_information(
#             'Trying to sent \'message\' notification to unknown platform:{0} device:{1}',
#             OrderedDict([
#                 ('platform', device.app.platform),
#                 ('token', device.token),
#             ]),
#             logging.WARNING,
#             device=device,
#         )


from .decorators import threaded
from .models import ResponseLog
from .push import send_call_message, send_text_message


@threaded
def task_incoming_call_notify(device, unique_key, phonenumber, caller_id, attempt):
    """
    Threaded task to send a call push notification.
    """
    send_call_message(device, unique_key, phonenumber, caller_id, attempt)


@threaded
def task_notify_old_token(device, app):
    """
    Threaded task to send a text push notification.
    """
    msg = 'A other device has registered for the same account. You won\'t be reachable on this device'
    send_text_message(device, app, msg)



# Path: app/decorators.py
# def threaded(fn):
#     """
#     Decorator to make a function run in his own thread.
#     """
#     def wrapper(*args, **kwargs):
#         Thread(target=fn, args=args, kwargs=kwargs).start()
#     return wrapper
# 
# Path: app/models.py
# class ResponseLog(models.Model):
#     """
#     Model for logging info about the device response.
#     """
#     platform = models.CharField(choices=PLATFORM_CHOICES, max_length=10)
#     roundtrip_time = models.FloatField()
#     available = models.BooleanField()
#     date = models.DateTimeField(auto_now_add=True)
# 
# Path: app/push.py
# def send_call_message(device, unique_key, phonenumber, caller_id, attempt):
#     """
#     Function to send the call push notification.
# 
#     Args:
#         device (Device): A Device object.
#         unique_key (string): String with the unique_key.
#         phonenumber (string): Phonenumber that is calling.
#         caller_id (string): ID of the caller.
#         attempt (int): The amount of attempts made.
#     """
#     data = {
#         'unique_key': unique_key,
#         'phonenumber': phonenumber,
#         'caller_id': caller_id,
#         'attempt': attempt,
#     }
#     if device.app.platform == APNS_PLATFORM:
#         send_apns_message(device, device.app, TYPE_CALL, data)
#     elif device.app.platform == GCM_PLATFORM:
#         send_gcm_message(device, device.app, TYPE_CALL, data)
#     elif device.app.platform == ANDROID_PLATFORM:
#         send_fcm_message(device, device.app, TYPE_CALL, data)
#     else:
#         log_middleware_information(
#             '{0} | Trying to sent \'call\' notification to unknown platform:{1} device:{2}',
#             OrderedDict([
#                 ('unique_key', unique_key),
#                 ('platform', device.app.platform),
#                 ('token', device.token),
#             ]),
#             logging.WARNING,
#             device=device,
#         )
# 
# def send_text_message(device, app, message):
#     """
#     Function to send a push notification with a message.
# 
#     Args:
#         device (Device): A Device object.
#         message (string): The message that needs to be send to the device.
#     """
#     if app.platform == APNS_PLATFORM:
#         send_apns_message(device, app, TYPE_MESSAGE, {'message': message})
#     elif app.platform == GCM_PLATFORM:
#         send_gcm_message(device, app, TYPE_MESSAGE, {'message': message})
#     elif app.platform == ANDROID_PLATFORM:
#         send_fcm_message(device, app, TYPE_MESSAGE, {'message': message})
#     else:
#         log_middleware_information(
#             'Trying to sent \'message\' notification to unknown platform:{0} device:{1}',
#             OrderedDict([
#                 ('platform', device.app.platform),
#                 ('token', device.token),
#             ]),
#             logging.WARNING,
#             device=device,
#         )


# Path: app/tasks.py
from .decorators import threaded
from .models import ResponseLog
from .push import send_call_message, send_text_message



@threaded
def task_incoming_call_notify(device, unique_key, phonenumber, caller_id, attempt):
    """
    Threaded task to send a call push notification.
    """
    send_call_message(device, unique_key, phonenumber, caller_id, attempt)


@threaded
def task_notify_old_token(device, app):
    """
    Threaded task to send a text push notification.
    """
    msg = 'A other device has registered for the same account. You won\'t be reachable on this device'
    send_text_message(device, app, msg)



@threaded