package eu.dnetlib.client.notification;

import java.util.logging.Logger;

import javax.inject.Inject;
import javax.inject.Singleton;

import com.google.gwt.user.client.rpc.AsyncCallback;

import eu.dnetlib.client.GinWidgetGinjector;
import eu.dnetlib.efg1914.authoring.components.Configuration;
import eu.dnetlib.efg1914.authoring.components.Frame;
import eu.dnetlib.efg1914.authoring.components.Item;
import eu.dnetlib.efg1914.authoring.components.Theme;
import eu.dnetlib.efg1914.authoring.components.Topic;
import eu.dnetlib.efg1914.authoring.users.User;

@Singleton
public class NotificationSenderImpl implements NotificationSender {

	private static Logger log = Logger.getLogger("NotificationSender.java");

	private NotificationServiceAsync CometService;

	@Inject
	public NotificationSenderImpl() {

		CometService = GinWidgetGinjector.INSTANCE.getNotificationServiceAsync();
		log.info("GWT.create(CometService.class) in NotificationSenderImpl;");

	}

	public void sendNewItem(Item item) {
		CometService.addItem(item, new AsyncCallback<Void>() {
			public void onSuccess(Void result) {
				log.info("\n\n   Sent new item to notification");

			}

			public void onFailure(Throwable caught) {
				log.warning(" Failed to send item notification" + caught.getMessage());
			}
		});

	}

	public void sendUpdatedItem(Item item) {
		CometService.updateItem(item, new AsyncCallback<Void>() {
			public void onSuccess(Void result) {
				log.info("\n\n  updated item notification sent");

			}

			public void onFailure(Throwable caught) {
				log.warning(" Failed to  send updated item notification" + caught.getMessage());
			}
		});
	}

	public void sendDeletedItem(Item it) {
		CometService.deleteItem(it, new AsyncCallback<Void>() {
			public void onSuccess(Void result) {
				log.info("\n\n  deleted item notification sent");

			}

			public void onFailure(Throwable caught) {
				log.warning(" Failed to  sent deleted item notification" + caught.getMessage());
			}
		});

	}

	public void sendNewFrame(Frame frame) {
		CometService.addFrame(frame, new AsyncCallback<Void>() {
			public void onSuccess(Void result) {
				log.info("\n\n   Sent new frame to notification");

			}

			public void onFailure(Throwable caught) {
				log.warning(" Failed to send frame notification" + caught.getMessage());
			}
		});
	}

	public void sendUpdatedFrame(Frame updatedFrame) {
		CometService.updateFrame(updatedFrame, new AsyncCallback<Void>() {
			public void onSuccess(Void result) {
				log.info("\n\n  updatedFrame   notification sent");

			}

			public void onFailure(Throwable caught) {
				log.warning(" Failed to  send   updatedFrame notification" + caught.getMessage());
			}
		});
	}

	public void sendDeletedFrame(Frame frame) {
		CometService.deleteFrame(frame, new AsyncCallback<Void>() {
			public void onSuccess(Void result) {
				log.info("\n\n  deleted frame notification sent");

			}

			public void onFailure(Throwable caught) {
				log.warning(" Failed to  send deleted frame notification" + caught.getMessage());
			}
		});
	}

	public void sendUpdatedTopic(Topic updatedTopic) {
		CometService.updateTopic(updatedTopic, new AsyncCallback<Void>() {
			public void onSuccess(Void result) {
				log.info("\n\n  updatedTopic   notification sent");

			}

			public void onFailure(Throwable caught) {
				log.warning(" Failed to  send   updatedTopic notification" + caught.getMessage());
			}
		});
	}

	public void sendNewTopic(Topic topic) {
		CometService.addTopic(topic, new AsyncCallback<Void>() {
			public void onSuccess(Void result) {
				log.info("\n\n   Sent new topic to notification");

			}

			public void onFailure(Throwable caught) {
				log.warning(" Failed to send topic notification" + caught.getMessage());
			}
		});
	}

	public void sendDeletedTopic(Topic topic) {
		CometService.deleteTopic(topic, new AsyncCallback<Void>() {
			public void onSuccess(Void result) {
				log.info("\n\n  deleted topic  notification sent");

			}

			public void onFailure(Throwable caught) {
				log.warning(" Failed to  send deleted topic  notification" + caught.getMessage());
			}
		});
	}

	public void sendUpdatedTheme(Theme updatedTheme) {
		CometService.updateTheme(updatedTheme, new AsyncCallback<Void>() {
			public void onSuccess(Void result) {
				log.info("\n\n  updatedTheme   notification sent");

			}

			public void onFailure(Throwable caught) {
				log.warning(" Failed to  send   updatedTheme notification" + caught.getMessage());
			}
		});
	}

	public void sendNewTheme(Theme theme) {
		CometService.addTheme(theme, new AsyncCallback<Void>() {
			public void onSuccess(Void result) {
				log.info("\n\n   Sent new Theme to notification");

			}

			public void onFailure(Throwable caught) {
				log.warning(" Failed to send Theme notification" + caught.getMessage());
			}
		});
	}

	public void sendDeletedTheme(Theme theme) {
		CometService.deleteTheme(theme, new AsyncCallback<Void>() {
			public void onSuccess(Void result) {
				log.info("\n\n  deleted theme   notification sent");

			}

			public void onFailure(Throwable caught) {
				log.warning(" Failed to  send deleted theme   notification" + caught.getMessage());
			}
		});
	}

	public void sendNewUser(User user) {
		CometService.addUser(user, new AsyncCallback<Void>() {

			public void onSuccess(Void arg0) {
				log.info("\n\n   Sent new user to notification");

			}

			public void onFailure(Throwable caught) {
				log.warning(" Failed to send user notification" + caught.getMessage());

			}
		});

	}

	public void sendUpdatedUser(User user) {
		CometService.updateUser(user, new AsyncCallback<Void>() {

			public void onSuccess(Void arg0) {
				log.info("\n\n  updated user notification sent");

			}

			public void onFailure(Throwable caught) {
				log.warning(" Failed to  sent updated user notification" + caught.getMessage());

			}
		});

	}

	public void sendDeletedUser(String userId) {
		CometService.deleteUser(userId, new AsyncCallback<Void>() {
			public void onSuccess(Void result) {
				log.info("\n\n  deleted user notification sent");

			}

			public void onFailure(Throwable caught) {
				log.warning(" Failed to  sent deleted user notification" + caught.getMessage());
			}
		});
	}

	public void sendupdatedConfiguration(Configuration configuration) {
		CometService.updateConfiguration(configuration, new AsyncCallback<Void>() {
			public void onSuccess(Void result) {
				log.info("\n\n  updated configuration notification sent");

			}

			public void onFailure(Throwable caught) {
				log.warning(" Failed to  send updated configuration  notification" + caught.getMessage());
			}
		});
	}
}
