/*
 * Copyright
 *   2008 axYus - www.axyus.com
 *   2008 C.Marchand - christophe.marchand@axyus.com
 *
 * This file is part of XEMELIOS.
 *
 * XEMELIOS is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * XEMELIOS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with XEMELIOS; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package fr.gouv.finances.dgfip.xemelios.web.gwt.client.widget;

import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.KeyboardListenerAdapter;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import fr.gouv.finances.dgfip.xemelios.web.gwt.client.Displayable;
import fr.gouv.finances.dgfip.xemelios.web.gwt.client.dto.SearchParameterDTO;
import fr.gouv.finances.dgfip.xemelios.web.gwt.client.utils.GWTUtils;
import fr.gouv.finances.dgfip.xemelios.web.gwt.client.widget.search.SearchWidget;
import java.util.ArrayList;
import java.util.List;

/**
 * Une boite proposant la liste des requetes enregistrées écrasables, ou un champ pour saisir un nouveau nom
 * @author chm
 */
public class SaveRequestDialog extends DialogBox {
    private VerticalPanel itemContainer;
//    private SelectListenerCollection selectListeners;
    private TextBox requestName;
    private String requestId = null;
    private SearchWidget parent;
    private List<SearchParameterDTO> parameters;

    public SaveRequestDialog(SearchWidget parent, List<SearchParameterDTO> parameters) {
        super(false,true);
//        super("Enregistrement recherche","350","200");
        this.parent=parent;
        this.parameters=parameters;
        setText("Enregistrement recherche");
//        this.selectListeners = new SelectListenerCollection();
        VerticalPanel mainPanel = new VerticalPanel();
        mainPanel.add(new HTML("Saisissez le nom d'une nouvelle recherche, ou<br/>s&eacute;lectionnez une recherche existante pour la modifier.",true));
        requestName = new TextBox();
        requestName.addKeyboardListener(new KeyboardListenerAdapter() {
            @Override
                public void onKeyPress(Widget sender, char keyCode, int modifiers) {
                    setRequestId(null);
                }
            }
        );
        requestName.setMaxLength(250);
        requestName.setWidth("300");
        mainPanel.add(requestName);
        ScrollPanel scrollPanel = new ScrollPanel();
        scrollPanel.setSize("350", "300");
        scrollPanel.addStyleName("bordered");
        this.itemContainer = new VerticalPanel();
        scrollPanel.add(itemContainer);
        mainPanel.add(scrollPanel);
        // Les boutons Annuler et Valider :
        mainPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
        mainPanel.add(createButtonsPanel());
        // Le widget Principal :
        setWidget(mainPanel);
        show(); center();
    }
    private void setRequestName(String name) {
        requestName.setText(name);
    }
    private void setRequestId(String id) { requestId = id; }


    private Widget createButtonsPanel() {
        HorizontalPanel horizontalPanel = new HorizontalPanel();
        Button okButton = new Button();
        okButton.setText("Ok");
        okButton.addClickListener(new ClickListener() {

            public void onClick(Widget sender) {
                ok();
            }
        });
        horizontalPanel.add(okButton);

        Button cancelButton = new Button();
        cancelButton.setText("Annuler");
        cancelButton.addClickListener(new ClickListener() {

            public void onClick(Widget sender) {
                cancel();
            }
        });
        horizontalPanel.add(cancelButton);
        return horizontalPanel;
    }

    private void cancel() {
        hide();
    }
    private void ok() {
        cancel();
        parent.executeSaveRequest(parameters,requestName.getText(),requestId);
    }

    public void setItems(List<? extends Displayable> items) {
        this.itemContainer.clear();
        for (final Displayable item : items) {
            ImageHLink hyperlink = new ImageHLink(item);
            hyperlink.addClickListener(new ClickListener() {

                public void onClick(Widget sender) {
                    setRequestName(item.getLibelle());
                    setRequestId(item.getId());
                }
            });
            itemContainer.add(hyperlink);
        }
    }

    static private class ImageHLink extends HorizontalPanel {
        private Image image;
        private Hyperlink hlink;
        public ImageHLink(Displayable dd) {
            String imageUrl = GWTUtils.getContextPath().concat("/img/empty.png");
            if(dd.getResource()!=null) {
                imageUrl = GWTUtils.getContextPath().concat("/img/").concat(dd.getResource());
            }
            image = new Image(imageUrl);
            add(image);
            hlink = new Hyperlink();
            hlink.setText(dd.getLibelle());
            add(hlink);
        }
        public void addClickListener(ClickListener cl) {
            hlink.addClickListener(cl);
            image.addClickListener(cl);
        }
    }

}
