package ac.ed.lurg.output;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Map.Entry;

import ac.ed.lurg.landuse.Intensity;
import ac.ed.lurg.landuse.LandUseItem;
import ac.ed.lurg.types.CropType;
import ac.ed.lurg.types.LandCoverType;
import ac.ed.lurg.utils.LogWriter;
import ac.sac.raster.RasterKey;
import ac.sac.raster.RasterSet;

public class LandUseOutputer extends AbstractLandUseOutputer {

	public LandUseOutputer(int year, RasterSet<LandUseItem> landUseRaster) {
		super(year, landUseRaster);
	}

	@Override
	public void writeOutput() {
		File outputDir = getOutputDir(year);
		BufferedWriter fertWriter = null;
		
		try {
			String landCoverFileName = outputDir.getPath() + File.separator + "LandUse.txt";
			fertWriter = new BufferedWriter(new FileWriter(landCoverFileName, false));
		
			StringBuffer sbHeader = new StringBuffer("Lon Lat area protected");
			
			for (LandCoverType cover : LandCoverType.values()) {
				sbHeader.append(" " + cover.getName());
			}
			
			for (CropType crop : CropType.getAllItems()) {
				String cropString = crop.getGamsName();
				sbHeader.append(" " + cropString + "_A " + cropString + "_FI " + cropString + "_FQ " + 
							cropString + "_II " + cropString + "_IQ " + cropString + "_OI " + cropString + "_Y");
			}
			fertWriter.write(sbHeader.toString());
			fertWriter.newLine();

			for (Entry<RasterKey, LandUseItem> entry : landUseRaster.entrySet()) {
				RasterKey key = entry.getKey();
				LandUseItem item = entry.getValue();
				
				if (item == null)
					continue;
				
				double lat = landUseRaster.getXCoordin(key);
				double lon = landUseRaster.getYCoordin(key);

				StringBuffer sbData = new StringBuffer(String.format("%.2f %.2f", lat, lon));

				sbData.append(String.format(" %.8f", item.getTotalLandCoverArea()));
				sbData.append(String.format(" %.8f", item.getProtectedAreaIncMinNatural()));

				for (LandCoverType cover : LandCoverType.values()) {
					sbData.append(String.format(" %.8f", item.getLandCoverArea(cover)));
				}

				for (CropType crop : CropType.getAllItems()) {
					double cropFract = item.getCropFraction(crop);
					Intensity intensity = item.getIntensity(crop);
					double fertI = intensity==null ? 0.0 : intensity.getFertiliserIntensity();
					double fertQ = intensity==null ? 0.0 : intensity.getFertiliserAmount();
					double irrigI = intensity==null ? 0.0 : intensity.getIrrigationIntensity();
					double irrigQ = intensity==null ? 0.0 : intensity.getIrrigationRate();
					double otherI = intensity==null ? 0.0 : intensity.getOtherIntensity();
					double yield = intensity==null ? 0.0 : intensity.getYield();
					sbData.append(String.format(" %.8f %.8f %.8f %.8f %.8f %.8f %.8f", cropFract, fertI, fertQ, irrigI, irrigQ, otherI, yield));
				}
				
				fertWriter.write(sbData.toString());
				fertWriter.newLine();
			}
		}
		catch (IOException e) {
			LogWriter.print(e);
		}
		finally {
			if (fertWriter != null) {
				try {
					fertWriter.close();
				} 
				catch (IOException e) {
					LogWriter.print(e);
				}
			}
		}
		
	}

}