/*
 * 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.atomconst;

import qsardes.molecule.Molecule;
import java.util.HashMap;
import java.util.Map;
import org.openscience.cdk.exception.CDKException;
import org.openscience.cdk.interfaces.IAtom;
import org.openscience.cdk.interfaces.IAtomContainer;

/**
 * Gets the atomic instrinsic state (hydrogens included).
 *
 * @author Nicola Chirico
 */
public final class AtomConstIntrStateH implements AtomConstFunctionCall
{
    public AtomConstIntrStateH()
    {
    }

    @Override
    public AtomConstResult call(Molecule mol) throws CDKException
    {
        IAtomContainer iac = mol.get_atom_container();

        final int ATOM_COUNT = iac.getAtomCount();

        RetVal ret_val = get_istates(mol);

        Map<IAtom, Double> map = new HashMap<>();
        double[] const_val = ret_val.istates();

        for (int i = 0; i < ATOM_COUNT; i++) {
            IAtom atom = iac.getAtom(i);
            map.put(atom, const_val[i]);
        }

        return new AtomConstResult(ret_val.carbon(), map, const_val);
    }

    private record RetVal(double[] istates, double carbon)
    {
    }

    private RetVal get_istates(Molecule mol) throws CDKException
    {
        IAtomContainer iac = mol.get_atom_container();

        double[] istate = new double[iac.getAtomCount()];

        mol.convert_impl_to_expl_hydrogens();

        int index = 0;

        double c_istate_sum = 0.0;
        int c_num = 0;

        for (int i = 0, n = iac.getAtomCount(); i < n; i++) {
            var atom = iac.getAtom(i);
            int atom_num = atom.getAtomicNumber();

            if (atom_num > 1) {
                istate[index] = mol.calc_intrinsic_state_molconn(atom)
                        .i_state();

                index++;

                if (atom_num == 6) {
                    c_istate_sum += istate[i];
                    c_num++;
                }
            }
        }

        mol.remove_hydrogens();

        return new RetVal(istate, c_istate_sum / (double) c_num);
    }
}
