package eu.dnetlib.client;

import com.github.gwtbootstrap.client.ui.Button;
import com.github.gwtbootstrap.client.ui.base.IconAnchor;
import com.github.gwtbootstrap.client.ui.constants.ButtonType;
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.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.History;
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.client.fundingrequest.newrequest.EligibilityDisplayWidget;
import eu.dnetlib.client.fundingrequest.newrequest.EligibilityService;
import eu.dnetlib.client.fundingrequest.newrequest.EligibilityServiceAsync;
import eu.dnetlib.client.widgets.AutoCompleteWidget;
import eu.dnetlib.goldoa.domain.Eligibility;
import eu.dnetlib.goldoa.domain.Project;
import eu.dnetlib.goldoa.domain.Request;
import eu.dnetlib.goldoa.domain.Vocabulary;

/**
 * Created by stefania on 3/6/15.
 */
public class HomeWidget implements MyWidget {

    private FlowPanel homePagePanel = new FlowPanel();

    private FlowPanel mainBlock = new FlowPanel();
    private FlowPanel mainGrid = new FlowPanel();

    private FlowPanel pilotAbout = new FlowPanel();
    private Label pilotAboutTitleLabel = new Label();
    private HTML pilotAboutContent = new HTML();

    private Label needMoreInfoLabel = new Label();
    private HTML needMoreInfoContent = new HTML();

    private FlowPanel projectEligibleAndSignIn = new FlowPanel();
    private Label projectEligibleTitleLabel = new Label();
    private Label projectEligibleInfoLabel = new Label();
    private FlowPanel eligibilityPanel = new FlowPanel();
    private AutoCompleteWidget projectAutoComplete = new AutoCompleteWidget("project", "Search...");
    private HTML projectEligibleCommentLabel = new HTML();

    private FlowPanel viewEligibleProjectsPanel = new FlowPanel();
    private Anchor viewEligibleProjects = new Anchor();

    private FlowPanel signInAndRegister = new FlowPanel();
    private Button signIn = new Button();
    private HTML register = new HTML();

    private FlowPanel secondaryBlock = new FlowPanel();

    private FlowPanel researchersPanel = new FlowPanel();
    private Label researcherTitleLabel = new Label();
    private Label researcherSubTitleLabel = new Label();
    private HTML researcherInfoLabel = new HTML();

    private FlowPanel institutionsPanel = new FlowPanel();
    private Label institutionTitleLabel = new Label();
    private Label institutionSubTitleLabel = new Label();
    private HTML institutionInfoFirstLabel = new HTML();
    private IconAnchor viewAllInstitutions = new IconAnchor();
    private HTML institutionInfoSecondLabel = new HTML();

    private FlowPanel publishersPanel = new FlowPanel();
    private Label publisherTitleLabel = new Label();
    private Label publisherSubTitleLabel = new Label();
    private HTML publisherInfoFirstLabel = new HTML();
    private HTML publisherInfoSecondLabel = new HTML();

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

    private EligibilityServiceAsync eligibilityService = GWT.create(EligibilityService.class);
    private DataServiceAsync dataService = GWT.create(DataService.class);

    public HomeWidget() {

        homePagePanel.addStyleName("homePage");

        mainBlock.addStyleName("content");
        mainGrid.addStyleName("uk-grid");
        mainGrid.addStyleName("uk-grid-divider");

        pilotAbout.addStyleName("uk-width-medium-2-3");

        pilotAboutTitleLabel.addStyleName("contentTitleLabel");
        pilotAboutTitleLabel.setText("Requesting funds for post-grant FP7 publications?");

        pilotAboutContent.addStyleName("contentText");
        pilotAboutContent.setHTML(goldOAConstants.whatIsThisServiceAbout());

        needMoreInfoLabel.addStyleName("contentTitleLabel");
        needMoreInfoLabel.setText("Need more information?");

        needMoreInfoContent.addStyleName("contentText");
        needMoreInfoContent.setHTML(goldOAConstants.needMoreInfoOnThePilot());

        pilotAbout.add(pilotAboutTitleLabel);
        pilotAbout.add(pilotAboutContent);
        pilotAbout.add(needMoreInfoLabel);
        pilotAbout.add(needMoreInfoContent);

        projectEligibleAndSignIn.addStyleName("uk-width-medium-1-3");

        projectEligibleTitleLabel.addStyleName("contentTitleLabel");
        projectEligibleTitleLabel.setText("Is your project eligible?");

        projectEligibleInfoLabel.addStyleName("contentText");
        projectEligibleInfoLabel.setText("Type your FP7 acronym/granted or project name to see if you can request funding for your publication");

        AutoCompleteWidget.AutoCompleteListener projectAutoCompleteListener = new AutoCompleteWidget.AutoCompleteListener() {
            @Override
            public void valueSelected(Vocabulary vocabulary) {

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

                final Request request = new Request();
                request.setProject(vocabulary.getProject());
                request.setStatus(Request.RequestStatus.INCOMPLETE);


                eligibilityService.validate(request, new AsyncCallback<Eligibility>() {

                    @Override
                    public void onFailure(Throwable throwable) {

                        eligibilityPanel.removeStyleName("loading-small");

                        eligibilityPanel.clear();
                        EligibilityDisplayWidget eligibilityDisplayWidget = new EligibilityDisplayWidget(null);
                        eligibilityPanel.add(eligibilityDisplayWidget.asWidget());
                    }

                    @Override
                    public void onSuccess(Eligibility eligibility) {

                        eligibilityPanel.removeStyleName("loading-small");

                        eligibilityPanel.clear();
                        EligibilityDisplayWidget eligibilityDisplayWidget = new EligibilityDisplayWidget(eligibility);
                        eligibilityPanel.add(eligibilityDisplayWidget.asWidget());
                    }
                });
            }
        };
        projectAutoComplete.setAutoCompleteListener(projectAutoCompleteListener);

        projectEligibleCommentLabel.addStyleName("comment");
        projectEligibleCommentLabel.addStyleName("fontItalic");
        projectEligibleCommentLabel.setHTML("If you cannot find your project in our database, do not hesitate to " +
                "<a class=\"contactUs\">contact us</a>");

        viewEligibleProjects.setText("View eligible projects by organization...");
        viewEligibleProjects.setHref("#eligibleProjects");
        viewEligibleProjects.addStyleName("emphasis");
        viewEligibleProjects.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent clickEvent) {
                History.newItem("eligibleProjects");
            }
        });
        viewEligibleProjectsPanel.add(viewEligibleProjects);
        viewEligibleProjectsPanel.addStyleName("viewEligibleProjectsHomeLink");

        signIn.addStyleName("signIn");
        signIn.setText("Sign In");
        signIn.setType(ButtonType.PRIMARY);
        signIn.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent clickEvent) {
                History.newItem("login");
            }
        });

        register.setHTML("<div class=\"register\">New User? <a href=\"#register\">Start here</a></div>");

        signInAndRegister.add(signIn);
        signInAndRegister.add(register);

        projectEligibleAndSignIn.add(projectEligibleTitleLabel);
        projectEligibleAndSignIn.add(projectEligibleInfoLabel);
        projectEligibleAndSignIn.add(projectAutoComplete);
        projectEligibleAndSignIn.add(projectEligibleCommentLabel);
        projectEligibleAndSignIn.add(eligibilityPanel);
        projectEligibleAndSignIn.add(viewEligibleProjectsPanel);

        mainGrid.add(pilotAbout);
        mainGrid.add(projectEligibleAndSignIn);

        mainBlock.add(mainGrid);
        

        researchersPanel.addStyleName("inlineBlock");
        researchersPanel.addStyleName("width25");
        researchersPanel.addStyleName("content");
        researchersPanel.addStyleName("smallContentPanel");

        researcherTitleLabel.addStyleName("contentTitleLabel");
        researcherTitleLabel.setText("Researchers");

        researcherSubTitleLabel.addStyleName("contentTitleLabel");
        researcherSubTitleLabel.setText("How to request APCs");

        researcherInfoLabel.addStyleName("contentText");
        researcherInfoLabel.setHTML(goldOAConstants.infoForResearchers());

        researchersPanel.add(researcherTitleLabel);
        researchersPanel.add(researcherSubTitleLabel);
        researchersPanel.add(researcherInfoLabel);


        institutionsPanel.addStyleName("inlineBlock");
        institutionsPanel.addStyleName("width25");
        institutionsPanel.addStyleName("content");
        institutionsPanel.addStyleName("smallContentPanel");

        institutionTitleLabel.addStyleName("contentTitleLabel");
        institutionTitleLabel.setText("Institutions");

        institutionSubTitleLabel.addStyleName("contentTitleLabel");
        institutionSubTitleLabel.setText("How to get involved");

        institutionInfoFirstLabel.addStyleName("contentText");
        institutionInfoFirstLabel.setHTML(goldOAConstants.infoForInstitutionsFirstText());

        viewAllInstitutions.setText("View all participating institutions...");
        viewAllInstitutions.setHref("https://www.openaire.eu/postgrantoapilot-participants");
        viewAllInstitutions.setTarget("_blank");

        institutionInfoSecondLabel.addStyleName("contentText");
        institutionInfoSecondLabel.setHTML(goldOAConstants.infoForInstitutionsSecondText());

        institutionsPanel.add(institutionTitleLabel);
        institutionsPanel.add(institutionSubTitleLabel);
        institutionsPanel.add(institutionInfoFirstLabel);
        institutionsPanel.add(viewAllInstitutions);
        institutionsPanel.add(institutionInfoSecondLabel);


        publishersPanel.addStyleName("inlineBlock");
        publishersPanel.addStyleName("width25");
        publishersPanel.addStyleName("content");
        publishersPanel.addStyleName("smallContentPanel");

        publisherTitleLabel.addStyleName("contentTitleLabel");
        publisherTitleLabel.setText("Publishers");

        publisherSubTitleLabel.addStyleName("contentTitleLabel");
        publisherSubTitleLabel.setText("Get in contact");

        publisherInfoFirstLabel.addStyleName("contentText");
        publisherInfoFirstLabel.setHTML(goldOAConstants.infoForPublishersFirstText());

        publisherInfoSecondLabel.addStyleName("contentText");
        publisherInfoSecondLabel.setHTML(goldOAConstants.infoForPublishersSecondText());

        publishersPanel.add(publisherTitleLabel);
        publishersPanel.add(publisherSubTitleLabel);
        publishersPanel.add(publisherInfoFirstLabel);
        publishersPanel.add(publisherInfoSecondLabel);


        secondaryBlock.addStyleName("secondaryBlock");
        secondaryBlock.add(researchersPanel);
        secondaryBlock.add(institutionsPanel);
        secondaryBlock.add(publishersPanel);


        homePagePanel.add(mainBlock);
        homePagePanel.add(secondaryBlock);
    }

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

    @Override
    public void clear() {

    }

    @Override
    public void reload() {

        Document.get().getElementById("content").removeClassName("uk-width-medium-3-4");
        Document.get().getElementById("content").addClassName("uk-width-medium-1-1");
        Document.get().getElementById("sidebar").getStyle().setDisplay(Style.Display.NONE);

        if(GoldOAPortal.currentUser==null) {
            projectEligibleAndSignIn.remove(viewEligibleProjectsPanel);
            projectEligibleAndSignIn.add(signInAndRegister);
            projectEligibleAndSignIn.add(viewEligibleProjectsPanel);
        } else {
            projectEligibleAndSignIn.remove(signInAndRegister);
        }
    }

    @Override
    public void setToken(String token) {

    }

    @Override
    public void afterAdditionToRootPanel() {

    }
}
