/*
 * 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.Arrays;
import java.util.HashMap;
import qsardes.molecule.Molecule;
import qsardes.molecule.AbstractMoleculeBuilder;
import java.util.List;
import java.util.Map;
import org.openscience.cdk.graph.PathTools;
import static org.openscience.cdk.interfaces.IBond.Order.DOUBLE;
import static org.openscience.cdk.interfaces.IBond.Order.QUADRUPLE;
import static org.openscience.cdk.interfaces.IBond.Order.TRIPLE;

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

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

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

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

        for (String order : order_list) {
            final int ORDER_NUM = Integer.parseInt(order);

            list.add("MPC" + ORDER_NUM);
            list.add("piPC" + ORDER_NUM);
        }

        list.addAll(Arrays.asList("TPC", "TpiPC", "R_TpiPCTPC"));

        return list;
    }

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

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

        return descr_map;
    }

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

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

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

        double tpc = (double) iac.getAtomCount();
        double tpipc = (double) iac.getAtomCount();

        for (String order : order_list) {
            final int ORDER_NUM = Integer.parseInt(order);

            double mpc = 0.0;
            double pipc = 0.0;

            for (var atom : iac.atoms()) {
                var paths = PathTools.getPathsOfLength​(iac, atom, ORDER_NUM);

                for (var atoms : paths) {
                    double prod_bond_order = 1.0;

                    for (int i = 1, n = atoms.size(); i < n; i++) {
                        var bond = iac.getBond(atoms.get(i - 1), atoms.get(i));

                        if (!bond.isAromatic()) {
                            switch (bond.getOrder()) {
                                case DOUBLE ->
                                    prod_bond_order *= 2.0;
                                case TRIPLE ->
                                    prod_bond_order *= 3.0;
                                case QUADRUPLE ->
                                    prod_bond_order *= 4.0;
                            }
                        } else {
                            prod_bond_order *= 1.5;
                        }
                    }

                    pipc += prod_bond_order;
                }

                mpc += paths.size();
            }

            mpc /= 2.0;

            descr_map.put("MPC" + ORDER_NUM, String.valueOf(mpc));

            tpc += mpc;

            double half_pipc = pipc /= 2.0;

            tpipc += half_pipc;

            descr_map.put("piPC" + ORDER_NUM, String.valueOf(
                    StrictMath.log(1.0 + half_pipc))); // To match Dragon descr.
        }

        descr_map.put("TPC", String.valueOf(tpc));

        descr_map.put("TpiPC", String.valueOf(
                StrictMath.log(1.0 + tpipc))); // To match Dragon descr.

        descr_map.put("R_TpiPCTPC", String.valueOf(tpipc / tpc));
    }

    private final List<String> order_list = List.of(
            "1", "2", "3", "4", "5", "6", "7", "8", "9", "10");

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

    private Molecule mol;
}
