package eu.dnetlib.client.adminpanel;

import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.Style;
import com.google.gwt.query.client.Function;
import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.Window;
import eu.dnetlib.espas.gui.shared.User;

import static com.google.gwt.query.client.GQuery.$;

public class MenuController {
	
	private static MenuController instance = null;
	
	private MenuController() {}
	
	public static final MenuController getInstance() {
		
		if(instance==null)
			instance = new MenuController();
		
		return instance;
	}
	
	public void addMenuLinkHandlers() {

        $("#dashboardMenu").click(new Function() {

            public boolean f(Event e) {

                Document.get().getBody().setId("dashboard");

                setMenuBarNotFixed();
                setAllMenusInactive();
                $("#dashboardMenu").addClass("active");

                AdminPanelController.getInstance().showWidget(DashboardWidget.getInstance());
                return false;
            }
        });

        $("#performanceIndicators").click(new Function() {

            public boolean f(Event e) {

                Document.get().getBody().setId("performanceIndicators");

                setMenuBarNotFixed();
                setAllMenusInactive();
                $("#performanceIndicators").addClass("active");

                AdminPanelController.getInstance().showWidget(PerformanceIndicatorsWidget.getInstance());
                return false;
            }
        });
		
		$("#manageUsers").click(new Function() {
			
			public boolean f(Event e) {

                Document.get().getBody().setId("users");

                setMenuBarFixed();
                setAllMenusInactive();
                $("#manageUsers").addClass("active");
				
				AdminPanelController.getInstance().showWidget(ManageUsersWidget.getInstance());
				return false;
			}
		});
		
		$("#manageRoles").click(new Function() {
			
			public boolean f(Event e) {

                Document.get().getBody().setId("roles");

                setMenuBarFixed();
                setAllMenusInactive();
                $("#manageRoles").addClass("active");
				
				AdminPanelController.getInstance().showWidget(ManageRolesWidget.getInstance());
				return false;
			}
		});

		$("#editDataProviderDetails").click(new Function() {
				
			public boolean f(Event e) {

                Document.get().getBody().setId("dataProviders");

                setMenuBarFixed();
                setAllMenusInactive();
                $("#editDataProviderDetails").addClass("active");
				
				AdminPanelController.getInstance().showWidget(ManageDataProviderWidget.getInstance());
				return false;
			}
		});	
		
		$("#harvestDataProvider").click(new Function() {
			
			public boolean f(Event e) {

                Document.get().getBody().setId("harvest");

                setMenuBarNotFixed();
                setAllMenusInactive();
                $("#harvestDataProvider").addClass("active");
				
				AdminPanelController.getInstance().showWidget(HarvestDataProviderWidget.getInstance());
				return false;
			}
		});
		
		
		$("#viewHarvestHistory").click(new Function() {
			
			public boolean f(Event e) {

                Document.get().getBody().setId("harvestHistory");

                setMenuBarNotFixed();
                setAllMenusInactive();
                $("#viewHarvestHistory").addClass("active");
				
				AdminPanelController.getInstance().showWidget(HarvestHistoryWidget.getInstance());
				return false;
			}
		});

        $("#newsAndAnnouncements").click(new Function() {

            public boolean f(Event e) {

                Document.get().getBody().setId("news");

                setMenuBarFixed();
                setAllMenusInactive();
                $("#newsAndAnnouncements").addClass("active");

                AdminPanelController.getInstance().showWidget(NewsAndAnnouncementsWidget.getInstance());

                return false;
            }
        });

        $("#manageTopics").click(new Function() {

            public boolean f(Event e) {

                Document.get().getBody().setId("faqTopics");

                setMenuBarFixed();
                setAllMenusInactive();
                $("#manageTopics").addClass("active");

                AdminPanelController.getInstance().showWidget(FAQTopicsWidget.getInstance());

                return false;
            }
        });

        $("#manageQuestions").click(new Function() {

            public boolean f(Event e) {

                Document.get().getBody().setId("faqQuestions");

                setMenuBarFixed();
                setAllMenusInactive();
                $("#manageQuestions").addClass("active");

                AdminPanelController.getInstance().showWidget(FAQQuestionsWidget.getInstance());

                return false;
            }
        });

        $("#adminActions").click(new Function() {

            public boolean f(Event e) {

                Document.get().getBody().setId("steps");

                setMenuBarNotFixed();
                setAllMenusInactive();
                $("#adminActions").addClass("active");

                AdminPanelController.getInstance().showWidget(ActionsWidget.getInstance());
                return true;
            }
        });

        $("#actions").click(new Function() {

            public boolean f(Event e) {

                Document.get().getBody().setId("steps");

                setMenuBarNotFixed();
                setAllMenusInactive();
                $("#adminActions").addClass("active");

                AdminPanelController.getInstance().showWidget(ActionsWidget.getInstance());
                return true;
            }
        });

        $("#actions").hover(new Function() {
            @Override
            public boolean f(Event e) {

                Document.get().getElementById("adminActionsMenu").getStyle().setDisplay(Style.Display.BLOCK);
                return false;
            }
        }, new Function() {
            @Override
            public boolean f(Event e) {

                Document.get().getElementById("adminActionsMenu").getStyle().setDisplay(Style.Display.NONE);
                return false;
            }
        });

        $("#adminActionsMenu").hover(new Function() {
            @Override
            public boolean f(Event e) {

                Document.get().getElementById("adminActionsMenu").getStyle().setDisplay(Style.Display.BLOCK);
                return false;
            }
        }, new Function() {
            @Override
            public boolean f(Event e) {

                Document.get().getElementById("adminActionsMenu").getStyle().setDisplay(Style.Display.NONE);
                return false;
            }
        });
		
		$("#logout").click(new Function() {
			
			public boolean f(Event e) {
				User.currentUser = null;
				Cookies.removeCookie("currentUser");
				Window.Location.reload();
				return false;
			}
		});
	}

    private void setAllMenusInactive() {

        $("#dashboardMenu").removeClass("active");
        $("#performanceIndicators").removeClass("active");
        $("#newsAndAnnouncements").removeClass("active");
        $("#adminActions").removeClass("active");
        $("#manageUsers").removeClass("active");
        $("#manageRoles").removeClass("active");
        $("#editDataProviderDetails").removeClass("active");
        $("#harvestDataProvider").removeClass("active");
        $("#viewHarvestHistory").removeClass("active");
        $("#manageTopics").removeClass("active");
        $("#manageQuestions").removeClass("active");
    }

    private void setMenuBarNotFixed() {

        $("#menubar").removeClass("fixed");
    }

    private void setMenuBarFixed() {

        $("#menubar").addClass("fixed");
    }
}
