package org.jerrymouse.gaes.mavenPlugin.mojos;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.Properties;

import org.apache.maven.model.Resource;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.codehaus.plexus.util.IOUtil;
import org.jerrymouse.gaes.mavenPlugin.base.ConfigFile;
import org.jerrymouse.gaes.mavenPlugin.base.InfoBase;
import org.jerrymouse.gaes.mavenPlugin.base.MojoBase;
import org.jerrymouse.gaes.mavenPlugin.eclipse.EclipseConfig;

/**
 * @goal config
 */
public class ConfigMojo extends MojoBase {

	@Override
	public void execute() throws MojoExecutionException, MojoFailureException {
		super.execute();
	}

	private void eclipseClean() {
		runMvnCommond("eclipse:clean");
	}

	private void eclipse() {
		runMvnCommond("eclipse:eclipse");
	}

	private void generateResources() {
		runMvnCommond("generate-resources");
	}

	private void clean() {
		runMvnCommond("clean");
	}

	private void eclipseAddon() {
			EclipseConfig eclipseConfig=new EclipseConfig(new File(project.getBasedir().getAbsolutePath()
					+ "/.settings/org.eclipse.wst.common.component"));
			eclipseConfig.config();
	}

	@Override
	protected void gaeExecute() {
		getLog().info("gae");
		changeProperty(gae);
		clean();
		eclipseClean();
		generateResources();
		eclipse();
		eclipseAddon();
	}

	protected void eclipseCleanAddon() {
		getLog().info("eclipseCleanAddon");
		File settings = new File(project.getBasedir().getAbsolutePath()
				+ "/.settings");
		if (settings.isDirectory()) {
			for (File f : settings.listFiles()) {
				f.delete();
			}
		}
	}

	private void changeProperty(String gae) {
		ConfigFile configFile = new ConfigFile(project, getBottomLayer());
		if (!configFile.getBottomLayerFile().equals(getBottomLayer())) {
			configFile.setBottomLayerFile(getBottomLayer());
		}
	}

	@Override
	protected void normalExecute() {
		getLog().info("normal");
		changeProperty(normal);
		clean();
		eclipseCleanAddon();
		generateResources();
		eclipse();
		eclipseAddon();
	}

	@Override
	protected void hadoopExecute() {
		getLog().info("hadoop");
		changeProperty(hadoop);
		clean();
		eclipseCleanAddon();
		generateResources();
		eclipse();
		eclipseAddon();
	}

}
