import json
import requests
from database.db_connection import db_connect


def fcmPushNotification(requiredData):

    con = db_connect()
    cur = con.cursor(dictionary=True, buffered=True)
    user_id = requiredData['user_id']
    channel = requiredData['channel']
    title = requiredData['title']
    message = requiredData['message']
    action_route = requiredData['route']
    web_url = requiredData['url']
    # big_image = requiredData['big_image']
    # small_image = requiredData['small_image']
    fcm_server_token = 'AAAAgFROJRU:APA91bGjm-5uX3h4SJWxFEQy_dDvmLDbIuhbIgfou9YyAyImTrZmaRX7cR4Fy8n_9RzqC_qA24-cpHDHTUsw_p10KLstUKS7uhpO-1RLu2UbtAjMbe7B8UL9cdv8vrCzvMR8XLOHF9Ba'
    headers = {
        'Content-Type': 'application/json',
        'Authorization': 'key=' + fcm_server_token,
    }
    cur.execute(
        'select noti_token from notification_user where user_id=%s and type=%s', (user_id, '1'))
    rawData = cur.fetchall()

    for x in rawData:
        notiToken = x['noti_token']
        data = {
            'channel': channel,
            'title': title,
            'message': message,
            'action_route': action_route,
            'url': web_url
            # 'big_img': big_image,
            # 'small_img': small_image
        }
        body = {
            'data': data,
            'to': notiToken,
            'priority': 'high'
        }
        response = requests.post(
            "https://fcm.googleapis.com/fcm/send", headers=headers, data=json.dumps(body))
        r_data = response.json()
        if r_data['success'] == 1:
            print("[FCM PUSH ALERT] : Push data is successfully sent.")
        else:
            print("[FCM PUSH ALERT] : Sending push data failed.")
    con.disconnect()


def fcmBroadcastNotification(requiredData):

    # con = db_connect()
    # cur = con.cursor(dictionary=True, buffered=True)
    channel = "general"
    title = requiredData['title']
    message = requiredData['message']
    action_route = '/index'
    web_url = ''
    # big_image = requiredData['big_image']
    # small_image = requiredData['small_image']
    fcm_server_token = 'AAAAgFROJRU:APA91bGjm-5uX3h4SJWxFEQy_dDvmLDbIuhbIgfou9YyAyImTrZmaRX7cR4Fy8n_9RzqC_qA24-cpHDHTUsw_p10KLstUKS7uhpO-1RLu2UbtAjMbe7B8UL9cdv8vrCzvMR8XLOHF9Ba'
    headers = {
        'Content-Type': 'application/json',
        'Authorization': 'key=' + fcm_server_token,
    }

    data = {
        'channel': channel,
        'title': title,
        'message': message,
        'action_route': action_route,
        'url': web_url
        # 'big_img': big_image,
        # 'small_img': small_image
    }
    body = {
        'data': data,
        'to': '/topics/get-ads-noti',
        'priority': 'high'
    }
    response = requests.post(
        "https://fcm.googleapis.com/fcm/send", headers=headers, data=json.dumps(body))
    r_data = response.json()
    print(r_data['message_id'])
    # if r_data['success'] == 1:
    #     print("[FCM PUSH ALERT] : Push data is successfully sent.")
    # else:
    #     print("[FCM PUSH ALERT] : Sending push data failed.")
    # con.disconnect()
