/*
 * 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.criteria;

import fr.gouv.finances.dgfip.xemelios.web.gwt.client.dto.CritereItemDTO;
import fr.gouv.finances.dgfip.xemelios.web.gwt.client.dto.CritereItemVisitor;
import fr.gouv.finances.dgfip.xemelios.web.gwt.client.dto.DateCritereItemDTO;
import fr.gouv.finances.dgfip.xemelios.web.gwt.client.dto.TextCritereItemDTO;
import fr.gouv.finances.dgfip.xemelios.web.gwt.client.dto.LabelCritereItemDTO;
import fr.gouv.finances.dgfip.xemelios.web.gwt.client.dto.NumericCritereItemDTO;
import fr.gouv.finances.dgfip.xemelios.web.gwt.client.dto.Property;
import fr.gouv.finances.dgfip.xemelios.web.gwt.client.dto.SelectOneCritereItemDTO;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author CBO
 * Fabrique permettant de créer les composants graphiques affichés pour la saisie d'un critère de recherche à partir des DTO
 */
public class CritereWidgetFactory {

    static public CritereWidget createWidget(CritereLineContainer criteriaLineContainer, CritereItemDTO critereItem) {
        CriteriaWidgetFactoryCritereItemVisitor criteriaWidgetFactoryCritereItemVisitor = new CriteriaWidgetFactoryCritereItemVisitor(criteriaLineContainer);
        critereItem.accept(criteriaWidgetFactoryCritereItemVisitor);
        return criteriaWidgetFactoryCritereItemVisitor.toReturn;
    }
    static public CritereWidget createWidget(CritereLineContainer criteriaLineContainer, CritereItemDTO critereItem,List<Property> properties) {
        CriteriaWidgetFactoryCritereItemVisitor criteriaWidgetFactoryCritereItemVisitor = new CriteriaWidgetFactoryCritereItemVisitor(criteriaLineContainer);
        critereItem.accept(criteriaWidgetFactoryCritereItemVisitor,properties);
        return criteriaWidgetFactoryCritereItemVisitor.toReturn;
    }

    static public class InputCritereWidgetRepository {

        private Map<String, InputCritereWidget> widgetByInputId = new HashMap<String, InputCritereWidget>();

        public void add(InputCritereWidget inputCritereWidget) {
            widgetByInputId.put(inputCritereWidget.getInputId(), inputCritereWidget);
        }

        public InputCritereWidget getWidget(String inputId) {
            return widgetByInputId.get(inputId);
        }
    }

    static private class CriteriaWidgetFactoryCritereItemVisitor implements CritereItemVisitor {

        private CritereLineContainer critereLineContainer;
        private CritereWidget toReturn;

        public CriteriaWidgetFactoryCritereItemVisitor(CritereLineContainer critereLineContainer) {
            this.critereLineContainer = critereLineContainer;
        }

        public void visit(LabelCritereItemDTO labelCritereItem) {
            this.toReturn = new LabelCritereWidget(critereLineContainer, labelCritereItem.getLibelle());
        }

        public void visit(final SelectOneCritereItemDTO selectOneCritereItem) {
            this.toReturn = new SelectOneCritereWidget(critereLineContainer, selectOneCritereItem);
        }

        public void visit(TextCritereItemDTO inputTextCritereItem) {
            this.toReturn = new TextCritereWidget(critereLineContainer, inputTextCritereItem);
        }

        public void visit(DateCritereItemDTO inputDateCritereItem) {
            this.toReturn = new DateCritereWidget(critereLineContainer, inputDateCritereItem);
        }
        
        public void visit(NumericCritereItemDTO numericCritereItem) {
            this.toReturn = new NumericCritereWidget(critereLineContainer, numericCritereItem);
        }
        public void visit(LabelCritereItemDTO labelCritereItem,List<Property> properties) {
            this.toReturn = new LabelCritereWidget(critereLineContainer, labelCritereItem.getLibelle());
        }

        public void visit(final SelectOneCritereItemDTO selectOneCritereItem, List<Property> properties) {
            this.toReturn = new SelectOneCritereWidget(critereLineContainer, selectOneCritereItem,properties);
        }

        public void visit(TextCritereItemDTO inputTextCritereItem, List<Property> properties) {
            this.toReturn = new TextCritereWidget(critereLineContainer, inputTextCritereItem,properties);
        }

        public void visit(DateCritereItemDTO inputDateCritereItem,List<Property> properties) {
            this.toReturn = new DateCritereWidget(critereLineContainer, inputDateCritereItem,properties);
        }

        public void visit(NumericCritereItemDTO numericCritereItem, List<Property> properties) {
            this.toReturn = new NumericCritereWidget(critereLineContainer, numericCritereItem,properties);
        }
    }

    private CritereWidgetFactory() {
        // unused
    }

}
