/*
 * Copyright
 *   2010 axYus - www.axyus.com
 *   2010 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.cp.xemelios.controls.models;

import java.util.Hashtable;

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.XmlAttributes;
import fr.gouv.finances.cp.utils.xml.marshal.XmlMarshallable;
import fr.gouv.finances.cp.utils.xml.marshal.XmlOutputter;
import fr.gouv.finances.cp.xemelios.controls.processors.Processor;
import fr.gouv.finances.cp.xemelios.controls.processors.Processor.EVENT;
import fr.gouv.finances.dgfip.utils.xml.XmlAttributesImpl;
import java.util.ArrayList;
import javax.xml.namespace.QName;
import org.apache.log4j.Logger;

public class AddOnModel implements XmlMarshallable {
    private static final Logger logger = Logger.getLogger(AddOnModel.class);
    public static final transient String TAG = "add-on";
    public static final transient QName QN = new QName(TAG);

    private Hashtable<String,ParamModel> parameters;
    private String name;

    private Processor processor;

    public AddOnModel(QName qn) {
        super();
        parameters = new Hashtable<String, ParamModel>();
    }

    public void addCharacterData(String cData) throws SAXException { }

    public void addChild(XmlMarshallable child, QName tag) throws SAXException {
        if(tag.equals(ParamModel.QN)) {
            ParamModel p = (ParamModel)child;
            parameters.put(p.getId(),p);
        }
    }

    public XmlMarshallable getAttributes(XmlAttributes attributes) throws SAXException {
        name = attributes.getValue("name");
        return this;
    }

    public XmlMarshallable getChildToModify(String uri, String localName, String qName, Attributes atts) {
        if(atts.getValue("name")!=null) {
            return parameters.get(atts.getValue("name"));
        }
        return null;
    }

    public void marshall(XmlOutputter output) {
        output.startTag(QN);
        output.addAttribute("name", name);
        for(ParamModel p:parameters.values()) {
            p.marshall(output);
        }
        output.endTag(QN);
    }

    public void validate() throws InvalidXmlDefinition {}

    public QName getQName() {
        return QN;
    }

    @Override
    public AddOnModel clone() {
        AddOnModel other = new AddOnModel(QN);
        other.name=name;
        for(ParamModel p:parameters.values()) {
            try {
                other.addChild(p, ParamModel.QN);
            } catch(Throwable t) {}
        }
        return other;
    }

    public boolean isConcernedBy(EVENT evt, String uri, String localName, String qName) {
        boolean ret = true;
        String targetEvent = (String)parameters.get("event").getValue();
        if(!"*".equals(targetEvent)) {
            switch(evt) {
                case START_DOCUMENT: ret = targetEvent.contains("start.document"); break;
                case START_ELEMENT: ret = targetEvent.contains("start.element"); break;
                case END_ELEMENT: ret = targetEvent.contains("end.element"); break;
                case END_DOCUMENT: ret = targetEvent.contains("end.document"); break;
                default: ret = false;
            }
            if(!ret) logger.debug(name+" is not concerned by "+qName);
        }
        if(ret) {
            // event is OK, try to match element name
            // only if start.element or end.elment
            if(evt == EVENT.START_ELEMENT || evt == EVENT.END_ELEMENT) {
                ret = parameters.get("element.name").getValue().equals(localName);
            }
        }
        return ret;
    }
    public Processor getProcessor() {
        if(processor==null) {
            try {
                Class clazz = Class.forName((String)parameters.get("processor").getValue());
                processor = (Processor)clazz.newInstance();
                ArrayList<ParamModel> params = new ArrayList<ParamModel>();
                for(ParamModel p:parameters.values()) params.add(p);
                processor.setParameters(params);
            } catch(Throwable t) {
                logger.error("getProcessor()",t);
            }
        }
        return processor;
    }
    public void process(EVENT evt, String uri, String localName, String qName, XmlAttributesImpl atts) {
        getProcessor().process(evt, uri, localName, qName, atts);
    }
    public void process(EVENT evt, String uri, String localName, String qName) {
        getProcessor().process(evt, uri, localName, qName);
    }
    public void process(EVENT evt) {
        getProcessor().process(evt);
    }

}
