package com.casamind.adware.client.presenter;
import com.casamind.adware.client.event.NavigationEvent;
import com.casamind.adware.client.event.UserMessageEvent;
import com.casamind.adware.client.helper.RPCCall;
import com.casamind.adware.client.resources.ErrorMessages;
import com.casamind.adware.client.service.DataServiceAsync;
import com.casamind.adware.client.view.OrderView;
import com.casamind.adware.shared.InfoTypes;
import com.casamind.adware.shared.Pages;
import com.casamind.adware.shared.model.UserAccountDTO;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.shared.SimpleEventBus;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.DeckPanel;
import com.google.gwt.user.client.ui.Frame;
import com.google.gwt.user.client.ui.Widget;
public class OrderPresenter implements Presenter {
private UserAccountDTO user;
private final DataServiceAsync rpcService;
private final SimpleEventBus eventBus;
private final Display display;
private final ErrorMessages errorMessages = GWT.create(ErrorMessages.class);
public interface Display {
HasClickHandlers getStatisticsLink();
HasClickHandlers getConsoleLink();
HasClickHandlers getCalendarLink();
Frame getCalendarFrame();
Widget asWidget();
}
public OrderPresenter(DataServiceAsync rpcService, SimpleEventBus eventBus, Display display) {
this.rpcService = rpcService;
this.eventBus = eventBus;
this.display = display;
bind();
}
public OrderPresenter(final DataServiceAsync rpcService, SimpleEventBus eventBus, Display display, UserAccountDTO user) {
this(rpcService, eventBus, display);
this.user = user;
setCalendarFrame();
}
private void setCalendarFrame() {
new RPCCall<String>() {
@Override
protected void callService(AsyncCallback<String> cb) {
rpcService.getAdvertizingSlotsFrameURL(cb);
}
@Override
public void onSuccess(String result) {
if (result == null)
return;
display.getCalendarFrame().setUrl(result);
}
@Override
public void onFailure(Throwable caught) {
eventBus.fireEvent(new UserMessageEvent(errorMessages.rpcError(caught.getMessage()), InfoTypes.Error, 0));
}
}.retry(3);
}
private void bind() {
this.display.getCalendarLink().addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
eventBus.fireEvent(new NavigationEvent(Pages.Calendar));
}
});
this.display.getConsoleLink().addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
eventBus.fireEvent(new NavigationEvent(Pages.Console));
}
});
this.display.getStatisticsLink().addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
eventBus.fireEvent(new NavigationEvent(Pages.Statistics));
}
});
}
@Override
public void go(DeckPanel container) {
for (int i = 0; i < container.getWidgetCount(); i++) {
if (!Pages.isHomePage(container.getWidget(i).getClass()))
container.remove(i);
}
for (int i = 0; i < container.getWidgetCount(); i++) {
if (container.getWidget(i).getClass().equals(OrderView.class)) {
container.showWidget(i);
return;
}
}
container.add(display.asWidget());
container.showWidget(container.getWidgetIndex(display.asWidget()));
}
}