# coding: utf8
from __future__ import unicode_literals
from backoffice.models import *
from fcm_django.models import FCMDevice
from fcm_django.fcm import FCMError

import logging
# Get an instance of a logger
logger = logging.getLogger('django')


def run():
    logger.info("SEND PUSH")
    user = User.objects.get(email="christophe.surbier@gmail.com")
    message="Test envoi push Agender"
    if user.ios and user.pushToken:
        try:
            device = FCMDevice.objects.get(registration_id=user.pushToken, active=True)
            if device:
                device.send_message(title="Agender" ,body=message ,badge=1 ,sound="default",  data={"title": "Agender", "body": message})
                logger.info("on envoi message a utilisateur %s" % (user))
        except FCMError(Exception):
            logger.error("FCMError %s", Exception)
            #email = user.email
            #envoiMessageMail(email, notification.message, "HopHopFood")
        except Exception as e:
            logger.error("Impossible d'envoyer push une exception est survenue %s", e)
    elif user.android and user.pushToken:
        try:
            device = FCMDevice.objects.get(registration_id=user.pushToken, active=True)
            if device:
                device.send_message(title="Agender", body=message ,badge=1 ,sound="default"
                                    ,click_action="FCM_PLUGIN_ACTIVITY",  data={"title": "Agender", "body": message});
                logger.info("on envoi message a utilisateur %s" % (user))
            # On passe la notif au status Envoye
        except FCMError(Exception):
            logger.error("FCMError %s", Exception)
            #email = user.email
            #envoiMessageMail(email, notification.message, "HopHopFood")
        except Exception as e:
            logger.error("Impossible d'envoyer push une exception est survenue %s", e)
