/* FichothequeLib_Tools - Copyright (c) 2013-2025 Vincent Calame - Exemole
 * Logiciel libre donné sous triple licence :
 * 1) selon les termes de la CeCILL V2
 * 2) selon les termes de l’EUPL V.1.1
 * 3) selon les termes de la GNU GPLv3
 * Voir le fichier licences.txt
 */


package net.fichotheque.tools.importation.parsers.corpus;

import java.text.ParseException;
import net.fichotheque.corpus.Corpus;
import net.fichotheque.corpus.FicheMeta;
import net.fichotheque.corpus.metadata.FieldKey;
import net.fichotheque.tools.importation.ParseResultBuilder;
import net.fichotheque.tools.importation.corpus.RemoveCorpusImportBuilder;
import net.fichotheque.tools.importation.parsers.Row;


/**
 *
 * @author Vincent Calame
 */
class RemoveRowParser extends CorpusRowParser {

    private final RemoveCorpusImportBuilder removeCorpusImportBuilder;
    private final ParseResultBuilder parseResultBuilder;
    private final int idIndex;

    RemoveRowParser(RemoveCorpusImportBuilder removeCorpusImportBuilder, ParseResultBuilder parseResultBuilder, int idIndex) {
        this.removeCorpusImportBuilder = removeCorpusImportBuilder;
        this.parseResultBuilder = parseResultBuilder;
        this.idIndex = idIndex;
    }

    @Override
    public RemoveCorpusImportBuilder getCorpusImportBuilder() {
        return removeCorpusImportBuilder;
    }

    @Override
    public void parseRow(int rowNumber, Row row) {
        if (row.getColumnCount() <= idIndex) {
            parseResultBuilder.missingIdColumn(rowNumber);
            return;
        }
        String idString = row.getColumnValue(idIndex).trim();
        Corpus corpus = removeCorpusImportBuilder.getCorpus();
        FicheMeta ficheMeta = null;
        try {
            int id = Integer.parseInt(idString);
            ficheMeta = corpus.getFicheMetaById(id);
            if (ficheMeta == null) {
                parseResultBuilder.unknownId(rowNumber, idString);
            }
        } catch (NumberFormatException nfe) {
            parseResultBuilder.notIntegerId(rowNumber, idString);
        }
        if (ficheMeta != null) {
            removeCorpusImportBuilder.add(ficheMeta);
        }
    }

    static RemoveRowParser newInstance(String[] fieldArray, RemoveCorpusImportBuilder removeCorpusImportBuilder, ParseResultBuilder parseResultBuilder) {
        int fieldIndex = -1;
        int length = fieldArray.length;
        for (int i = 0; i < length; i++) {
            String field = fieldArray[i].trim();
            FieldKey fieldKey;
            try {
                fieldKey = FieldKey.parse(field);
            } catch (ParseException pe) {
                continue;
            }
            if (isIdField(fieldKey)) {
                if (fieldIndex == -1) {
                    fieldIndex = i;
                }
            }
        }
        if (fieldIndex == -1) {
            parseResultBuilder.missingColumn(FieldKey.SPECIAL_ID);
        }
        if (fieldIndex != -1) {
            return new RemoveRowParser(removeCorpusImportBuilder, parseResultBuilder, fieldIndex);
        } else {
            return null;
        }
    }

    private static boolean isIdField(FieldKey fieldKey) {
        switch (fieldKey.getKeyString()) {
            case FieldKey.SPECIAL_ID:
                return true;
            default:
                return false;
        }
    }

}
