/*
 * ICESOFT COMMERCIAL SOURCE CODE LICENSE V 1.1
 *
 * The contents of this file are subject to the ICEsoft Commercial Source
 * Code License Agreement V1.1 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a copy of the
 * License at
 * http://www.icesoft.com/license/commercial-source-v1.1.html
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations under
 * the License.
 *
 * Copyright 2009-2015 ICEsoft Technologies Canada, Corp. All Rights Reserved.
 */

package com.icesoft.icepush;

import org.icepush.NotificationProvider;
import org.icepush.OutOfBandNotifier;
import org.icepush.PushNotification;
import org.icepush.util.ExtensionRegistry;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * An implementation of a NotificationProvider that simply logs to the console.  Useful for debugging
 * or when you want to simulate a "cloud" style push from a desktop browser.  It requires that the
 * context parameter org.icemobile.registerLoggingProvider be set to true.  The protocol to register
 * with is "log" (e.g. log:yourCustomInformationHere);
 */
public class LoggingNotificationProvider implements NotificationProvider {

    private static final Logger log = Logger.getLogger(LoggingNotificationProvider.class.getName());

    //The key to use if the credentials are specified using ICEfaces system property or parameter
    private static final String LOGGING_PROVIDER_ON = "org.icemobile.registerLoggingProvider";

    private OutOfBandNotifier outOfBandNotifier;

    public LoggingNotificationProvider() {
    }

    public void send(String uri, PushNotification notification) {
        StringBuilder mess = new StringBuilder();
        mess.append("\n  uri    : ").append(uri);
        mess.append("\n  subject: ").append(notification.getSubject());
        mess.append("\n  detail : ").append(notification.getDetail());
        log.info(mess.toString());
    }

    public void registerWith(OutOfBandNotifier outOfBandNotifier) {
        outOfBandNotifier.registerProvider("log", this);
        this.outOfBandNotifier = outOfBandNotifier;
    }

    public static class AutoRegister implements ServletContextListener {

        //Private logger required to avoid instantiating the outer class and to help with NoClassDefFoundError
        private static final Logger registerLog = Logger.getLogger(AutoRegister.class.getName());

        public void contextInitialized(ServletContextEvent servletContextEvent) {

            ServletContext servletContext = servletContextEvent.getServletContext();

            try {
                //Create a new provider
                LoggingNotificationProvider provider = new LoggingNotificationProvider();

                //Only register if it's turned on
                String param = servletContext.getInitParameter(LOGGING_PROVIDER_ON);
                if (param == null || !Boolean.parseBoolean(param)) {
                    return;
                }

                ExtensionRegistry.addExtension(servletContext, 10, NotificationProvider.class.getName(), provider);
                registerLog.log(Level.INFO, "Logging Notification Provider is on");

            } catch (Throwable t) {
                registerLog.log(Level.INFO, "Logging Notification Provider is off");
                registerLog.log(Level.FINE, "reason: ", t);
            }
        }

        public void contextDestroyed(ServletContextEvent servletContextEvent) {
        }
    }

}