# This is the main control program
# It will contain the individual control
# codes as functions.
complete_control=open("__control.py","w")

# Check if there exists a controlled element
if controlled_elements:
    user_input=raw_input("Enter the control files. Omit the .py extension and just leave spaces between files --> ")
    control_files=user_input.split()

    control_descs=[]
    for c1 in range(len(control_files)):
        control_descs.append(control_files[c1]+"_desc.csv")

    control_functions=[]
    for c1 in range(len(control_files)):
        control_functions.append(control_files[c1]+"_func")


    for c1 in range(len(control_files)):
        control_files[c1]=control_files[c1]+".py"

    # These lists will contain separate dictionaries
    # for every control file.
    control_file_inputs=[]
    control_file_outputs=[]
    control_file_staticvars=[]
    control_file_timeevents=[]
    
    control_desc_handles=[]
    for c1 in range(len(control_files)):
        # Adding an empty dictionary for a control file.
        control_file_inputs.append({})
        control_file_outputs.append({})
        control_file_staticvars.append({})
        control_file_timeevents.append({})

        # Check if the descriptor exists.
        try:
            control_desc_handles.append(open(control_descs[c1],"r"))

        except:
            # If it doesn't create a blank template.
            control_desc_handles.append(open(control_descs[c1],"w"))

            # Input template
            control_desc_handles[c1].write("Input")
            control_desc_handles[c1].write(", ")
            control_desc_handles[c1].write("Element name in circuit spreadsheet = %s" %(component_objects[meter_list[0]].type+"_"+component_objects[meter_list[0]].tag))
            control_desc_handles[c1].write(", ")
            control_desc_handles[c1].write("Desired variable name in control code = %s" %(component_objects[meter_list[0]].type+"_"+component_objects[meter_list[0]].tag))
            control_desc_handles[c1].write(", ")
            control_desc_handles[c1].write("\n")

            # Output template. Create a line for every
            # control input a particular controlled
            # element has.
            for c2 in range(len(component_objects[controlled_elements[0]].control_tag)):
                control_desc_handles[c1].write("Output")
                control_desc_handles[c1].write(", ")
                control_desc_handles[c1].write("Element name in circuit spreadsheet = %s" %(component_objects[controlled_elements[0]].type+"_"+component_objects[controlled_elements[0]].tag))
                control_desc_handles[c1].write(", ")
                control_desc_handles[c1].write("Control tag defined in parameters spreadhseet = %s" %(component_objects[controlled_elements[0]].control_tag[c2]))
                control_desc_handles[c1].write(", ")
                control_desc_handles[c1].write("Desired variable name in control code = %s" %(component_objects[controlled_elements[0]].control_tag[c2]))
                control_desc_handles[c1].write(", ")
                control_desc_handles[c1].write("\n")

            # Static variable template
            control_desc_handles[c1].write("StaticVariable")
            control_desc_handles[c1].write(", ")
            control_desc_handles[c1].write("Desired variable name in control code = Var1")
            control_desc_handles[c1].write(", ")
            control_desc_handles[c1].write("Initial value of variable = 0.0")
            control_desc_handles[c1].write(", ")
            control_desc_handles[c1].write("\n")

            # Time event template
            control_desc_handles[c1].write("TimeEvent")
            control_desc_handles[c1].write(", ")
            control_desc_handles[c1].write("Desired variable name in control code = t1")
            control_desc_handles[c1].write(", ")
            control_desc_handles[c1].write("First time event = 0.0")
            control_desc_handles[c1].write(", ")
            control_desc_handles[c1].write("\n")

            control_desc_handles[c1].close()