# Import Base ComputeCube from Floe
from floe.api import ComputeCube
from orionplatform.mixins import RecordPortsMixin
from orionplatform.parameters import FloatFieldParameter, PrimaryMolFieldParameter

# Note: oechem must be imported before OpenEye toolkits
from openeye.oechem import OECalculateMolecularWeight


# Use RecordsPortMixin to add the default intake, success and failure ports
class MolecularWeightCube(RecordPortsMixin, ComputeCube):

    title = "Example Molecular Weight Cube"
    description = """A Description of the cube"""
    classification = [["Example"]]
    tags = ["Example"]

    mwfield = FloatFieldParameter(
        "mwfield",
        # Default value for the Field Name
        default="Molecular Weight",
        # Indicates that the field must be populated
        required=True,
        # Title that is displayed in the UI
        title="Molecular Weight Field",
        # Description displayed in UI
        description="The field name of the molecule weight field.",
    )

    # Read only Primary Molecule Field
    mol_field = PrimaryMolFieldParameter("mol_field", read_only=True)

    def process(self, record, port):
        # Get the primary molecule using the mol field parameter
        mol = record.get_readonly_reference(self.args.mol_field)
        # Calculate the molecular weight
        mol_weight = OECalculateMolecularWeight(mol, False)
        record.set_value(self.args.mwfield, mol_weight)
        self.success.emit(record)
