/*
 * 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.List;
import java.util.Map;
import qsardes.molecule.AbstractMoleculeBuilder;
import qsardes.molecule.Molecule;

/**
 * Fragment complexity descriptor abstract class.
 *
 * @author Nicola Chirico
 */
public abstract class DescriptorFragmentComplexity implements Descriptor
{
    protected DescriptorFragmentComplexity()
    {
    }

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

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

    @Override
    public List<String> get_descriptor_names()
    {
        return List.of("fragC");
    }

    @Override
    public Map<String, String> get_descriptor_values(
            AbstractMoleculeBuilder mol_build)
    {
        try {
            return Map.of(get_descriptor_names().get(0),
                    get_fragc(mol_build));
        } catch (Exception ex) {
            System.err.println(ex);
            return Map.of(get_descriptor_names().get(0),
                    String.valueOf(Double.NaN));
        }
    }

    private String get_fragc(AbstractMoleculeBuilder mol_build)
            throws CloneNotSupportedException
    {
        var iac = new Molecule(mol_build).get_atom_container();

        int a = iac.getAtomCount();
        int b = iac.getBondCount();
        int h = 0;

        for (var atom : iac.atoms()) {
            if (atom.getAtomicNumber() != 6) h++;
        }

        return String.valueOf(StrictMath.abs((double)((b * b) - (a * a) + a))
                        + ((double) h / 100.0));
    }

}
