package org.jerrymouse.gaes.mavenPlugin.base;

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.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.hbase.thrift.generated.Hbase.get_args;
import org.apache.maven.model.Resource;
import org.apache.maven.project.MavenProject;

public class ConfigFile {
	private static Log log = LogFactory.getLog(ConfigFile.class);

	File file;
	Properties properties;
	String bottomLayer;
	MavenProject project;

	public ConfigFile(MavenProject project, String bottomLayer) {
		this.bottomLayer = bottomLayer;
		this.project = project;
		List<Resource> list = project.getResources();
		Resource resource = list.get(0);
		file = new File(resource.getDirectory() + "/gaes.properties");

		properties = new Properties();
		try {
			if (!file.exists()) {
				properties.put("bottomLayer", "normal");
				properties.store(new FileOutputStream(file), "create by tools");
			}
			properties.load(new FileInputStream(file));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public String getBottomLayerFile() {
		return (String) properties.get("bottomLayer");
	}

	public void setBottomLayerFile(String gae) {
		log.info("changeProperty " + gae);
		try {
			properties.setProperty("bottomLayer", getBottomLayer());
			properties.store(new FileOutputStream(file), "create by tools");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private String getBottomLayer() {
		return bottomLayer;
	}

	public Properties getProperties() {
		return properties;
	}

	public String getGaeEmail() {
		return (String) properties.get("gae.email");
	}

	public String getGaePassword() {
		return (String) properties.get("gae.password");
	}

}
