/*
 * Copyright
 *   2008 axYus - www.axyus.com
 *   2008 C.Bosquet - charles.bosquet@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.search;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.VerticalPanel;
import fr.gouv.finances.dgfip.xemelios.web.gwt.client.utils.GWTUtils;
import fr.gouv.finances.dgfip.xemelios.web.gwt.client.widget.ImageButton;

/**
 * @author CBO
 * Widget contenant les boutons liés à la fenêtre de recherche (bouton d'ajout d'un critère, bouton de lancement de la recherche, ...)
 * @TODO : Corriger bug : quand on clique deux fois rapidement il semble qu'il y a trois evenements déclenchés...
 */
public class ButtonsBarWidget extends Composite {

    private static final transient String SEARCH="Search";
    private static final transient String ADD_CRITERIA="AddSearchCriteria";
    private static final transient String RESET_CRITERIA="ResetSearchCriteria";
    private static final transient String OPEN_SEARCH="OpenSearch";
    private static final transient String SAVE_SEARCH="SaveSearch";
    private static final transient String SORT="Sort";

    private Anchor addCritereButton;
    private Anchor searchButton;
    private Anchor resetCriteresButton;
    private Anchor openSearchButton;
    private Anchor saveRequestButton;
    private Anchor searchParamsButton;

    private SearchWidget.AddCriteriaClickListener addCriteriaClickListener;

    public ButtonsBarWidget() {

        searchButton = createAnchor(SEARCH,SEARCH);
        addCritereButton = createAnchor(ADD_CRITERIA, ADD_CRITERIA);
        resetCriteresButton = createAnchor(RESET_CRITERIA, RESET_CRITERIA);
        openSearchButton = createAnchor(OPEN_SEARCH, OPEN_SEARCH);
        saveRequestButton = createAnchor(SAVE_SEARCH, SAVE_SEARCH);
        searchParamsButton = createAnchor(SORT, SORT);

//        StringBuffer html = new StringBuffer();
//        html.append("<div id=\"SearchTools\" class=\"ToolBar\">");
//            html.append("<div class=\"Left Decorator\"><span style=\"display: none;\"/></div>");
//            html.append("<div class=\"Right Decorator\"><span style=\"display: none;\"/></div>");
//            html.append("<div class=\"Main\">");
//                html.append("<ul class=\"ToolGroups\">");
//                    html.append("<li class=\"ToolGroup Strong\"><span id=\"").append(SEARCH).append("\" name=\"").append(SEARCH).append("\"/></li>");
//                    html.append("<li id=\"criterias\" class=\"ToolGroup\"><span id=\"").append(ADD_CRITERIA).append("\" name=\"").append(ADD_CRITERIA).append("\"/><span id=\"").append(RESET_CRITERIA).append("\" name=\"").append(RESET_CRITERIA).append("\"/></li>");
//                    html.append("<li id=\"saved\" class=\"ToolGroup\"><span id=\"").append(OPEN_SEARCH).append("\" name=\"").append(OPEN_SEARCH).append("\"/><span id=\"").append(SAVE_SEARCH).append("\" name=\"").append(SAVE_SEARCH).append("\"/></li>");
//                    html.append("<li class=\"ToolGroup\"><span id=\"").append(SORT).append("\" name=\"").append(SORT).append("\"/></li>");
//                html.append("</ul>");
//            html.append("</div>");
//        html.append("</div>");
//
//        HTMLPanel pane = new HTMLPanel(html.toString());
//        pane.add(searchButton, SEARCH);
//
//        pane.add(addCritereButton,"criterias");
//        pane.add(resetCriteresButton,"criterias");
//
//        pane.add(openSearchButton,"saved");
//        pane.add(saveRequestButton,"saved");
//        pane.add(searchParamsButton, SORT);
//
//        initWidget(pane);

        FlowPanel mainPanel = new FlowPanel();
        mainPanel.getElement().setId("SearchTools");
        mainPanel.setStyleName("ToolBar");
        HTML div1 = new HTML("<span style=\"display: none\"/>");
        div1.setStyleName("Left Decorator");
        mainPanel.add(div1);
        HTML div2 = new HTML("<span style=\"display: none\"/>");
        div2.setStyleName("Right Decorator");
        mainPanel.add(div2);

        FlowPanel toolGroups = new FlowPanel();
        toolGroups.setStyleName("ToolGroups");

        FlowPanel hpSearch = new FlowPanel();
        hpSearch.setStyleName("ToolGroup Strong");
        hpSearch.add(searchButton);
        toolGroups.add(hpSearch);

        FlowPanel hpCriterias = new FlowPanel();
        hpCriterias.setStyleName("ToolGroup Norm");
        hpCriterias.add(addCritereButton);
        hpCriterias.add(resetCriteresButton);
        toolGroups.add(hpCriterias);

        Image sep = new Image(GWTUtils.getContextPath()+"/img/toolbar-vsep.png");
        sep.setStyleName("separator");

        FlowPanel hpSaved = new FlowPanel();
        hpSaved.setStyleName("ToolGroup Norm");
        hpSaved.add(sep);
        hpSaved.add(openSearchButton);
        hpSaved.add(saveRequestButton);
        toolGroups.add(hpSaved);

        FlowPanel hpSort = new FlowPanel();
        hpSort.setStyleName("ToolGroup Norm");
        Image sep2 = new Image(GWTUtils.getContextPath()+"/img/toolbar-vsep.png");
        sep2.setStyleName("separator");
        hpSort.add(sep2);
        hpSort.add(searchParamsButton);
        toolGroups.add(hpSort);

        mainPanel.add(toolGroups);

        initWidget(mainPanel);

}

    public void addClickListener(ClickListenerType clickListenerType, ClickListener cl) {
        if (clickListenerType.equals(ClickListenerType.ADD_CRITERE)) {
            addCriteriaClickListener = (SearchWidget.AddCriteriaClickListener) cl;
            this.addCritereButton.addClickListener(cl);
        } else if (clickListenerType.equals(ClickListenerType.SEARCH)) {
            this.searchButton.addClickListener(cl);
        } else if (clickListenerType.equals(ClickListenerType.RESET_CRITERES)) {
            this.resetCriteresButton.addClickListener(cl);
        } else if (clickListenerType.equals(ClickListenerType.OPEN_SEARCH)) {
            this.openSearchButton.addClickListener(cl);
        } else if (clickListenerType.equals(ClickListenerType.SAVE_REQUEST)) {
            this.saveRequestButton.addClickListener(cl);
        } else if (clickListenerType.equals(ClickListenerType.SEARCH_PARAMS)) {
            this.searchParamsButton.addClickListener(cl);
        }
    }

    public void setAddCritereEnable(boolean enable) {
        addCriteriaClickListener.setEnabled(enable);
    }

    static public enum ClickListenerType {

        ADD_CRITERE,
        SEARCH,
        RESET_CRITERES,
        OPEN_SEARCH,
        SAVE_REQUEST,
        SEARCH_PARAMS
    }

    private static Anchor createAnchor(String styleName, String id) {
        Anchor a = new Anchor();
        a.setStylePrimaryName("Icon");
        a.addStyleName(styleName);
        a.getElement().setId(id);
        return a;
    }
}
