/*
 * Created on 20.08.2003
 *
 */
package net.sourceforge.ganttproject.document;

import java.io.File;
import java.io.IOException;

import net.sourceforge.ganttproject.IGanttProject;
import net.sourceforge.ganttproject.gui.UIFacade;
import net.sourceforge.ganttproject.gui.options.model.DefaultStringOption;
import net.sourceforge.ganttproject.gui.options.model.GP1XOptionConverter;
import net.sourceforge.ganttproject.gui.options.model.GPOption;
import net.sourceforge.ganttproject.gui.options.model.GPOptionGroup;
import net.sourceforge.ganttproject.parser.ParserFactory;

/**
 * This is a helper class, to create new instances of Document easily. It
 * chooses the correct implementation based on the given path.
 * 
 * @author Michael Haeusler (michael at akatose.de)
 */
public class DocumentCreator implements DocumentManager {
    private IGanttProject myProject;

    private UIFacade myUIFacade;

    private ParserFactory myParserFactory;

    public DocumentCreator(IGanttProject project, UIFacade uiFacade,
            ParserFactory parserFactory) {
        myProject = project;
        myUIFacade = uiFacade;
        setParserFactory(parserFactory);

    }

    /**
     * Creates an HttpDocument if path starts with "http://" or "https://";
     * creates a FileDocument otherwise.
     * 
     * @param path
     *            path to the document
     * @return an implementation of the interface Document
     */
    private static Document createDocument(String path) {
        return createDocument(path, null, null);
    }

    /**
     * Creates an HttpDocument if path starts with "http://" or "https://";
     * creates a FileDocument otherwise.
     * 
     * @param path
     *            path to the document
     * @param user
     *            username
     * @param pass
     *            password
     * @return an implementation of the interface Document
     */
    public static Document createDocument(String path, String user, String pass) {
        if (null == path)
            return null;
        if (path.toLowerCase().startsWith("http://")
                || path.toLowerCase().startsWith("https://")) {
            return new HttpDocument(path, user, pass);
        } else {
            return new FileDocument(new File(path));
        }
    }

    public Document getDocument(String path) {
        Document physicalDocument = createDocument(path);
        Document proxyDocument = new ProxyDocument(this, physicalDocument, myProject,
                myUIFacade, getParserFactory());
        return proxyDocument;
    }
    
    public Document getDocument(String path, String userName, String password) {
        Document physicalDocument = createDocument(path, userName, password);
        Document proxyDocument = new ProxyDocument(this, physicalDocument, myProject, myUIFacade, getParserFactory());
        return proxyDocument;
    }

    public void addToRecentDocuments(Document document) {
        // TODO Auto-generated method stub
        
    }

    protected void setParserFactory(ParserFactory myParserFactory) {
        this.myParserFactory = myParserFactory;
    }

    protected ParserFactory getParserFactory() {
        return myParserFactory;
    }

	String createTemporaryFile() throws IOException {
		File tempFile = File.createTempFile("project", ".gan", getWorkingDirectoryFile());
		return tempFile.getAbsolutePath();
	}

    public void changeWorkingDirectory(File directory) {
    	assert directory.isDirectory();
        myWorkingDirectory.lock();
        myWorkingDirectory.setValue(directory.getAbsolutePath());
        myWorkingDirectory.commit();
    }
	public String getWorkingDirectory() {
		return myWorkingDirectory.getValue();
	}
    private File getWorkingDirectoryFile() {
    	return new File(getWorkingDirectory());
    }
    public GPOptionGroup getOptionGroup() {
        return myOptionGroup;
    }
    
    private final WorkingDirectoryOption myWorkingDirectory = new WorkingDirectoryOption();
    private final GPOptionGroup myOptionGroup = new GPOptionGroup("", new GPOption[] {myWorkingDirectory});
    private static class WorkingDirectoryOption extends DefaultStringOption implements GP1XOptionConverter {
        WorkingDirectoryOption() {
            super("working-dir");
        }
        public String getTagName() {
            return "working-dir";
        }
        public String getAttributeName() {
            return "dir";
        }
        public void loadValue(String legacyValue) {
            lock();
            loadPersistentValue(legacyValue);
            commit();
        }
    }
	
}
