/* FichothequeLib_API - Copyright (c) 2011-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.utils;

import java.io.IOException;
import java.util.List;
import net.fichotheque.corpus.metadata.FieldKey;
import net.fichotheque.format.FichothequeFormatDef;
import net.fichotheque.format.FormatSourceKey;
import net.mapeadores.util.format.FormatDef;


/**
 *
 * @author Vincent Calame
 */
public final class FichothequeFormatUtils {

    private FichothequeFormatUtils() {
    }

    public static FieldKey toFieldKey(Object value) {
        if (value == null) {
            return null;
        }
        try {
            return (FieldKey) value;
        } catch (ClassCastException cce) {
            return null;
        }
    }

    public static String toLines(FichothequeFormatDef formatDef) {
        StringBuilder buf = new StringBuilder();
        try {
            appendLines(formatDef, buf);
        } catch (IOException ioe) {
        }
        return buf.toString();
    }

    public static void appendLines(FichothequeFormatDef formatDef, Appendable appendable) throws IOException {
        String[] array = FichothequeFormatUtils.toStringArray(formatDef);
        appendable.append(array[0]);
        appendable.append('\n');
        String format = array[1];
        String param = array[2];
        if (format == null) {
            if (param != null) {
                appendable.append("-\n");
            }
        } else {
            appendable.append(format);
            appendable.append('\n');
        }
        if (param != null) {
            appendable.append(param);
            appendable.append('\n');
        }
    }

    public static String[] toStringArray(FichothequeFormatDef formatDef) {
        StringBuilder buf = new StringBuilder();
        for (FormatSourceKey formatSourceKey : formatDef.getFormatSourceKeyList()) {
            if (buf.length() > 0) {
                buf.append(',');
            }
            buf.append(formatSourceKey);
        }
        String paramLine = ParamLineConverter.convert(formatDef);
        if (paramLine.isEmpty()) {
            paramLine = null;
        }
        String[] result = new String[3];
        result[0] = buf.toString();
        result[1] = formatPatternListToString(formatDef);
        result[2] = paramLine;
        return result;
    }

    public static String getFormatPatternBySourceIndex(FichothequeFormatDef formatDef, int sourceIndex) {
        List<FormatDef.Pattern> patternList = formatDef.getPatternList();
        int size = patternList.size();
        if (size == 0) {
            return null;
        }
        if (sourceIndex >= size) {
            sourceIndex = size - 1;
        }
        return getFormatPattern(patternList, sourceIndex);
    }

    private static String getFormatPattern(List<FormatDef.Pattern> patternList, int i) {
        FormatDef.Pattern formatPattern = patternList.get(i);
        if (!formatPattern.isDefault()) {
            return formatPattern.getPattern();
        }
        if (i == 0) {
            return null;
        }
        return getFormatPattern(patternList, i - 1);
    }

    public static String formatPatternListToString(FichothequeFormatDef formatDef) {
        List<FormatDef.Pattern> patternList = formatDef.getPatternList();
        if (patternList.isEmpty()) {
            return null;
        }
        StringBuilder buf = new StringBuilder();
        for (FormatDef.Pattern pattern : patternList) {
            if (buf.length() > 0) {
                buf.append("||");
            }
            if (pattern.isDefault()) {
                buf.append(' ');
            } else {
                buf.append(pattern.getPattern());
            }
        }
        return buf.toString();
    }

    public static boolean isSame(FichothequeFormatDef formatDef1, FichothequeFormatDef formatDef2) {
        String stg1 = toLines(formatDef1);
        String stg2 = toLines(formatDef2);
        return stg1.equals(stg2);
    }

}
