'use strict';
const httpStatus = require('http-status');

const generateControllers = require('../../core/controllers');
const { buildContext } = require('../../core/controllers');
const notificationService = require('./notification.service');
const notificationDetailService = require('./notification_detail.service');
const userDeviceService = require('./user_device.service');

const { extractParamsFromRequest, handleErrorResponse, handleResultResponse } = require('../../helpers/controller.helper');

// Module Name
const MODULE_NAME = '[notification.controller]';
const controllerOptions = { MODULE_NAME };

const extraControllers = {
    sendNotification: (config) => {
        return async (req, res, next) => {
            config = config || {
                scopes: [],
            };

            let receipt = undefined;
            const context = buildContext(req, config);
            let params = extractParamsFromRequest(req, res);

            let userIds = req.body.userIds;
            if (!userIds) {
                return handleErrorResponse(controllerOptions.MODULE_NAME, 'sendNotification', new Error('Missing user Ids'), res)
            }

            if (!req.body.title) {
                return handleErrorResponse(controllerOptions.MODULE_NAME, 'sendNotification', new Error('Missing message title'), res)
            }

            if (!req.body.message) {
                return handleErrorResponse(controllerOptions.MODULE_NAME, 'sendNotification', new Error('Missing message content'), res)
            }

            try {
                let getUserDevicesPromise = (userId) => userDeviceService.fetchAll({ params: { userId: userId }}, context).then(function(result) {
                    return new Promise(function(resolve) { resolve(result.rows) });
                });
                let saveNotificationPromise = (notification) => notificationService.create(notification, context);
                let sendNotificationsPromise = (messages) => notificationService.sendNotification(messages);
                let disableUserDevicePromise = (token) => userDeviceService.update({ params: {
                    token: token,
                }}, { valid: false }, context);

                let disableInvalidUserDevicesPromise = (userDevices) => {
                    return new Promise(function (resolve) {
                        let _userDevices = [];
                        userDevices.forEach(async function (userDevice) {
                            if (!userDeviceService.isValidPushToken(userDevice.token)) {
                                await disableUserDevicePromise(userDevice.token);
                            } else {
                                _userDevices.push(userDevice);
                            }
                        });

                        resolve(_userDevices)
                    });
                };

                let disableUserDevicesWithErrorStatus = (messages, tickets) => {
                    return new Promise(function (resolve) {
                        tickets.forEach(async function (ticket, index) {
                            if ((ticket.status === 'error') && (ticket.details.error === 'DeviceNotRegistered')) {                                
                                await disableUserDevicePromise(messages[index].to)
                            }
                        });
                        resolve(true);
                    });
                }

                let saveResponseStatusPromise = (messages, tickets) => notificationDetailService.saveNotificationDetails(messages, tickets);
                
                const notificationRecord = {
                    title: req.body.title,
                    body: req.body.message,
                    ttl: req.body.ttl || undefined,
                    priority: req.body.priority || 'default',
                    data: req.body.data || { userIds: req.body.userIds },
                };

                let buildMessagesPromise = (userDevices) => {                    
                    return new Promise(function (resolve) { 
                        let messages = [];

                        userDevices.forEach(async function (userDevice) {       
                            messages.push({
                                title: notificationRecord.title,
                                body: notificationRecord.body,
                                ttl: notificationRecord.ttl,
                                priority: notificationRecord.priority,
                                userId: userDevice.userId,
                                to: userDevice.token,
                                sound: 'default',
                                notificationId: notificationRecord.id,
                                data: notificationRecord.data,
                            });
                        });
                    
                        resolve(messages) 
                    });
                };
                
                let getUserDevicesList = [];
                
                userIds.forEach(function (userId) {
                    getUserDevicesList.push(getUserDevicesPromise(userId));
                });

                saveNotificationPromise(notificationRecord)
                .then(function(notification) {
                    notificationRecord.id = notification.id;
                    return Promise.all(getUserDevicesList)
                }).then(function (userDeviceList) {
                    return new Promise(function(resolve) { resolve(userDeviceList[0]); });
                })
                .then(disableInvalidUserDevicesPromise)
                .then(buildMessagesPromise)
                .then(sendNotificationsPromise)
                .then(function({ messages, tickets }) {
                    let _saveStatus = saveResponseStatusPromise(messages, tickets);
                    let _disableDevices = disableUserDevicesWithErrorStatus(messages, tickets);

                    return Promise.all([_saveStatus, _disableDevices]);
                })
                .then(function (details) {
                    console.log(details);
                });
            } catch (error) {
                console.error(error);
                return handleErrorResponse(controllerOptions.MODULE_NAME, 'sendNotification', error, res)
            } finally {
                return handleResultResponse(receipt, null, null, res, httpStatus.OK);
            }
        }
    },

    updateNotificationsWithReceipts: (config) => {
        return async (req, res, next) => {
            config = config || {
                scopes: [],
            };

            const context = buildContext(req, config);
            let params = extractParamsFromRequest(req, res);

            let getNotificationsWithoutReceipt = async () => {
                let params = {
                    where: { }
                };               
                return await notificationDetailService.fetchAll(params, context);
            }

            let receipt = await Promise.all(getUserDevicesList).then(
                function (userDeviceList) {
                    return Promise.all(userDeviceList.map(buildMessagePromise))
                })
                .then(sendNotificationsPromise)
        }
    },

    registerDevice: (config) => {
        return async (req, res, next) => {
            config = config || {
                scopes: [],
            };

            try {
                const context = buildContext(req, config);
                var data = {
                    token: req.body.token,
                    valid: 1,
                    userId: context.user.id
                };

                let params = extractParamsFromRequest(req, res, { 
                    includeAll: false, 
                    paginate: { limit: 1, page: 1 },
                    params: {
                        userId: context.user.id,
                        token: data.token,
                    }
                });

                // Buscamos el token y el usuario
                console.log('>> Busco el usuario y el token', params.params);
                let result = await userDeviceService.fetchOne(params, context);
                if (!result) {
                    // Dar de alta el token
                    console.log('>> Dar de alta el token', data);
                    result = await userDeviceService.create(data, context);
                } else {
                    // Actualizar el token
                    console.log('>> Actualizar el token', params.params, data, context);
                    result = await userDeviceService.update(params.params, data, context);
                }
            } catch(error) {
                console.error(error);
            } finally {
                // En todo caso devolver OK al cliente
                return handleResultResponse('OK', null, null, res, httpStatus.OK);    
            }
        }
    },    
};

module.exports = generateControllers(notificationService, extraControllers, controllerOptions);