package eu.dnetlib.client;

import com.github.gwtbootstrap.client.ui.Alert;
import com.github.gwtbootstrap.client.ui.constants.AlertType;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.Style;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.*;
import eu.dnetlib.goldoa.domain.Affiliation;
import eu.dnetlib.goldoa.domain.Budget;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by stefania on 4/6/15.
 */
public class ExistingBudgetRequestsWidget implements MyWidget {

    private String token = "";

    private FlowPanel existingBudgetsPagePanel = new FlowPanel();
    private Label existingBudgetsTitleLabel = new Label();
    private Label existingBudgetsInfoLabel = new Label();

    private Alert errorLabel = new Alert();
    private Alert warningLabel = new Alert();

    private FlowPanel budgetsForApprovalPanel = new FlowPanel();

    private DateTimeFormat dtf = DateTimeFormat.getFormat("yyyy/MM/dd");
    private DataServiceAsync dataService = GWT.create(DataService.class);

    private GoldOAConstants goldOAConstants = GWT.create(GoldOAConstants.class);

    public ExistingBudgetRequestsWidget() {

        existingBudgetsPagePanel.addStyleName("content");

        existingBudgetsTitleLabel.setText("Existing Budgets");
        existingBudgetsTitleLabel.addStyleName("contentTitleLabel");

        existingBudgetsInfoLabel.setText("View all your budget requests and the amount of money left in each one.");
        existingBudgetsInfoLabel.addStyleName("contentInfoLabel");

        errorLabel.addStyleName("alertLabel");
        errorLabel.setType(AlertType.ERROR);
        errorLabel.setClose(false);
        errorLabel.setVisible(false);

        warningLabel.addStyleName("alertLabel");
        warningLabel.setType(AlertType.WARNING);
        warningLabel.setClose(false);
        warningLabel.setVisible(false);

        budgetsForApprovalPanel.addStyleName("budgetsListPanel");

        existingBudgetsPagePanel.add(existingBudgetsTitleLabel);
        existingBudgetsPagePanel.add(existingBudgetsInfoLabel);
        existingBudgetsPagePanel.add(errorLabel);
        existingBudgetsPagePanel.add(warningLabel);
        existingBudgetsPagePanel.add(budgetsForApprovalPanel);
    }

    @Override
    public void clear() {

        errorLabel.setVisible(false);
        warningLabel.setVisible(false);
        budgetsForApprovalPanel.clear();
    }

    @Override
    public void reload() {

        MyWidgetHelper.hideSidebar();

        SidebarPanel helpPanel = new SidebarPanel("Help");
        MyWidgetHelper.loadHelp(helpPanel, token.split("\\.")[0]);

        loadBudgets();
    }

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

    @Override
    public void afterAdditionToRootPanel() {

    }

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

    private void loadBudgets() {

        errorLabel.setVisible(false);
        warningLabel.setVisible(false);

        final HTML loadingWheel = new HTML("<div class=\"loader-big\"></div><div class=\"whiteFilm\"></div>");
        budgetsForApprovalPanel.addStyleName("loading");
        budgetsForApprovalPanel.add(loadingWheel);

        if(Utils.currentUserHasRole("library_staff")) {

            List<String> organizationIds = new ArrayList<>();
            for(Affiliation affiliation : GoldOAPortal.currentUser.getAffiliations()) {
                if(affiliation.getOrganization()!=null) {
                    organizationIds.add(affiliation.getOrganization().getId());
                }
            }

            dataService.getBudgetsForOrganizations(organizationIds, new AsyncCallback<List<Budget>>() {

                @Override
                public void onFailure(Throwable throwable) {

                    budgetsForApprovalPanel.clear();
                    budgetsForApprovalPanel.removeStyleName("loading");

                    errorLabel.setText(goldOAConstants.errorGettingListOfBudgets());
                    errorLabel.setVisible(true);
                }

                @Override
                public void onSuccess(List<Budget> budgetInfoList) {

                    budgetsForApprovalPanel.clear();
                    budgetsForApprovalPanel.removeStyleName("loading");

                    if (budgetInfoList.isEmpty()) {
                        warningLabel.setText(goldOAConstants.warningNoBudgetsAvailable());
                        warningLabel.setVisible(true);
                    } else {
                        for (int i = 0; i < budgetInfoList.size(); i++)
                            drawBudgetInfo(budgetInfoList.get(i), i);
                    }
                }
            });

        } else {

            dataService.getBudgetsForUser(GoldOAPortal.currentUser.getEmail(), new AsyncCallback<List<Budget>>() {

                @Override
                public void onFailure(Throwable throwable) {

                    budgetsForApprovalPanel.clear();
                    budgetsForApprovalPanel.removeStyleName("loading");

                    errorLabel.setText(goldOAConstants.errorGettingListOfBudgets());
                    errorLabel.setVisible(true);
                }

                @Override
                public void onSuccess(List<Budget> budgetInfoList) {

                    budgetsForApprovalPanel.clear();
                    budgetsForApprovalPanel.removeStyleName("loading");

                    if (budgetInfoList.isEmpty()) {
                        warningLabel.setText(goldOAConstants.warningNoBudgetsAvailable());
                        warningLabel.setVisible(true);
                    } else {

                        for (int i = 0; i < budgetInfoList.size(); i++)
                            drawBudgetInfo(budgetInfoList.get(i), i);
                    }
                }
            });
        }
    }

    private void drawBudgetInfo(Budget budgetInfo, int i) {

        FlowPanel budgetElement = new FlowPanel();
        budgetElement.addStyleName("budgetRequest");
        if(((i+1)%2)==1)
            budgetElement.addStyleName("odd");

        BudgetInfoElement budgetInfoElement = new BudgetInfoElement(budgetInfo);

        budgetElement.add(budgetInfoElement.asWidget());
        budgetsForApprovalPanel.add(budgetElement);
    }
}
