package eu.dnetlib.client.widgets;

import com.github.gwtbootstrap.client.ui.*;
import com.github.gwtbootstrap.client.ui.constants.AlertType;
import com.github.gwtbootstrap.client.ui.constants.AlternateSize;
import com.github.gwtbootstrap.client.ui.constants.BackdropType;
import com.github.gwtbootstrap.client.ui.constants.ButtonType;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
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 eu.dnetlib.goldoa.domain.CommentTemplate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by stefania on 11/17/15.
 */
public class ConfirmActionModal {

    private Modal actionConfirmationModal = new Modal();

    private FlowPanel actionConfirmationPanel = new FlowPanel();

    private Alert errorLabel = new Alert();

    private Form commentsForm = new Form();
    private ListBox commentTemplatesListBox = new ListBox();
    private TextArea comments = new TextArea();

    private ModalFooter modalFooter = new ModalFooter();
    private FlowPanel actionButtons = new FlowPanel();
    private Button cancelButton = new Button();
    private Button okButton = new Button();

    private Map<String, CommentTemplate> commentTemplatesMap = new HashMap<>();

    private ActionConfirmedListener actionConfirmedListener;

    public ConfirmActionModal(String action, final List<CommentTemplate> commentTemplates) {

        actionConfirmationModal.setTitle(action + " Confirmation");

        actionConfirmationModal.add(actionConfirmationPanel);

        errorLabel.setType(AlertType.ERROR);
        errorLabel.setVisible(false);
        errorLabel.setClose(false);

        actionConfirmationPanel.add(errorLabel);

        actionConfirmationPanel.add(commentsForm);

        if(commentTemplates!=null && !commentTemplates.isEmpty()) {

            for(CommentTemplate commentTemplate : commentTemplates) {
                commentTemplatesListBox.addItem(commentTemplate.getName(), commentTemplate.getId());
                commentTemplatesMap.put(commentTemplate.getId(), commentTemplate);
            }

            commentTemplatesListBox.setAlternateSize(AlternateSize.XXLARGE);
            commentsForm.add(new FormFieldSet("Reason (*)", commentTemplatesListBox));

            commentTemplatesListBox.addChangeHandler(new ChangeHandler() {

                @Override
                public void onChange(ChangeEvent event) {
                    comments.setValue(commentTemplatesMap.get(commentTemplatesListBox.getSelectedValue()).getComment());
                }
            });

            comments.setValue(commentTemplatesMap.get(commentTemplatesListBox.getValue(0)).getComment());
        }

        comments.setAlternateSize(AlternateSize.XXLARGE);
        commentsForm.add(new FormFieldSet("Comment (*)", comments));

        actionButtons.addStyleName("confirmationModalButtons");

        modalFooter.add(actionButtons);
        actionConfirmationModal.add(modalFooter);

        cancelButton.setText("Cancel");
        cancelButton.setType(ButtonType.DEFAULT);
        cancelButton.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                hide();
            }
        });
        actionButtons.add(cancelButton);


        okButton.setText("Ok");
        okButton.setType(ButtonType.PRIMARY);
        okButton.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {

                errorLabel.setVisible(false);

                if(comments.getValue()==null || comments.getValue().trim().isEmpty()) {
                    errorLabel.setVisible(true);
                    errorLabel.setText("Providing a comment is mandatory");
                } else {
                    if(actionConfirmedListener!=null) {
                        if(commentTemplates!=null && !commentTemplates.isEmpty())
                            actionConfirmedListener.actionConfirmed(comments.getValue().trim(), commentTemplatesListBox.getSelectedValue());
                        else
                            actionConfirmedListener.actionConfirmed(comments.getValue().trim(), null);
                    }
                    hide();
                }
            }
        });
        actionButtons.add(okButton);

        actionConfirmationModal.addStyleName("confirmationModal");
        actionConfirmationModal.setAnimation(true);
        actionConfirmationModal.setBackdrop(BackdropType.STATIC);
    }

    public void show() {
        actionConfirmationModal.show();
    }

    public void hide() {
        actionConfirmationModal.hide();
        actionConfirmationModal.removeFromParent();
    }

    public interface ActionConfirmedListener {
        void actionConfirmed(String comment, String templateId);
    }

    public void setActionConfirmedListener(ActionConfirmedListener actionConfirmedListener) {
        this.actionConfirmedListener = actionConfirmedListener;
    }
}
