/*
 * 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.Arrays;
import qsardes.molecule.Molecule;
import qsardes.molecule.AbstractMoleculeBuilder;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.openscience.cdk.interfaces.IAtomContainer;
import org.openscience.cdk.interfaces.IBond;

/**
 * Bond count descriptors abstract class.
 *
 * @author Nicola Chirico
 */
public abstract class DescriptorBondCount implements Descriptor
{
    protected DescriptorBondCount()
    {
    }

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

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

    @Override
    public List<String> get_descriptor_names()
    {
        return Arrays.asList(
                "nBonds",   "nBonds2", "nBondsS", "nBondsS2",
                "nBondsS3", "nBondsD", "nBondsT", "nBondsQ",
                "nBondsM");
    }

    @Override
    public Map<String, String> get_descriptor_values(
            AbstractMoleculeBuilder mol_build)
    {
        prepare_descr_map();

        try {
            mol = new Molecule(mol_build);
            normalize_aromaticity();
            calculate_descr();
        } catch (Exception ex) {
            System.err.println(ex);
        }

        return descr_map;
    }

    private void normalize_aromaticity()
    {
        for (var bond : mol.get_atom_container().bonds()) {
            if ((bond.getOrder() == IBond.Order.DOUBLE)
                    && bond.isAromatic()) {
                bond.setOrder(IBond.Order.SINGLE);
            }

            bond.atoms().forEach(atom -> atom.setIsAromatic(
                    bond.isAromatic()));
        }
    }

    private void prepare_descr_map()
    {
        final String NAN = String.valueOf(Double.NaN);

        get_descriptor_names().forEach(item -> descr_map.put(item, NAN));
    }

    private void calculate_descr()
    {
        IAtomContainer iac = mol.get_atom_container();

        int bond_sum = 0;
        int bond_s_sum = 0;
        int bond_d_sum = 0;
        int bond_t_sum = 0;
        int bond_q_sum = 0;

        int arom_bond_sum = 0;
        int arom_s_bond_sum = 0;

        int impl_h_sum = 0;

        for (var bond : iac.bonds()) {
            switch (bond.getOrder()) {
                case SINGLE -> {
                    bond_s_sum++;

                    if (bond.isAromatic()) {
                        arom_s_bond_sum++;
                    }
                }

                case DOUBLE -> {
                    bond_d_sum++;
                }

                case TRIPLE ->
                    bond_t_sum++;

                case QUADRUPLE ->
                    bond_q_sum++;
            }

            if (bond.isAromatic()) {
                arom_bond_sum++;
            }

            bond_sum++;
        }

        for (var atom : iac.atoms()) {
            impl_h_sum += atom.getImplicitHydrogenCount();
        }

        descr_map.put("nBonds", String.valueOf(bond_sum));

        descr_map.put("nBonds2", String.valueOf(bond_sum + impl_h_sum));

        descr_map.put("nBondsS", String.valueOf(bond_s_sum + impl_h_sum));

        descr_map.put("nBondsS2", String.valueOf(
                bond_s_sum + impl_h_sum - arom_s_bond_sum));

        descr_map.put("nBondsS3", String.valueOf(bond_s_sum - arom_s_bond_sum));

        descr_map.put("nBondsD", String.valueOf(bond_d_sum));

        descr_map.put("nBondsT", String.valueOf(bond_t_sum));

        descr_map.put("nBondsQ", String.valueOf(bond_q_sum));

        descr_map.put("nBondsM", String.valueOf(
                bond_d_sum + bond_t_sum + bond_q_sum + arom_bond_sum));
    }

    private final Map<String, String> descr_map = new LinkedHashMap<>();

    private Molecule mol;
}
