/*
 */

package au.gov.ansto.bragg.nexus.tool;

import au.gov.ansto.bragg.forward.All_;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collections;
//import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.Set;

/**
 *
 * @author drck
 * @version "%I%, %G%"
 */
public class Project_
        extends All_
        implements Project {
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        
        Project project = new Project_("<root>");
        try {
            project.addXmlTemplate("file:/<file1.xml>");
            project.addXmlTemplate("file:/<file2.xml>");
        } catch (MalformedURLException ex) {
            ex.printStackTrace();
        }
    }
    
    public Project_(
            String $directory
            ) {
        if ($directory==null) error("null $directory");
        else $directory_ = $directory;
        /*
        debug("$directory",$directory());
        debug("$package_out",$package_out());
        debug("$src",$src());
        debug("$src_out",$src_out());
         */
    }
    
    
    /** root directory for the project */
    private String $directory_;
    public String get$directory() {
        return $directory_;
    }
    
    private Set<XmlTemplate> xmlTemplates_;
    protected Set<XmlTemplate> xmlTemplates() {
        if (xmlTemplates_==null) {
            xmlTemplates_ =
                    Collections.synchronizedSet(new LinkedHashSet<XmlTemplate>());
        }
        return xmlTemplates_;
    }
    
    /** @inheritDoc */
    public Set<XmlTemplate> getXmlTemplates() {
        return Collections.unmodifiableSet(xmlTemplates());
    }
    
    /** @inheritDoc */
    public boolean addXmlTemplate(String $url) throws MalformedURLException {
        XmlTemplate t = newXmlTemplate($url);
        return xmlTemplates().add(t);
    }
    
    /** Creates a new XML template retrieved from the given string URL */
    protected XmlTemplate newXmlTemplate(String $url) throws MalformedURLException {
        XmlTemplate t = new XmlTemplate_(new URL($url));
        return t;
    }
    
    /** @inheritDoc */
    public boolean addXmlTemplateDirectory(
            String $xmldir
            ) {
        //INFO("base:"+base);
        //INFO("reldir:"+reldir);
        
        //String $fulldir = $base + $reldir;
        File file = new File($xmldir);
        if (!file.isDirectory()) {
            error("not directory:"+$xmldir);
            return false;
        }
        
        File[] files = file.listFiles();
        
        boolean ok = true;
        for (int i=0; i < files.length; i++) {
            File f = files[i];
            //INFO(f.getName());
            if (f.isDirectory()) {
                debug("isDirectory",f.getName());
                ok = ok && addXmlTemplateDirectory($xmldir+"/"+f.getName()+"/");
            } else if (f.isFile()) {
                debug("isFile",f.getName());
                //String $url = $reldir+"/"+f.getName();
                String $url = "file:"+$xmldir+f.getName();
                debug("adding XML template for URL",$url);
                try {
                    ok = ok && addXmlTemplate($url);
                } catch (MalformedURLException ex) {
                    error(ex);
                }
            }
        }
        return ok;
    }
    
    /** The 'src' directory stem */
    static public final String $SRC = "src/";
    
    /** The full 'src' directory (under the root directory) */
    protected String $src() { return get$directory()+"/"+$SRC;}
    
    /** The package stem for the auto generated NeXusBeans */
    static public final String $AUTO = "auto";
    
    /** The default output package for the auto generated NeXusBeans;
     * currently FIXED to enforce Bragg Institute naming convention.
     */
    static public final String $PACKAGE_OUT = "au.gov.ansto.bragg.nexus."+$AUTO;
    
    /** @inheritDoc */
    public String get$package_out() {return $PACKAGE_OUT;}
    
    static public final String $OUT = "au/gov/ansto/bragg/nexus/"+$AUTO+"/";
    
    /** @inheritDoc */
    public String get$directory_out() {return $src()+$OUT;}
}

//static public final String $PACKAGE_XML = "au.gov.ansto.bragg.nexus.xml";
//public String $package_xml() {return $PACKAGE_XML;}

    /*
    static public final String $SRC_XML = $SRC + "/xml";
    public String $src_xml() {return $src()+$SRC_XML;}
     */
    /*
    static private String $stripDoubleSlash(String s) {
    }
     */
