#import System
import ctypes  # An included library with Python install.
import datetime
import Visure

PROJECT         = "ProtonMed"
SPECIFICATION   = "Product Requirements"
ATTRIBUTE       = "Status"
INITIAL_STATUS  = "New"
APPROVED_STATUS  = "Approved"


def Visure_beforeChangeDescription(bl, lID, lCRC32):
    # The following script prevents users from modifying a description if the item is in Approved state
    try:
        # The bl.Trace_INFO option will ouput a given string in the log file of the server, either for debuggin purposes, or for logging
        # As as example, if uncommented, the following line would print "Start of the script" in the server's log file: 
        #bl.Trace_INFO("Start of script")
        # By default the editing of the description will be possible
        result = True
        # Validate that we´re on the selected project
        if PROJECT == bl.GetProjectName() :
            # Identify the item being modified
            item = bl.item(lID)
            # Retrieve the specification the item belongs to
            specification = bl.specification(SPECIFICATION)
            # Validate that the item belongs to the corresponding specification
            if specification.containsItem(lID) :
                # Retrieve the corresponding attribute
                attribute = bl.attribute(ATTRIBUTE)
                if (item.has_attribute(attribute.id)) :
                    # Retrieve the attribute values
                    enum_value = 0
                    type = attribute.type
                    # Find the ID of the attribute value 
                    for ev in type.enum_values:
                        # Once we find the attribute value, retrieve the ID 
                        if (ev.name == APPROVED_STATUS) :
                            enum_value = ev.id
                    # If the item takes the attribute value, set the trigger to false so that description cannot be edited 
                    if item.value(attribute.id) == enum_value:
                        result = False

        return result
    except Exception as e:
        #bl.Trace_ERROR(str(e))
        return True