/*
 * Copyright
 *   2007 axYus - www.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.search;

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.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import fr.gouv.finances.dgfip.xemelios.web.gwt.client.utils.GWTUtils;

/**
 * @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 static final transient String REPOSITORY="Preferences";
    private static final transient String REPOSITORY_DEACTIVATED = "Preferences-deactivated";

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

    private SearchWidget.AddCriteriaClickListener addCriteriaClickListener;

    public ButtonsBarWidget() {

        searchButton = createAnchor(SEARCH,SEARCH,new HTML("Chercher").getHTML());
        addCritereButton = createAnchor(ADD_CRITERIA, ADD_CRITERIA,new HTML("Ajouter un ctrit&egrave;re").getHTML());
        resetCriteresButton = createAnchor(RESET_CRITERIA, RESET_CRITERIA,new HTML("Supprimer tous les crit&egrave;res").getHTML());
        openSearchButton = createAnchor(OPEN_SEARCH, OPEN_SEARCH,new HTML("Ouvrir une recherche pr&eacute;-enregistr&eacute;e").getHTML());
        saveRequestButton = createAnchor(SAVE_SEARCH, SAVE_SEARCH,new HTML("Enregistrer les crit&egrave;res de recherche").getHTML());
        searchParamsButton = createAnchor(SORT, SORT,new HTML("Champs et tris").getHTML());
        repositoryButton = createAnchor(REPOSITORY, REPOSITORY, new HTML("Nomenclature").getHTML());

        HorizontalPanel mainPanel = new HorizontalPanel();
        mainPanel.getElement().setId("SearchTools");
        mainPanel.setStyleName("ToolBar");

        HorizontalPanel toolGroups = new HorizontalPanel();
        toolGroups.setStyleName("ToolGroups");
//        Window.alert(GWTUtils.getBrowserName()+" - "+GWTUtils.getBrowserVersion());
        toolGroups.add(new Image(GWTUtils.getContextPath()+"/img/toolbar-left-strong.gif"));

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

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

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

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

        HorizontalPanel hpRepository = new HorizontalPanel();
        hpRepository.setStyleName("ToolGroup Norm hasBackground");
        Image sep3 = new Image(GWTUtils.getContextPath()+"/img/toolbar-vsep.png");
        sep3.setStyleName("separator");
        hpRepository.add(sep3);
        hpRepository.add(repositoryButton);
        toolGroups.add(hpRepository);

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

        toolGroups.add(new Image(GWTUtils.getContextPath()+"/img/toolbar-right.gif"));

        mainPanel.add(toolGroups);

        initWidget(mainPanel);

}
    public void disableRepositoryButton() {
        repositoryButton.setEnabled(false);
        repositoryButton.removeStyleName(REPOSITORY);
        repositoryButton.addStyleName(REPOSITORY_DEACTIVATED);
        repositoryButton.setTitle(null);
    }

    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);
        } else if (clickListenerType.equals(ClickListenerType.REPOSITORY)) {
            this.repositoryButton.addClickListener(cl);
        }
    }

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

    public void enableSearchPossibilities(boolean enable) {
//Window.alert("ButtonsBarWidget.enableSearchPossibilities(boolean)");
        setAddCritereEnable(enable);
        searchButton.setEnabled(enable);
        resetCriteresButton.setEnabled(enable);
        openSearchButton.setEnabled(enable);
        saveRequestButton.setEnabled(enable);
        searchParamsButton.setEnabled(enable);
    }

    static public enum ClickListenerType {

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

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