/*
 * 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.List;
import qsardes.cdk.CdkBuilder;
import qsardes.molecule.AbstractMoleculeBuilder;
import java.util.Map;
import org.openscience.cdk.qsar.descriptors.molecular.RotatableBondsCountDescriptor;
import qsardes.cdk.CdkTools;

/**
 * Rotatable bonds count descriptors abstract class.
 *
 * @author Nicola Chirico
 */
public abstract class DescriptorRotatableBondsCount implements Descriptor
{
    protected DescriptorRotatableBondsCount(Boolean[] params_no_term_bonds,
            Boolean[] params_term_bonds)
    {
        this.params_no_term_bonds = params_no_term_bonds;
        this.params_term_bonds = params_term_bonds;
    }

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

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

    @Override
    public List<String> get_descriptor_names()
    {
        List<String> list = new ArrayList<>(
                new CdkTools().get_descriptor_names(
                        new RotatableBondsCountDescriptor(),
                        params_no_term_bonds));

        list.addAll(new CdkTools().get_descriptor_names(
                        new RotatableBondsCountDescriptor(),
                        params_term_bonds));

        list.add("RotBFrac");
        list.add("RotBtFrac");

        return list;
    }

    @Override
    public Map<String, String> get_descriptor_values(
            AbstractMoleculeBuilder mol_build)
    {
        var map = new CdkBuilder(new RotatableBondsCountDescriptor(),
                params_no_term_bonds, mol_build).calc_descr_impl_hydrogens();

        var map_t = new CdkBuilder(new RotatableBondsCountDescriptor(),
                params_term_bonds, mol_build).calc_descr_impl_hydrogens();

        map.putAll(map_t);

        try {
            map.put("RotBFrac", String.valueOf(
                    Double.parseDouble(map.get("nRotB"))
                    / (double) mol_build.get_bond_count()));
        } catch (Exception ex) {
            System.err.println(ex);
            map.put("RotBFrac", String.valueOf(Double.NaN));
        }

        try {
            map.put("RotBtFrac", String.valueOf(
                    Double.parseDouble(map.get("nRotBt"))
                    / (double) mol_build.get_bond_count()));
        } catch (Exception ex) {
            System.err.println(ex);
            map.put("RotBtFrac", String.valueOf(Double.NaN));
        }

        return map;
    }

    private final Boolean[] params_no_term_bonds;
    private final Boolean[] params_term_bonds;
}
