/*
 * 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.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.ChangeListener;
import com.google.gwt.user.client.ui.ChangeListenerCollection;
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.InlineHTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.Widget;
import fr.gouv.finances.dgfip.xemelios.web.gwt.client.dto.LabelValueDTO;
import fr.gouv.finances.dgfip.xemelios.web.gwt.client.service.ServiceLocator;
import fr.gouv.finances.dgfip.xemelios.web.gwt.client.utils.GWTUtils;
import java.util.List;

/**
 * @author CBO
 * Widget permettant � l'utilisateur de choisir la collectivit� et le budget
 */
public class ColAndBudChooserWidget extends Composite {

    private SearchWidget searchWidget;
    private ListBox collectivitesListBox;
    private ListBox budgetListBox;
    private ChangeListenerCollection changeListeners;
//    private HorizontalPanel horizontalPanel;

    public ColAndBudChooserWidget(final SearchWidget searchWidget) {
        this.searchWidget = searchWidget;
        this.collectivitesListBox = new ListBox();
        this.budgetListBox = new ListBox();
        this.changeListeners = new ChangeListenerCollection();
        collectivitesListBox.addChangeListener(new ChangeListener() {

            public void onChange(Widget widget) {
                collectiviteChange();
            }
        });
        budgetListBox.addChangeListener(new ChangeListener() {

            public void onChange(Widget widget) {
                budgetChange();
            }
        });

//        StringBuffer html = new StringBuffer();
//        html.append("<div id=\"NavigationTools\" 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("<span id=\"divColl\">Collectivit&eacute; : </span>");
//                html.append("<span id=\"divBudg\">Budget : </span>");
//            html.append("</div>");
//        html.append("</div>");
//
//        HTMLPanel pane = new HTMLPanel(html.toString());
//        pane.add(collectivitesListBox, "divColl");
//        pane.add(budgetListBox,"divBudg");
//        initWidget(pane);

        FlowPanel mainPanel = new FlowPanel();
        mainPanel.getElement().setId("NavigationTools");
        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 divMain = new FlowPanel();
        divMain.setStyleName("Main");
        divMain.add(new InlineHTML("Collectivit&eacute; : "));
        divMain.add(collectivitesListBox);
        divMain.add(new InlineHTML("Budget : "));
        divMain.add(budgetListBox);
        mainPanel.add(divMain);

        initWidget(mainPanel);
    }

    public void init(List<LabelValueDTO> collectivites, List<LabelValueDTO> budgets) {
        this.collectivitesListBox.clear();
        for (LabelValueDTO collectivite : collectivites) {
            collectivitesListBox.addItem(collectivite.getLabel(), collectivite.getValue());
        }
        for (LabelValueDTO budget : budgets) {
            budgetListBox.addItem(budget.getLabel(), budget.getValue());
        }
    }

    public String getCollectiviteId() {
        return collectivitesListBox.getValue(collectivitesListBox.getSelectedIndex());
    }

    public String getBudgetId() {
        return budgetListBox.getValue(budgetListBox.getSelectedIndex());
    }

    public void addChangeListener(ChangeListener changeListener) {
        this.changeListeners.add(changeListener);
    }

    private void collectiviteChange() {
        ServiceLocator.getXemeliosService().getBudgets(searchWidget.getSearchSessionId(), getCollectiviteId(), new AsyncCallback<List<LabelValueDTO>>() {

            public void onFailure(Throwable throwable) {
                GWTUtils.handleRPCError(throwable);
            }

            public void onSuccess(List<LabelValueDTO> budgets) {
                budgetListBox.clear();
                for (LabelValueDTO budget : budgets) {
                    budgetListBox.addItem(budget.getLabel(), budget.getValue());
                }
                fireOnchange();
            }
        });
    }

    private void budgetChange() {
        fireOnchange();
        searchWidget.resetCriteres();
    }

    private void fireOnchange() {
        this.changeListeners.fireChange(this);
    }
}
