/**
 * ツールクラスの候補。DiffResultDetailから行を抽出するユーティリティ
 */
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.io.BlancoNLpackGeneratorDiffResultDetailCsvWriter;
import blanco.nlpack.generator.csv.record.BlancoNLpackGeneratorDiffResultDetailCsvRecord;

/**
 * クラス名は未定。
 */
public class SelectDiffResultDetail {
    public static final void main(final String[] args) throws IOException,
            BlancoCsvIOException {
        System.out.println("行の抽出: begin");
        new SelectDiffResultDetail()
                .process(
                        new File(
                                "tmp/phase4.diff/ResultDetailDiffEclipse3.3to3.4.csv"),
                        new File(
                                "tmp/phase4.diff/ResultDetailDiffEclipse3.3to3.4Selected.csv"));
        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 BlancoNLpackGeneratorDiffResultDetailCsvWriter writer = new BlancoNLpackGeneratorDiffResultDetailCsvWriter(
                new BufferedWriter(new OutputStreamWriter(new FileOutputStream(
                        fileOutput), "Windows-31J")));
        writer.writeTitle();
        for (;;) {
            if (BlancoNLpackGeneratorResultDetailUtil
                    .checkValidResultDetailRecordByFieldBundleUri(reader
                            .getReader()) == false) {
                // その行は読み飛ばします。
                reader.getReader().readLine();
                continue;
            }
            final BlancoNLpackGeneratorDiffResultDetailCsvRecord record = reader
                    .readRecord();
            if (record == null) {
                break;
            }

            // 条件部分
            if (record.getLeftAction().equals("hit.p")
                    && record.getRightAction().equals("miss")) {
                writer.writeRecord(record);
            }
        }
        reader.close();
        writer.close();
    }
}
