package eu.dnetlib.client.user;

import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.Style;
import com.google.gwt.user.client.ui.*;
import eu.dnetlib.client.*;

/**
 * Created by stefania on 3/8/15.
 */
public class MyAccountWidget implements MyWidget {

    private String token = "";

    private FlowPanel myAccountPagePanel = new FlowPanel();
    private Label myAccountPageLabel = new Label();

    private MyPersonalInfoWidget myPersonalInfoWidget;
    private MyEligibleProjectsWidget myEligibleProjectsWidget;

    private SidebarPanel recentActivityPanel = new SidebarPanel("My Recent Requests");
    private MyRecentActivityWidget myRecentActivityWidget;

    private SidebarPanel helpPanel = new SidebarPanel("Help");

    public MyAccountWidget() {
        loadContent();
    }

    @Override
    public Widget asWidget() {
        return myAccountPagePanel;
    }

    @Override
    public void clear() {
        myAccountPagePanel.clear();
    }

    @Override
    public void reload() {

        MyWidgetHelper.hideSidebar();

        loadContent();
    }

    @Override
    public void setToken(String token) {
        this.token = token;
    }

    @Override
    public void afterAdditionToRootPanel() {

    }

    private void loadContent() {

        myPersonalInfoWidget = new MyPersonalInfoWidget();

        myAccountPagePanel.addStyleName("content");
        myAccountPagePanel.addStyleName("myAccountPanel");

        myAccountPageLabel.addStyleName("myAccountTitle");
        myAccountPageLabel.addStyleName("contentTitleLabel");
        myAccountPageLabel.setText("Hey, " + GoldOAPortal.currentUser.getName());

        myAccountPagePanel.add(myAccountPageLabel);
        myAccountPagePanel.add(myPersonalInfoWidget.asWidget());

        if(Utils.currentUserHasRoleApproved("library_staff") || Utils.currentUserHasRoleApproved("moderator")
                || Utils.currentUserHasRoleApproved("administrator") || Utils.currentUserHasRoleApproved("researcher")
                || Utils.currentUserHasRoleApproved("project_coordinator") || Utils.currentUserHasRoleApproved("accounting")) {
            myEligibleProjectsWidget = new MyEligibleProjectsWidget();
            myAccountPagePanel.add(myEligibleProjectsWidget.asWidget());
        }

        if(Utils.currentUserHasRoleApproved("library_staff") || Utils.currentUserHasRoleApproved("researcher")
                || Utils.currentUserHasRoleApproved("project_coordinator") || Utils.currentUserHasRoleApproved("publisher")) {

            myRecentActivityWidget = new MyRecentActivityWidget();
            recentActivityPanel.clearContent();
            recentActivityPanel.addContent(myRecentActivityWidget.asWidget());

            MyWidgetHelper.showSidebar();
            RootPanel.get("sidebar").add(recentActivityPanel);
        }

        MyWidgetHelper.loadHelp(helpPanel, token.split("\\.")[0]);
    }
}
