/*
 * Copyright
 *   2008 axYus - www.axyus.com
 *   2008 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 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.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Widget;
import fr.gouv.finances.dgfip.xemelios.web.gwt.client.dto.PjRefInfoDTO;
import fr.gouv.finances.dgfip.xemelios.web.gwt.client.service.ServiceLocator;
import fr.gouv.finances.dgfip.xemelios.web.gwt.client.utils.GWTUtils;

/**
 * Une ligne de PJ, avec l'icone
 */
public class PjRefWidget extends HorizontalPanel {
    private Image image;
    private Hyperlink hLink;
    private ShowAvailablesPJsDialogBox db;
    private static final String IMG_WIDTH = "16";
    private static final String IMG_HEIGHT = "16";
    private static final String IMG_DEFAULT_URL = "pj_notcomputed.gif";

    // copie de fr.gouv.finances.dgfip.xemelios.common.config.PjRefInfo
    public static final transient String SUPPORT_ONLINE = "00";
	public static final transient String SUPPORT_LOCAL1 = "01";
	public static final transient String SUPPORT_PAPIER = "02";
	public static final transient String SUPPORT_CD_DVD = "03";
    public static final transient String SUPPORT_LOCAL2 = "10";

    public PjRefWidget(PjRefInfoDTO dto,ShowAvailablesPJsDialogBox db) {
        super();
        this.db=db;
        image = new Image(getImageUrl(dto.getSupport()));
        image.setSize(IMG_WIDTH, IMG_HEIGHT);
        add(image);
        hLink = new Hyperlink();
        hLink.setText(dto.getNom());
        add(hLink);
        postProcess(dto,hLink);
    }
    private String getImageUrl(String support) {
        String ret = null;
        if(SUPPORT_ONLINE.equals(support))
            ret = "pj-online.png";
        else if(SUPPORT_PAPIER.equals(support))
            ret = "pj-paper.png";
        else if(SUPPORT_CD_DVD.equals(support) || SUPPORT_LOCAL1.equals(support) || SUPPORT_LOCAL2.equals(support))
            ret = IMG_DEFAULT_URL;
        return GWTUtils.getContextPath() +"/img/pjs/" + ret;
    }
    private void postProcess(final PjRefInfoDTO dto, final Hyperlink hLink) {
        String support = dto.getSupport();
        if(SUPPORT_CD_DVD.equals(support) || SUPPORT_LOCAL1.equals(support) || SUPPORT_LOCAL2.equals(support)) {
            ServiceLocator.getXemeliosService().isPjAvailable(dto.getCollectivite(),dto.getNom(),
                    new AsyncCallback<Boolean>() {

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

                public void onSuccess(Boolean result) {
                    if(result.booleanValue()) {
                        image.setUrl(GWTUtils.getContextPath()+"/img/pjs/pj-available.png");
                        hLink.addClickListener(new ClickListener() {

                            public void onClick(Widget sender) {
                                openPj(dto.getCollectivite(), dto.getNom());
                                db.hide();
                            }
                        });
                    } else {
                        image.setUrl(GWTUtils.getContextPath()+"/img/pjs/pj-unavailable.png");
                    }
                }
            });
        }
    }
    public static void openPj(String collectivite, String pjName) {
        String url = GWTUtils.getContextPath()+"/attachment.do?collectivite="+collectivite+"&pjId="+pjName+"&method=byNom";
        Window.open(url, "_blank", "");
    }
}
