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

import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.ui.TextBox;
import fr.gouv.finances.dgfip.xemelios.web.gwt.client.dto.DateCritereItemDTO;
import fr.gouv.finances.dgfip.xemelios.web.gwt.client.dto.Property;
import java.util.List;

public class DateCritereWidget extends InputCritereWidget {

    private DateCritereItemDTO dateCritereItem;
    private DateTimeFormat dateFormat;
    private TextBox textBox;

    public DateCritereWidget(CritereLineContainer critereLineContainer, DateCritereItemDTO critereItem) {
        super(critereLineContainer, critereItem);
        this.dateCritereItem = critereItem;
        this.dateFormat = DateTimeFormat.getFormat(critereItem.getFormat());
        this.textBox = new TextBox();
        if(critereItem.getLastInputValue()!=null) {
            textBox.setText(critereItem.getLastInputValue());
        }
        initWidget(textBox);
    }
    public DateCritereWidget(CritereLineContainer critereLineContainer, DateCritereItemDTO critereItem, List<Property> properties) {
        super(critereLineContainer, critereItem);
        this.dateCritereItem = critereItem;
        this.dateFormat = DateTimeFormat.getFormat(critereItem.getFormat());
        this.textBox = new TextBox();
        String lastValue = null;
        for(Property  prop: properties) {
            if(prop.getName().equals(getInputId())) {
                lastValue = prop.getValue();
            }
        }
        if(lastValue==null) {
            lastValue = critereItem.getLastInputValue();
        }
        if(lastValue!=null) {
            textBox.setText(lastValue);
        }
        initWidget(textBox);
    }

    @Override
    public void setFocus() {
        this.textBox.setFocus(true);
    }

    @Override
    public String getInputValue() {
        return textBox.getText();
    }

    @Override
    public void refresh() {
        throw new UnsupportedOperationException();
    }

    @Override
    public void checkValidity() throws ValidityException {
        if (isFilled()) {
            try {
                this.dateFormat.parse(getInputValue());
            } catch(IllegalArgumentException pe) {
                throw new ValidityException("La valeur \"" + getInputValue() + "\" n'est pas une date valide, le format attendu est " + dateCritereItem.getFormat());
            }
        }
    }

    @Override
    public void setInputValue(String value) {
        textBox.setText(value);
    }

    @Override
    public void setEnabled(boolean enabled) {
        textBox.setEnabled(enabled);
    }
}
