/*
 * Copyright
 *   2005 axYus - www.axyus.com
 *   2005 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.common.config;

import org.apache.commons.lang.StringEscapeUtils;
import org.apache.log4j.Logger;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

import fr.gouv.finances.cp.utils.xml.marshal.InvalidXmlDefinition;
import fr.gouv.finances.cp.utils.xml.marshal.NoeudModifiable;
import fr.gouv.finances.cp.utils.xml.marshal.XmlAttributes;
import fr.gouv.finances.cp.utils.xml.marshal.XmlMarshallable;
import fr.gouv.finances.cp.utils.xml.marshal.XmlOutputter;
import javax.xml.namespace.QName;

/**
 * Modelise un champ de saisie
 * @author chm
 */
public class InputModel implements NoeudModifiable {
    private static Logger logger = Logger.getLogger(InputModel.class);
    private NoeudModifiable _NMParent = null;
    
    public static final transient String TAG = "input";
    public static final transient QName QN = new QName(TAG);
    private String id, libelle, datatype, helpPath, value,xmlFormat;
    private boolean uppercase = false;
    private CritereModel parent = null;
    private String keyName=null;
    private QName qn;
    
    public void setParent(CritereModel parent) {
        this.parent = parent;
    }
    public boolean isUppercase() {
        return uppercase;
    }
    public InputModel(QName tagName) { super(); this.qn=tagName; }
    public void addCharacterData(String cData) throws SAXException {}
    public void addChild(XmlMarshallable child, QName tagName) throws SAXException {}
    public XmlMarshallable getAttributes(XmlAttributes attributes) throws SAXException {
        id = (attributes.getValue("id")!=null?attributes.getValue("id"):id);
        libelle = (attributes.getValue("libelle")!=null?attributes.getValue("libelle"):libelle);
        datatype = (attributes.getValue("datatype")!=null?attributes.getValue("datatype"):datatype);
        if (attributes.getValue("uppercase")!=null)
            uppercase = attributes.getBooleanValue("uppercase");
        helpPath = (attributes.getValue("help")!=null?attributes.getValue("help"):helpPath);
        value = (attributes.getValue("value")!=null?attributes.getValue("value"):value);
        xmlFormat = (attributes.getValue("xml-format")!=null?attributes.getValue("xml-format"):xmlFormat);
        return this;
    }
    public void marshall(XmlOutputter output) {
        output.startTag(TAG);
        output.addAttribute("id",id);
        if(libelle!=null) output.addAttribute("libelle",StringEscapeUtils.escapeXml(libelle));
        output.addAttribute("datatype",datatype);
        output.addAttribute("uppercase",uppercase);
        if("date".equals(datatype)) output.addAttribute("xml-format",xmlFormat);
        if(helpPath!=null) output.addAttribute("help",helpPath);
//	    if(value!=null) output.addAttribute("value",StringEscapeUtils.escapeXml(value));
        output.endTag(TAG);
    }
    public void validate() throws InvalidXmlDefinition {
        if(id==null || id.length()==0) throw new InvalidXmlDefinition("//"+TAG+"/@id is required ("+getParentAsNoeudModifiable().getConfigXPath()+")");
//        if((xmlFormat==null || xmlFormat.length()==0) && "date".equals(datatype)) throw new InvalidXmlDefinition("//"+TAG+"[@datatype='date']/@xml-format is required ("+getConfigXPath()+")");
    }
    public String getDatatype() {return datatype;}
    public String getId() {return id;}
    public String getLibelle() {return libelle;}
    public String getHelpPath() { return helpPath; }
    public String getXmlDateFormat() {
        // changed for types
        //return xmlFormat;
        if(getDatatype().equals("date")) return "yyyy-MM-dd";
        else if(getDatatype().equals("time")) return "yyyy-MM-dd'T'hh:mm:ss";
        else return "";
    }
    public String getValue() { return value; }
    public void setValue(String value) { this.value=value; }
    @Override
    public InputModel clone() {
        InputModel im = new InputModel(QN);
        im.id = this.id;
        im.libelle = this.libelle;
        im.datatype = this.datatype;
        im.helpPath = this.helpPath;
        im.uppercase = this.uppercase;
        im.xmlFormat = this.xmlFormat;
        im.value=this.value;
        im._NMParent=this._NMParent;
        return im;
    }
    public void setParentAsNoeudModifiable(NoeudModifiable p) {
        this._NMParent = p;
    }
    public NoeudModifiable getParentAsNoeudModifiable() {
        return this._NMParent;
    }
    public NoeudModifiable getChildAsNoeudModifiable(String tagName, String id) {
        return null;
    }
    public void modifyAttr(String attrName, String value) {
    }
    public void modifyAttrs(Attributes attrs) {
        try {
            getAttributes(new XmlAttributes(attrs));
        } catch (Exception e) {
            logger.error("Erreur lors de la mise � jour des attributs : "+e);
        }
    }
    public String[] getChildIdAttrName(String childTagName) {
        return null;
    }
    public void resetCharData() {
    }
    public String getIdValue() { return getId(); }
    /**
     * This criteria key. Key is composed of Document id + "-" + Etat id + "-" + criteria id<br/>
     * a same criteria may be shared between differents elements.
     */
    public String getKeyName() {
        if(keyName==null) {
            CritereModel cm = (CritereModel)_NMParent;
            if(cm==null) return null;
            Object o = cm.getParentAsNoeudModifiable();
            ElementModel el = null;
            PluginModel pm = null;
            if(o instanceof ElementModel) {
                el = (ElementModel)o;
            } else if(o instanceof PluginModel) {
                pm = (PluginModel)o;
                el = (ElementModel)pm.getParentAsNoeudModifiable();
            }
            if(el==null) return null;
            EtatModel em = (EtatModel)el.getParent();
            if(em==null) return null;
            DocumentModel dm = em.getParent();
            if(dm==null) return null;
            StringBuffer b = new StringBuffer();
            b.append(dm.getId()).append("-").append(em.getId()).append("-");
            if(pm!=null) b.append(pm.getId()).append("-");
            b.append(getId());
            keyName = b.toString();
        }
        return keyName;
    }
    private String configXPath = null;
    public String getConfigXPath() {
        if(configXPath==null) {
            if(getParentAsNoeudModifiable()!=null) configXPath = getParentAsNoeudModifiable().getConfigXPath();
            else configXPath="";
            configXPath+="/"+TAG+"[@id='"+getId()+"']";
        }
        return configXPath;
    }

    public XmlMarshallable getChildToModify(String uri, String localName, String qName, Attributes atts) { return null; }

    public QName getQName() { return qn; }
}
