package org.agmip.acmo.translators.dssat.core;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.CharArrayReader;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import static org.agmip.acmo.translators.dssat.core.AcmoCommonOutput.*;
import static org.agmip.util.MapUtil.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * DSSAT AFile Data I/O API Class
 *
 * @author Meng Zhang
 * @version 1.0
 */
public class AcmoDssatCsvOutput extends AcmoCommonOutput {

    private static final Logger log = LoggerFactory.getLogger(AcmoDssatCsvOutput.class);

    /**
     * Get output file object
     */
    @Override
    public File getOutputFile() {
        return outputFile;
    }

    /**
     * Generate ACMO CSV file
     *
     * @param outputCsvPath The path for output csv file
     * @param data The data holder for model output data and meta data
     */
    @Override
    public void writeFile(String outputCsvPath, Map data) throws IOException {

        HashMap sumData = getObjectOr(data, "summary", new HashMap());
        ArrayList<HashMap> sumSubArr = getObjectOr(sumData, "data", new ArrayList<HashMap>());
        HashMap sumSubData;

        Object buf = data.get("meta");
        BufferedReader brCsv;
        // If Output File File is no been found
        if (buf == null) {
            log.error("META DATA FILE IS MISSING");
            return;
        } else {
            if (buf instanceof char[]) {
                brCsv = new BufferedReader(new CharArrayReader((char[]) buf));
            } else {
                brCsv = (BufferedReader) buf;
            }
        }

        // Get Model version
        String version = getObjectOr(sumData, "vevsion", "Ver. N/A");
        StringBuilder sbData;

        // Write CSV File
        outputCsvPath = revisePath(outputCsvPath);
        outputFile = createCsvFile(outputCsvPath);
        BufferedWriter bw = new BufferedWriter(new FileWriter(outputFile));
        String line;
        while ((line = brCsv.readLine()) != null) {
            if (line.startsWith("*") || line.startsWith("\"*\"")) {
                break;
            } else {
                bw.write(line);
                bw.write("\r\n");
            }
        }

        // Write data
        int index = 0;
        do {
            // Check if the line is ended with comma
            if (line.endsWith(",") && line.matches(".+,[^,]+,$")) {
                line = line.substring(0, line.length() - 1);
            }
            bw.write(line);

            // wirte simulation output info
            if (index < sumSubArr.size()) {
                sbData = new StringBuilder();
                sumSubData = sumSubArr.get(index);
                sbData.append(",\"DSSAT ").append(getObjectOr(sumSubData, "model", "")).append(" ").append(version).append("\""); // MODEL_VER
                sbData.append(",\"").append(getObjectOr(sumSubData, "hwah", "")).append("\""); // HWAH
                sbData.append(",\"").append(getObjectOr(sumSubData, "cwam", "")).append("\""); // CWAH
                sbData.append(",\"").append(formatDateStr(getObjectOr(sumSubData, "adat", ""))).append("\""); // ADAT
                sbData.append(",\"").append(formatDateStr(getObjectOr(sumSubData, "mdat", ""))).append("\""); // MDAT
                sbData.append(",\"").append(formatDateStr(getObjectOr(sumSubData, "hdat", ""))).append("\""); // HDATE
                sbData.append(",\"").append(getObjectOr(sumSubData, "laix", "")).append("\""); // LAIX
                sbData.append(",\"").append(getObjectOr(sumSubData, "prcp", "")).append("\""); // PRCP
                sbData.append(",\"").append(getObjectOr(sumSubData, "etcp", "")).append("\""); // ETCP
                sbData.append(",\"").append(getObjectOr(sumSubData, "nucm", "")).append("\""); // NUCM
                sbData.append(",\"").append(getObjectOr(sumSubData, "nlcm", "")).append("\""); // NLCM
                bw.write(sbData.toString());
            }

            bw.write("\r\n");
            index++;
        } while((line = brCsv.readLine()) != null);

        // Check if there is record not been output yet
        if (index != sumSubArr.size()) {
            log.warn("The number of records in Summary.out does not match the metadata.");
        }

        bw.close();
    }

    private File createCsvFile(String outputCsvPath) {
        File f = new File(outputCsvPath + "ACMO_DSSAT.csv");
        int count = 1;
        while (f.exists()) {
            f = new File(outputCsvPath + "ACMO_DSSAT (" + count + ").csv");
            count++;
        }
        return f;
    }
}

@@ -355,24 +355,61 @@
 onOutput.*;%0A
+import org.agmip.acmo.util.AcmoUtil;%0A
 import stati
@@ -1998,16 +1998,25 @@
 tFile = 
+AcmoUtil.
 createCs
@@ -2026,32 +2026,41 @@
 le(outputCsvPath
+, %22DSSAT%22
 );%0A        Buffe
@@ -4477,307 +4477,7 @@
    %7D
-%0A%0A    private File createCsvFile(String outputCsvPath) %7B%0A        File f = new File(outputCsvPath + %22ACMO_DSSAT.csv%22);%0A        int count = 1;%0A        while (f.exists()) %7B%0A            f = new File(outputCsvPath + %22ACMO_DSSAT (%22 + count + %22).csv%22);%0A            count++;%0A        %7D%0A        return f;%0A    %7D
 %0A%7D%0A

Move createCsvFile to AcmoUtil for common using