/**
 * ツールクラスの候補。DiffResultDetailからPleiades形式に変換するユーティリティ
 */
package blanco.nlpack.generator.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import blanco.nlpack.generator.BlancoNLpackGeneratorResultDetailUtil;
import blanco.nlpack.generator.csv.io.BlancoCsvIOException;
import blanco.nlpack.generator.csv.io.BlancoNLpackGeneratorDiffResultDetailCsvReader;
import blanco.nlpack.generator.csv.record.BlancoNLpackGeneratorDiffResultDetailCsvRecord;
import blanco.properties.BlancoPropertiesUtil;

/**
 * クラス名は未定。
 */
public class DiffResultDetailToPleiades {
    public static final void main(final String[] args) throws IOException,
            BlancoCsvIOException {
        System.out.println("プロパティ化: begin");
        new DiffResultDetailToPleiades()
                .process(
                        new File(
                                "tmp/phase4.diff/ResultDetailDiffEclipse3.3to3.4Selected.csv"),
                        new File(
                                "tmp/phase4.diff/ResultDetailDiffEclipse3.3to3.4Selected.properties"));
        System.out.println("プロパティ化: end");
    }

    public void process(final File fileInput, final File fileOutput)
            throws IOException, BlancoCsvIOException {
        final BlancoNLpackGeneratorDiffResultDetailCsvReader reader = new BlancoNLpackGeneratorDiffResultDetailCsvReader(
                new BufferedReader(new InputStreamReader(new FileInputStream(
                        fileInput), "Windows-31J")));
        final BufferedWriter writer = new BufferedWriter(new BufferedWriter(
                new OutputStreamWriter(new FileOutputStream(fileOutput),
                        "UTF-8")));
        for (;;) {
            if (BlancoNLpackGeneratorResultDetailUtil
                    .checkValidResultDetailRecordByFieldBundleUri(reader
                            .getReader()) == false) {
                // その行は読み飛ばします。
                reader.getReader().readLine();
                continue;
            }
            final BlancoNLpackGeneratorDiffResultDetailCsvRecord record = reader
                    .readRecord();
            if (record == null) {
                break;
            }

            writer.write("# uri  : " + record.getBundleUri());
            writer.newLine();
            writer.write("# left : " + record.getLeftAction());
            writer.newLine();
            writer.write(BlancoPropertiesUtil.getPropertiesLine(record
                    .getLeftInput(), record.getLeftOutput()));
            writer.newLine();
            writer.write("# right: " + record.getRightAction());
            writer.newLine();
            writer.write(BlancoPropertiesUtil.getPropertiesLine(record
                    .getRightInput(), record.getRightOutput()));
            writer.newLine();
            writer.newLine();
        }
        reader.close();
        writer.close();
    }
}
