package eu.dnetlib.client.widgets;

import com.github.gwtbootstrap.client.ui.base.IconAnchor;
import com.github.gwtbootstrap.client.ui.constants.IconType;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.Widget;
import eu.dnetlib.goldoa.domain.RequestCoFunder;

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

/**
 * Created by stefania on 5/7/15.
 */
public class MultipleCoFundersWidget implements IsWidget {

    private boolean hasInvalid = false;

    private FlowPanel multipleCoFundersPanel = new FlowPanel();
    private List<CoFunderInfoWidget> coFunderInfoWidgetList = new ArrayList<>();

    private IconAnchor addMore = new IconAnchor();

    public MultipleCoFundersWidget() {

        final CoFunderInfoWidget coFunderInfoWidget = new CoFunderInfoWidget();
        coFunderInfoWidgetList.add(coFunderInfoWidget);
        multipleCoFundersPanel.add(coFunderInfoWidget.asWidget());
        CoFunderInfoWidget.DeleteCoFunderListener deleteAffiliationListener = new CoFunderInfoWidget.DeleteCoFunderListener() {
            @Override
            public void deleteCoFunder() {
                coFunderInfoWidgetList.remove(coFunderInfoWidget);
                multipleCoFundersPanel.remove(coFunderInfoWidget.asWidget());
            }
        };
        coFunderInfoWidget.setDeleteCoFunderListener(deleteAffiliationListener);

        addMore.setIcon(IconType.PLUS);
        addMore.setText("Add another co-funder");
        addMore.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent clickEvent) {

                final CoFunderInfoWidget coFunderInfoWidget = new CoFunderInfoWidget();
                coFunderInfoWidgetList.add(coFunderInfoWidget);
                multipleCoFundersPanel.insert(coFunderInfoWidget.asWidget(), multipleCoFundersPanel.getWidgetIndex(addMore));
                CoFunderInfoWidget.DeleteCoFunderListener deleteAffiliationListener = new CoFunderInfoWidget.DeleteCoFunderListener() {
                    @Override
                    public void deleteCoFunder() {
                        coFunderInfoWidgetList.remove(coFunderInfoWidget);
                        multipleCoFundersPanel.remove(coFunderInfoWidget.asWidget());
                    }
                };
                coFunderInfoWidget.setDeleteCoFunderListener(deleteAffiliationListener);
            }
        });
        multipleCoFundersPanel.add(addMore);
    }

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

    public List<RequestCoFunder> getCoFunders() {

        hasInvalid = false;

        List<RequestCoFunder> coFunderList = new ArrayList<>();

        for(CoFunderInfoWidget coFunderInfoWidget : coFunderInfoWidgetList) {

            if(coFunderInfoWidget.getCoFunder()!=null) {
                if(!coFunderInfoWidget.isValid())
                    hasInvalid = true;
                coFunderList.add(coFunderInfoWidget.getCoFunder());
            }
        }

        return coFunderList;
    }

    public void clear() {

        multipleCoFundersPanel.clear();
        coFunderInfoWidgetList = new ArrayList<>();

        final CoFunderInfoWidget coFunderInfoWidget = new CoFunderInfoWidget();
        coFunderInfoWidgetList.add(coFunderInfoWidget);
        multipleCoFundersPanel.add(coFunderInfoWidget.asWidget());
        CoFunderInfoWidget.DeleteCoFunderListener deleteAffiliationListener = new CoFunderInfoWidget.DeleteCoFunderListener() {
            @Override
            public void deleteCoFunder() {
                coFunderInfoWidgetList.remove(coFunderInfoWidget);
                multipleCoFundersPanel.remove(coFunderInfoWidget.asWidget());
            }
        };
        coFunderInfoWidget.setDeleteCoFunderListener(deleteAffiliationListener);
        multipleCoFundersPanel.add(addMore);
    }

    public void loadCoFunders(List<RequestCoFunder> coFunderList) {

        if(!coFunderList.isEmpty()) {
            multipleCoFundersPanel.clear();
            coFunderInfoWidgetList = new ArrayList<>();

            for (RequestCoFunder requestCoFunder : coFunderList) {
                final CoFunderInfoWidget coFunderInfoWidget = new CoFunderInfoWidget();
                coFunderInfoWidgetList.add(coFunderInfoWidget);
                multipleCoFundersPanel.add(coFunderInfoWidget.asWidget());
                CoFunderInfoWidget.DeleteCoFunderListener deleteAffiliationListener = new CoFunderInfoWidget.DeleteCoFunderListener() {
                    @Override
                    public void deleteCoFunder() {
                        coFunderInfoWidgetList.remove(coFunderInfoWidget);
                        multipleCoFundersPanel.remove(coFunderInfoWidget.asWidget());
                    }
                };
                coFunderInfoWidget.setDeleteCoFunderListener(deleteAffiliationListener);
                coFunderInfoWidget.loadCoFunder(requestCoFunder);
            }
            multipleCoFundersPanel.add(addMore);
        }
    }

    public boolean hasInvalid() {
        return hasInvalid;
    }
}
