/*
 * Copyright
 *   2007 axYus - www.axyus.com
 *   2009 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.exports;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ChangeListener;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.Grid;
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.ScrollPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Tree;
import com.google.gwt.user.client.ui.TreeItem;
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.ConfigExportDTO;
import fr.gouv.finances.dgfip.xemelios.web.gwt.client.dto.ExportChampDTO;
import fr.gouv.finances.dgfip.xemelios.web.gwt.client.dto.ExportElementDTO;
import fr.gouv.finances.dgfip.xemelios.web.gwt.client.service.ServiceLocator;

/**
 *
 * @author chm
 */
public class DlgEditConfig extends DialogBox {
    private ConfigExportDTO element=null;
    private TextBox dfCode=null, dfLibelle=null;
    private Button pbCancel=null, pbOk=null, pbSelectAll=null, pbUnSelectAll=null;
    private Tree tree=null;

    public DlgEditConfig(String docId, String etatId, String configId) {
        super(false, true);
        setText("Configuration d'export");
        initComponents();
        loadElement(docId,etatId,configId);
    }
    public DlgEditConfig(String docId, String etatId) {
        super(false, true);
        setText("Configuration d'export");
        initComponents();
        loadNewElement(docId, etatId);
    }

    protected void initComponents() {
        VerticalPanel vPane = new VerticalPanel();

        DecoratorPanel dp = new DecoratorPanel();
        Grid grd = new Grid(2,2);
        grd.setText(0, 0, "Code");
        grd.setText(1,0,new HTML("Libell&eacute;").getHTML());
        grd.setWidget(0, 1, dfCode = new TextBox());
        grd.setWidget(1,1,dfLibelle = new TextBox());
        dp.setWidget(grd);
        vPane.add(dp);

        HorizontalPanel hP = new HorizontalPanel();
        hP.getElement().getStyle().setProperty("margin-top", "5px");
        hP.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
        pbSelectAll = new Button(new HTML("Tout s&eacute;lectionner").getHTML());
        pbUnSelectAll = new Button(new HTML("Tout d&eacute;s&eacute;lectionner").getHTML());
        hP.add(pbSelectAll);
        hP.add(pbUnSelectAll);
        vPane.add(hP);

        ScrollPanel sc = new ScrollPanel();
        sc.setSize("300px", "300px");
        tree = new Tree();
        sc.add(tree);
        vPane.add(sc);


        HorizontalPanel hPane = new HorizontalPanel();
        hPane.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
        hPane.add(pbCancel=new Button("Annuler"));
        hPane.add(pbOk=new Button("Enregistrer"));
        vPane.add(hPane);

        setWidget(vPane);
        
        center();

        initListeners();

    }

    protected void initListeners() {
        pbCancel.addClickListener(new ClickListener() {
            public void onClick(Widget sender) {
                hide();
            }
        });
        dfCode.addChangeListener(new ChangeListener() {
            public void onChange(Widget sender) {
                element.setCode(dfCode.getText());
            }
        });
        dfLibelle.addChangeListener(new ChangeListener() {
            public void onChange(Widget sender) {
                element.setLibelle(dfLibelle.getText());
            }
        });
        pbOk.addClickListener(new ClickListener() {
            public void onClick(Widget sender) {
                ServiceLocator.getXemeliosService().saveExportModel(element, new AsyncCallback() {

                    public void onFailure(Throwable caught) {
                        Window.alert(caught.toString());
                    }

                    public void onSuccess(Object result) {
                        hide();
                    }
                });
            }
        });
        pbSelectAll.addClickListener(new ClickListener() {
            public void onClick(Widget sender) {
                TreeItem item = tree.getItem(0);
                selectTree(item,true);
            }
        });
        pbUnSelectAll.addClickListener(new ClickListener() {
            public void onClick(Widget sender) {
                TreeItem item = tree.getItem(0);
                selectTree(item,false);
            }
        });
    }

    private void selectTree(TreeItem item, boolean value) {
        Widget w = item.getWidget();
        if(w!=null && w instanceof MyCheckBox) {
            MyCheckBox mcb = (MyCheckBox)w;
            mcb.setSelected(value);
        }
        for(int i=0;i<item.getChildCount();i++) {
            selectTree(item.getChild(i),value);
        }
    }

    protected void displayElement() {
        dfCode.setText(element.getCode());
        dfLibelle.setText(element.getLibelle());
        TreeItem root = makeTreeItem(element.getExportModel().getElement());
        tree.addItem(root);
    }

    private void loadElement(String docId, String etatId, String configId) {
        ServiceLocator.getXemeliosService().getExportModel(docId, etatId, configId, new AsyncCallback() {
            public void onFailure(Throwable caught) {
                Window.alert("Erreur lors du chargement.");
                hide();
            }
            public void onSuccess(Object result) {
                element=(ConfigExportDTO)result;
                displayElement();
            }
        });
    }
    private void loadNewElement(String docId, String etatId) {
        ServiceLocator.getXemeliosService().createExportModel(docId, etatId, new AsyncCallback() {
            public void onFailure(Throwable caught) {
                Window.alert("Erreur lors du chargement.");
                hide();
            }
            public void onSuccess(Object result) {
                element=(ConfigExportDTO)result;
                displayElement();
            }
        });
    }

    protected TreeItem makeTreeItem(ExportElementDTO el) {
        TreeItem ret = new TreeItem(el.getLibelle());
        for(ExportChampDTO champ:el.getChamps()) {
            CheckBox chk = new MyCheckBox(champ);
            TreeItem it = new TreeItem(chk);
            ret.addItem(it);
        }
        for(ExportElementDTO child:el.getEnfants()) {
            ret.addItem(makeTreeItem(child));
        }
        return ret;
    }

    private class MyCheckBox extends CheckBox {
        private ExportChampDTO champ;
        public MyCheckBox(ExportChampDTO champ) {
            super(champ.getLibelle());
            this.champ=champ;
            setName(champ.getId());
            setChecked(champ.isSelectionne());
            addClickListener(new ClickListener() {
                public void onClick(Widget sender) {
                    MyCheckBox.this.champ.setSelectionne(isChecked());
                }
            });
        }
        public void setSelected(boolean value) {
            setChecked(value);
            champ.setSelectionne(value);
        }
    }

}
