/*
 * Copyright (c) 2026 University of Insubria <https://www.uninsubria.it>
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or (at
 * your option) any later version. All we ask is that proper credit is given
 * for our work, which includes - but is not limited to - adding the above
 * copyright notice to the beginning of your source code files, and to any
 * copyright notice that you may distribute with programs based on this work.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation, Inc.
 * <https://fsf.org/>
 */

package qsardes.descriptor;

import java.util.ArrayList;
import java.util.HashMap;
import qsardes.molecule.Molecule;
import qsardes.molecule.AbstractMoleculeBuilder;
import org.openscience.cdk.exception.CDKException;
import org.openscience.cdk.fingerprint.SubstructureFingerprinter;
import java.util.List;
import java.util.Map;
import org.openscience.cdk.interfaces.IAtomContainer;

/**
 * Substructure fingerprints abstract class.
 *
 * @author Nicola Chirico
 */
public abstract class DescriptorFingerprintSubstructure implements Descriptor
{
    protected DescriptorFingerprintSubstructure()
    {
    }

    @Override
    public String toString()
    {
        return "Substructure";
    }

    @Override
    public DescriptorType get_descriptor_type()
    {
        return DescriptorType.valueOf("FINGERPRINT");
    }

    @Override
    public List<String> get_descriptor_names()
    {
        List<String> list = new ArrayList<>();

        for (int i = 0, n = new SubstructureFingerprinter().getSize();
                i < n; i++) {
            list.add("SubFP" + String.valueOf(i + 1));
        }

        return list;
    }

    @Override
    public Map<String, String> get_descriptor_values(
            AbstractMoleculeBuilder mol_build)
    {
        try {
            return build_fingerprint_map(
                    new Molecule(mol_build).get_atom_container());
        } catch (Exception ex) {
            System.err.println(ex);
            return get_empty_map();
        }
    }

    private Map<String, String> build_fingerprint_map(IAtomContainer iac)
            throws CDKException
    {
        var bit_set = new SubstructureFingerprinter().getFingerprint(iac);

        Map<String, String> map = new HashMap<>();

        int index = 0;

        for (String name : get_descriptor_names()) {
            map.put(name, String.valueOf(bit_set.get(index) ? 1 : 0));
            index++;
        }

        return map;
    }

    private Map<String, String> get_empty_map()
    {
        Map<String, String> map = new HashMap<>();

        final String NAN = String.valueOf(Double.NaN);

        get_descriptor_names().forEach(name -> map.put(name, NAN));

        return map;
    }
}
