#---------------------------------#
# source files to be included     #
#---------------------------------#
source light.tcl
source output_light.tcl

#--------------------------#
# default light position   #
#--------------------------#



    set xl1 35
    set yl1 35
    set xl2 65
    set yl2 35
    set xl3 70
    set yl3 70
    set xl4 115
    set yl4 70
    set xl5 115
    set yl5 35
    set xl6 150
    set yl6 35
    set xl7 150
    set yl7 175
    set xl8 115
    set yl8 130
    set xl9 70
    set yl9 130
    set xl10 35
    set yl10 175


    for {set i 1} { $i <= 10 } {incr i} {
    	set light_out($i) ""
    }



#--------------------------#
# default light output     #
#--------------------------#

set light_out(1) "Digital Gesout 1"
set light_out(2) "Digital Gesout 2"
set light_out(3) "Digital Gesout 3"
set light_out(4) "Digital Gesout 4"
set light_out(5) "Digital Gesout 5"
set light_out(6) "Digital Gesout 6"
set light_out(7) "Digital Gesout 7"
set light_out(8) "Digital Gesout 8"
set light_out(9) "Digital Gesout 9"
set light_out(10) "Digital Gesout 10"


#-------------------------------------------------------------------
#
#
#-------------------------------------------------------------------


proc light_menu { } {

	# textvariable from output connection
	global light_out


	#--------------------------#
	# Initialize variables     #
	#--------------------------#
	set num_of_light 10
	set multiplier 30
	set offset 40

	set light .light_menu




	# Check if the window has already exist
	if [winfo exists $light] {
		destroy $light
	}

	# Create the window
	global env
	set env(DISPLAY) :0
	toplevel $light -width 375 -height [expr {$num_of_light * $multiplier + $offset +100 }]



	# "Light Position" title
	label $light.title -text "Light Position" -fg blue
	place $light.title -x 10 -y 7

	# "Light Output" title
	label $light.name_title -text "Light Output" -fg blue
	place $light.name_title -x 220 -y 7

	# "Name" label
	label $light.name_label -text "Name"
	place $light.name_label -x 10 -y 40

	# "Name" entry
	entry $light.name_entry -bg white -textvariable light_name
	place $light.name_entry -width 120 -x 60 -y 40

	# "x" label
	label $light.xlabel -text "x"
	place $light.xlabel -x 200 -y 40

	# "x" entry
	entry $light.xentry -bg white -textvariable light_xpos
	place $light.xentry -width 45 -x 215 -y 40

	# "y" label
	label $light.ylabel -text "y"
	place $light.ylabel -x 275 -y 40

	# "y" entry
	entry $light.yentry -bg white -textvariable light_ypos
	place $light.yentry -width 45 -x 290 -y 40





    	#---------------------------------------------------------------
    	# Bind the motion of the mouse to display the mouse
    	# coordinate at current time
    	#---------------------------------------------------------------

    	bind .ventana <ButtonRelease-1> {
    		global light_xpos light_ypos
    		#puts "%x %y"
     		set light_xpos %x
     		set light_ypos %y
    	}	





   	for {set i 1} {$i <= $num_of_light} {incr i} {
   	


      		# The number of lights -> displayed on the left of the output field
      		label $light.count$i -text "$i"
      		place $light.count$i -x 170 -y [expr { ($i * $multiplier) + $offset }]

      		# Connection entry
      		entry $light.out$i -bg white -width 15 -textvariable light_out($i)
      		place $light.out$i -x 200 -y [expr { ($i * $multiplier) + $offset }]

      		# x label
      		label $light.xl$i -text "x"
      		place $light.xl$i -x 10 -y [expr { ($i * $multiplier) + $offset }]

     		# y label
      		label $light.yl$i -text "y"
      		place $light.yl$i -x 80 -y [expr { ($i * $multiplier) + $offset }]

      		# x entry
      		entry $light.xe$i -bg white -width 5  -textvariable xl$i
      		place $light.xe$i -x 25 -y [expr { ($i * $multiplier) + $offset }]

      		# y entry
      		entry $light.ye$i -bg white -width 5  -textvariable yl$i
      		place $light.ye$i -x 105 -y [expr { ($i * $multiplier) + $offset }]


   	# end of for loop
   	}

   	#-------------------------------------------------------------#
   	# Button to open "output_light.tcl" to choose which output the light is going to go to        #
   	#-------------------------------------------------------------#

    	button $light.outbut1 -text v  -command {output_light out1}
     	place $light.outbut1 -x 315 -y [expr { (1 * $multiplier) + $offset -3}]
      	button $light.outbut2 -text v  -command {output_light out2}
      	place $light.outbut2 -x 315 -y [expr { (2 * $multiplier) + $offset -3}]
      	button $light.outbut3 -text v  -command {output_light out3}
      	place $light.outbut3 -x 315 -y [expr { (3 * $multiplier) + $offset -3}]
      	button $light.outbut4 -text v  -command {output_light out4}
      	place $light.outbut4 -x 315 -y [expr { (4 * $multiplier) + $offset -3}]
      	button $light.outbut5 -text v  -command {output_light out5}
      	place $light.outbut5 -x 315 -y [expr { (5 * $multiplier) + $offset -3}]
      	button $light.outbut6 -text v  -command {output_light out6}
      	place $light.outbut6 -x 315 -y [expr { (6 * $multiplier) + $offset -3}]
     	button $light.outbut7 -text v  -command {output_light out7}
     	place $light.outbut7 -x 315 -y [expr { (7 * $multiplier) + $offset -3}]
      	button $light.outbut8 -text v  -command {output_light out8}
      	place $light.outbut8 -x 315 -y [expr { (8 * $multiplier) + $offset -3}]
      	button $light.outbut9 -text v  -command {output_light out9}
      	place $light.outbut9 -x 315 -y [expr { (9 * $multiplier) + $offset -3}]
      	button $light.outbut10 -text v  -command {output_light out10}
      	place $light.outbut10 -x 315 -y [expr { (10 * $multiplier) + $offset -3}]

	# OK button
	button $light.ok -text "Ok" -command {send_to_light_db; draw_light }
	place $light.ok -x 150 -y [expr { ($i * $multiplier) + $offset + 20}]

	# Remove button
	button $light.remove -text "Remove" -command {remove_light}
	place $light.remove -x 200 -y [expr { ($i * $multiplier) + $offset + 20}]


	# Cancel Button
	button $light.cancel -text "Close" -command {destroy .light_menu}
	place $light.cancel -x 285 -y [expr { ($i * $multiplier) + $offset + 20}]

	# Label to display maximum x and y coordinate

	label $light.xmax -text "Maximum x = 250"
	label $light.ymax -text "Maximum y = 300"

	#place $light.xmax -x 20 -y [expr {$num_of_light * $multiplier + $offset  + 50 }]
	#place $light.ymax -x 20 -y [expr {$num_of_light * $multiplier + $offset + 70 }]

# end of procedure "light_menu"
}
#---------------------------------------------------------------------



proc send_to_light_db { } {

	global light_xpos light_ypos
	global light_out            	
	global xl1 xl2 xl3 xl4 xl5 xl6 xl7 xl8 xl9 xl10
	global yl1 yl2 yl3 yl4 yl5 yl6 yl7 yl8 yl9 yl10

	if { $light_xpos == "" } {
	       	set light_xpos 0
	}
	
	if { $light_ypos == "" } {
	       	set light_ypos 0
	}
	
	for {set i 1} {$i <= 10} {incr i} {
			
	
		set lbrd($i) [lindex $light_out($i) 1]
		set lelem($i) [lindex $light_out($i) 2]
		set lo($i) "$lbrd($i) $lelem($i)"
	
	}
	
	
	# coming from screen 1 with position x.....y.....
	set_light_db 1 $light_xpos $light_ypos $xl1 $xl2 $xl3 $xl4 $xl5 $xl6 $xl7 $xl8 $xl9 $xl10 $yl1 $yl2 $yl3 $yl4 $yl5 $yl6 $yl7 $yl8 $yl9 $yl10
	
	
  	#puts "light_menu.tcl: testing $xl1 $xl2 $xl3"
        set_light_conn_db $lo(1)|$lo(2)|$lo(3)|$lo(4)|$lo(5)|$lo(6)|$lo(7)|$lo(8)|$lo(9)|$lo(10)
	
}



#------------------------------------------------------
# This procedure will do the checking for empty entry fields and call
# another procedure called "light" from the file "light.tcl" that will create
# the actual graphic and lights
#------------------------------------------------------

proc draw_light { } {

     #-----------------------#
     # Declare the variables #
     #-----------------------#
     global v2h
     global num_of_light
     global light_name
     global light_xpos
     global light_ypos
     global xl1 xl2 xl3 xl4 xl5 xl6 xl7 xl8 xl9 xl10
     global yl1 yl2 yl3 yl4 yl5 yl6 yl7 yl8 yl9 yl10

     #-------------------------------------------------------------
     # This part is optional if the user wants to have more than 7 lights, but if this part
     # is left empty then we set it to 1000 so that it would not get displayed on the graphic
     #-------------------------------------------------------------

     if { [string compare $xl8 ""] == 0 } {
   	set xl8 1000
     }

     if { [string compare $xl9 ""] == 0 } {
   	set xl9 1000
     }

     if { [string compare $xl10 ""] == 0 } {
   	set xl10 1000
     }

     if { [string compare $yl8 ""] == 0 } {
   	set yl8 1000
     }

     if { [string compare $yl9 ""] == 0 } {
   	set yl9 1000
     }

     if { [string compare $yl10 ""] == 0 } {
   	set yl10 1000
     }

     # set the position and window path name

     set_light_pos $light_xpos $light_ypos
     get_window_path_lname $light_name

     # Call the procedure "light" that will draw the light graphic and this values provided below will
     # display the default lights
     light $xl1 $xl2 $xl3 $xl4 $xl5 $xl6 $xl7 $xl8 $xl9 $xl10 $yl1 $yl2 $yl3 $yl4 $yl5 $yl6 $yl7 $yl8 $yl9 $yl10

     # Don't forget to redraw the other window too, if it exists
      set v2lf .ventana2.editor.v2frame
      set v2h $v2lf.vehicle_light

     if [winfo exists $v2h] {
 	light2 $xl1 $xl2 $xl3 $xl4 $xl5 $xl6 $xl7 $xl8 $xl9 $xl10 $yl1 $yl2 $yl3 $yl4 $yl5 $yl6 $yl7 $yl8 $yl9 $yl10

     }

# end of procedure draw_light
}

proc remove_light { } {
   set vlf .ventana.editor.vframe

   if [winfo exists $vlf] {
   	set_graphic_info "light" "none" -1 -1 1
	destroy $vlf
   }




}
