/*
 * Copyright
 *   2007 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 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.cp.xemelios.updater.config;

import java.util.ArrayList;
import java.util.Iterator;

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 javax.xml.namespace.QName;

public class ComponentModel implements XmlMarshallable {
	public static final transient String TAG = "component";
        public static final transient QName QN = new QName(TAG);
	
	/**
	 * XEMELIOS type component
	 */
	public static final int XEMELIOS = 1;
	/**
	 * CONFIG type component
	 */
	public static final int CONFIG = 2;
	/**
	 * FEATURE type component
	 */
	public static final int FEATURE = 3;
	
	
	private String id, name;
	private ReleaseModel release;
	private TexteModel description;
	private RequireModel requires;
	private ArrayList<FileListModel> fileLists;
	private ConfigurationModel configuration;
	private boolean visible = true;
	
	// for UI behavior only
	public boolean selected = false;
	public int type = 0;

	public ComponentModel(QName tag) {
		super();
		fileLists = new ArrayList<FileListModel>();
	}

	public void addCharacterData(String cData) throws SAXException {
	}

	public void addChild(XmlMarshallable child, QName tagName) throws SAXException {
		if(ReleaseModel.QN.equals(tagName)) release = (ReleaseModel)child;
		else if(UpdateParser.DESCRIPTION.equals(tagName)) description = (TexteModel)child;
		else if(RequireModel.QN.equals(tagName)) requires=(RequireModel)child;
		else if(FileListModel.QN.equals(tagName)) fileLists.add((FileListModel)child);
		else if(ConfigurationModel.QN.equals(tagName)) configuration=(ConfigurationModel)child;
	}

	public XmlMarshallable getAttributes(XmlAttributes attributes) throws SAXException {
		id = attributes.getValue("cid");
		name = attributes.getValue("name");
		String sTmp = attributes.getValue("visible");
		if(sTmp!=null) visible=attributes.getBooleanValue("visible");
		return this;
	}

	public void marshall(XmlOutputter output) {
		output.startTag(TAG);
		output.addAttribute("cid",id);
		output.addAttribute("name",name);
		if(!visible) output.addAttribute("visible","no");
		release.marshall(output);
		description.marshall(output);
		if(requires!=null) requires.marshall(output);
		for(Iterator<FileListModel> it=fileLists.iterator();it.hasNext();)
			it.next().marshall(output);
		// TODO
		output.endTag(TAG);
	}

	public void validate() throws InvalidXmlDefinition {
		if(id==null) throw new InvalidXmlDefinition("cid attribute is mandatory for element "+TAG);
		if(name==null) throw new InvalidXmlDefinition("name attribute is mandatory for element "+TAG);
	}
	
	public ComponentModel clone() {
		ComponentModel cm = new ComponentModel(QN);
		cm.id=this.id;
		cm.name=this.name;
		cm.release=release.clone();
		cm.description=this.description.clone();
		for(Iterator<FileListModel> it=fileLists.iterator();it.hasNext();)
			cm.fileLists.add(it.next().clone());
		return cm;
	}

	public TexteModel getDescription() {
		return description;
	}

	public void setDescription(TexteModel description) {
		this.description = description;
	}
	public void setDescription(String s) {
		TexteModel desc = new TexteModel(UpdateParser.DESCRIPTION);
		try { desc.addCharacterData(s); } catch(Throwable t) {}
		setDescription(desc);
	}

	public ArrayList<FileListModel> getFileLists() {
		return fileLists;
	}

	public void setFileLists(ArrayList<FileListModel> fileLists) {
		this.fileLists = fileLists;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public ReleaseModel getRelease() {
		return release;
	}

	public void setRelease(ReleaseModel release) {
		this.release = release;
	}
	public void setRelease(String s) {
		ReleaseModel rm = new ReleaseModel(ReleaseModel.QN);
		rm.setV(s);
		setRelease(rm);
	}

	public RequireModel getRequires() {
		return requires;
	}

	public void setRequires(RequireModel requires) {
		this.requires = requires;
	}
	public String toString() {
		return getId()+" ("+getRelease().getV()+")";
	}
	public boolean isVisible() {
		return visible;
	}

	@Override
	public boolean equals(Object o) {
		if(o instanceof ComponentModel) {
			ComponentModel cm = (ComponentModel)o;
			return cm.getId().equals(id) && cm.getRelease().getV().equals(getRelease().getV());
		} else if(o instanceof ComponentRefModel) {
			ComponentRefModel cm = (ComponentRefModel)o;
			return cm.getId().equals(id) && cm.getVersion().equals(getRelease().getV());
		} else if(o instanceof String) {
			return getId().equals((String)o);
		}
		return false;
	}

	public ConfigurationModel getConfiguration() {
		return configuration;
	}

	public void setConfiguration(ConfigurationModel configuration) {
		this.configuration = configuration;
	}

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

    public QName getQName() {
        return QN;
    }
}
