/*
 * Copyright
 *   2007 axYus - www.axyus.com
 *   2010 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 Lesser General Public License as published by
 * the Free Software Foundation; either version 3 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser 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.navigate;

import com.google.gwt.dom.client.Element;
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.FlexTable;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTMLTable;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.InlineHTML;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.SourcesTableEvents;
import com.google.gwt.user.client.ui.TableListener;
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.dto.LabelValueDTO;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Christophe Marchand
 * Permet de choisir un élément dans une liste
 */
public class DlgChooseOnePair extends DialogBox implements TableListener {

    private FlexTable itemContainer;
    private SelectListenerCollection selectListeners;

    public DlgChooseOnePair(String title) {
        setText(title);
        this.selectListeners = new SelectListenerCollection();
        VerticalPanel mainPanel = new VerticalPanel();
        Grid header = new Grid(1,2);
        header.setWidth("100%");
        header.setCellSpacing(0);
        header.setStyleName("table-collapsed");
        header.setWidget(0,0, new InlineHTML("Code"));
        header.setWidget(0,1, new InlineHTML("Libell&eacute;"));
        HTMLTable.CellFormatter cf = header.getCellFormatter();
        cf.setAlignment(0, 0, HasHorizontalAlignment.ALIGN_CENTER, HasVerticalAlignment.ALIGN_MIDDLE);
        cf.setWidth(0, 0, "50%");
        cf.setStyleName(0, 0, "cell-header");
        cf.setAlignment(0, 1, HasHorizontalAlignment.ALIGN_CENTER, HasVerticalAlignment.ALIGN_MIDDLE);
        cf.setStyleName(0, 1, "cell-header");
        cf.setWidth(0, 1, "50%");
        mainPanel.add(header);
        ScrollPanel scrollPanel = new ScrollPanel();
        scrollPanel.setSize("350", "200");
        this.itemContainer = new FlexTable();
        itemContainer.setWidth("100%");
        itemContainer.addTableListener(this);
        itemContainer.setStyleName("table-collapsed");
        itemContainer.setCellSpacing(0);
        scrollPanel.add(itemContainer);
        mainPanel.add(scrollPanel);
        // Les boutons Annuler et Valider :
        mainPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
        mainPanel.add(createButtonsPanel());
        // Le widget Principal :
        setWidget(mainPanel);
    }

    public void addSelectListener(SelectListener selectListener) {
        this.selectListeners.add(selectListener);
    }

    private Widget createButtonsPanel() {
        HorizontalPanel horizontalPanel = new HorizontalPanel();
        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();
    }

    public void setItems(List<? extends LabelValueDTO> items) {
        this.itemContainer.clear();
        for (final LabelValueDTO item : items) {
            int row = itemContainer.getRowCount();
            itemContainer.insertRow(row);
            itemContainer.addCell(row);
            itemContainer.addCell(row);
            HTMLTable.CellFormatter cf = itemContainer.getCellFormatter();
            cf.setStyleName(row, 0, "clickable-cell");
            cf.setWidth(row, 0, "50%");
            cf.setWidth(row, 1, "50%");
            cf.setStyleName(row, 1, "clickable-cell");
            itemContainer.setWidget(row, 0, new InlineHTML(item.getId()));
            itemContainer.setWidget(row, 1, new InlineHTML(item.getLibelle()));
            itemContainer.getWidget(row, 0).getElement().setAttribute("code", item.getId());
            itemContainer.getWidget(row, 1).getElement().setAttribute("code", item.getLibelle());
        }
    }

    public void onCellClicked(SourcesTableEvents sender, int row, int cell) {
        selectListeners.fireSelect(new LabelValueDTO(itemContainer.getWidget(row, 0).getElement().getAttribute("code"), itemContainer.getWidget(row, 1).getElement().getAttribute("code")));
        hide();
    }

    static public interface SelectListener {

        void onSelect(Object selectedItem);
    }

    static public class SelectListenerCollection extends ArrayList<SelectListener> {

        public void fireSelect(Object selectedItem) {
            for (SelectListener listener : this) {
                listener.onSelect(selectedItem);
            }
        }
    }

}
