#!/usr/bin/env python

"""Parser for reaction strings and files."""

import libsbml
import re
import sympy as sp
from sympy.abc import _clash

from .crncomplex import Complex
from .reaction import Reaction

__author__ = "Elisa Tonello"
__copyright__ = "Copyright (c) 2016, Elisa Tonello"
__license__ = "BSD"
__version__ = "0.0.1"


def parse_reaction(r):
    """Parse a (potentially reversible) reaction written as

    reactionid: i1 R1 + ... + in Rn (k\_)<->(k) j1 P1 + ... + jm Pm

    where (k\_)< is optional.
    Return a two-element tuple with two reactions, if "<" is included
    (the reaction is reversible),
    or one reaction and None, if it is not reversible.
    k, k\_ can be any string representing the rates. They are optional. If present,
    they must be enclosed in parenthesis.
    No spaces are allowed between the parenthesis and <->.
    reactionid is optional. If the reaction is reversible, "_rev"
    is added to the reactionid for the reverse reaction.
    Everything after a # sign is ignored.

    :type r: string
    :param r: string of the form "reactionid: i1 R1 + ... + in Rn (k\_)<->(k) j1 P1 + ... + jm Pm".
    :rtype: (Reaction, Reaction/None).
    """
    # Everything after a # sign is ignored.
    reaction = r.split("#")[0]

    # Check for reaction id.
    colon = reaction.split(":")
    if len(colon) > 2:
        raise ValueError("Unrecognised reaction. More then one colon in reaction definition.")
    if len(colon) == 2:
        reactionid = colon[0].strip()
        reactionBody = colon[1]