#-------------------------------------------
# Initialize variables that are going to be used to display the
# x y coordinate for the "light" graph and its name
#-------------------------------------------

set lightx2 0
set lighty2 0
set l2name ""



for {set i 1 } { $i <= 10 } { incr i} {
 	set lboard$i -1
 	set lenum$i -1
}    	


#----------------------------------------------
# This procedure set the x y coordinate value to the global
# variable so that we can use the variables inside procedures
#----------------------------------------------

proc set_light2_pos { x_light2_pos y_light2_pos } {
   global lightx2
   global lighty2

   set lightx2 $x_light2_pos
   set lighty2 $y_light2_pos
}

proc get_window_path_l2name {name} {
   global l2name

   set l2name $name

}


#-----------------------------------------------------------
#
#
#-----------------------------------------------------------


for {set i 1} { $i <= 10 } {incr i} {
 	set x2($i) 0
 	set y2($i) 0
 	set active_color($i) "blue"
}

#set v2lf .ventana2.editor.v2frame
#set v2h $v2lf.vehicle_light$l2name


proc light2 { xl1 xl2 xl3 xl4 xl5 xl6 xl7 xl8 xl9 xl10 yl1 yl2 yl3 yl4 yl5 yl6 yl7 yl8 yl9 yl10 } {

   # Declare global variables
   global l2name
   global lightx2
   global lighty2
   global v2h v2lf
   global csize
   global tsize
   global light_out
   global light_id1 light_id2 light_id3 light_id4 light_id5  light_id6 light_id7 light_id8 light_id9 light_id10

   global x2
   global y2

   set v2lf .ventana2.editor.v2frame
   set v2h $v2lf.vehicle_light


   set x2(1) $xl1
   set x2(2) $xl2
   set x2(3) $xl3
   set x2(4) $xl4
   set x2(5) $xl5
   set x2(6) $xl6
   set x2(7) $xl7
   set x2(8) $xl8
   set x2(9) $xl9
   set x2(10) $xl10
   set y2(1) $yl1
   set y2(2) $yl2
   set y2(3) $yl3
   set y2(4) $yl4
   set y2(5) $yl5
   set y2(6) $yl6
   set y2(7) $yl7
   set y2(8) $yl8
   set y2(9) $yl9
   set y2(10) $yl10



   if {$lightx2 == ""} {
    	set lightx2 0
   }

   if {$lighty2 == ""} {
   	set lighty2 0
   }

   if [winfo exists $v2lf] {
	destroy $v2lf
   }

   set xmax 250
   set ymax 300

   #----------------------------#
   # Create the canvas here     #
   #----------------------------#
   frame $v2lf -bg black -width 190 -height 230
   canvas $v2h -bg black -width 195 -height 235

   # Change this number if you want to change the size of yellow drawing
   set size 30

   # Change this number to change the size of circle (lights)
   set csize 35

   $v2h create rectangle  \
    	[expr {$size * 2  + 14}]  [expr {$size + 15}] [expr {$size * 3 + 35}]  [expr {$size * 2 + 15}] \
     	-fill #f4d052

   $v2h create poly \
	[expr {$size + 15 }] [expr {$size + 15 }] [expr {$size + 15}] [expr {$size * 4 + 35}] \
	[expr {$size * 2 + 15}] [expr {$size * 5 + 35}] [expr {$size * 3 + 35}] [expr {$size * 5 + 35}] \
	[expr {$size * 4 + 35}] [expr {$size * 4 + 35}] [expr {$size * 4 + 35}]  [expr {$size + 15 }]  \
	[expr {$size * 3 + 35}]  [expr {$size + 15 }]  [expr {$size * 3 + 35}]  [expr {$size * 2 + 15}] \
	[expr {$size * 2 + 15 }]  [expr {$size * 2 + 15 }]  [expr {$size * 2  + 15}]  [expr {$size + 15}] \
	[expr {$size + 15 }] [expr {$size + 15 }] -fill orange

	
    # outer grey frame 	
    $v2h create rectangle  \
    	[expr {$size + 15}] [expr {$size * 3 + 10}] [expr {$size * 4 + 35}] [expr {$size * 5 + 35}] \
     	-fill grey
     	
    # inner black rectangle frame
    $v2h create rectangle  \
    	[expr {$size + 18}] [expr {$size * 3 + 13}] [expr {$size * 4 + 32}] [expr {$size * 5 + 32}] \
     	-fill black	
	
	


    # Need to do this so that we can get the value inside binding procedure
    global lboard1 lboard2 lboard3 lboard4 lboard5 lboard6 lboard7 lboard8 lboard9 lboard10
    global lenum1 lenum2 lenum3 lenum4 lenum5 lenum6 lenum7 lenum8 lenum9 lenum10

     # This number will depend on the size of circle --->csize
    set tsize  [expr { $csize  / 2 + 1} ]

    # Draw each of the circle lights (by default, the light is Off -> blue color) and
    # draw the text numbre (from 1 - 10) as well

    if {$x2(1) >= 0 && $y2(1) >= 0} { 		
    	$v2h create oval $x2(1) $y2(1) [expr { $x2(1)  + $csize} ] [expr {$y2(1)  + $csize} ] -fill blue  -tag light_id1
    	$v2h create text [expr { $x2(1)  + $tsize} ] [expr {$y2(1)  + $tsize} ] -text "1" -fill white -tag ltext_id1
    }
    if {$x2(2) >= 0 && $y2(2) >= 0} { 		
        	$v2h create oval $x2(2) $y2(2) [expr { $x2(2)  + $csize} ] [expr {$y2(2)  + $csize} ] -fill blue  -tag light_id2
    	$v2h create text [expr { $x2(2)  + $tsize} ] [expr {$y2(2)  + $tsize} ] -text "2" -fill white -tag ltext_id2
     }
     if {$x2(3) >= 0 && $y2(3) >= 0} { 		
    	$v2h create oval $x2(3) $y2(3) [expr { $x2(3)  + $csize} ] [expr {$y2(3)  + $csize} ] -fill blue  -tag light_id3
             $v2h create text [expr { $x2(3)  + $tsize} ] [expr {$y2(3)  + $tsize} ] -text "3" -fill white -tag ltext_id3
     }
     if {$x2(4) >= 0 && $y2(4) >= 0} {
         	$v2h create oval $x2(4) $y2(4) [expr { $x2(4)  + $csize} ] [expr {$y2(4)  + $csize} ] -fill blue  -tag light_id4
             $v2h create text [expr { $x2(4)  + $tsize} ] [expr {$y2(4)  + $tsize} ] -text "4" -fill white -tag ltext_id4
     }
     if {$x2(5) >= 0 && $y2(5) >= 0} {
    	$v2h create oval $x2(5) $y2(5) [expr { $x2(5)  + $csize} ] [expr {$y2(5)  + $csize} ] -fill blue  -tag light_id5
             $v2h create text [expr { $x2(5)  + $tsize} ] [expr {$y2(5)  + $tsize} ] -text "5" -fill white -tag ltext_id5
     }
     if {$x2(6) >= 0 && $y2(6) >= 0} { 		
    	$v2h create oval $x2(6) $y2(6) [expr { $x2(6)  + $csize} ] [expr {$y2(6)  + $csize} ] -fill blue  -tag light_id6
     	$v2h create text [expr { $x2(6)  + $tsize} ] [expr {$y2(6)  + $tsize} ] -text "6" -fill white -tag ltext_id6
     }
     if {$x2(7) >= 0 && $y2(7) >= 0} {
    	$v2h create oval $x2(7) $y2(7) [expr { $x2(7)  + $csize} ] [expr {$y2(7)  + $csize} ] -fill blue  -tag light_id7
    	$v2h create text [expr { $x2(7)  + $tsize} ] [expr {$y2(7)  + $tsize} ] -text "7" -fill white  -tag ltext_id7
     }
     if {$x2(8) >= 0 && $y2(8) >= 0} { 		
    	$v2h create oval $x2(8) $y2(8) [expr { $x2(8)  + $csize} ] [expr {$y2(8)  + $csize} ] -fill blue  -tag light_id8
    	$v2h create text [expr { $x2(8)  + $tsize} ] [expr {$y2(8)  + $tsize} ] -text "8" -fill white -tag ltext_id8
    }
    if {$x2(9) >= 0 && $y2(9) >= 0} { 		
    	$v2h create oval $x2(9) $y2(9) [expr { $x2(9)  + $csize} ] [expr {$y2(9)  + $csize} ] -fill blue  -tag light_id9
    	$v2h create text [expr { $x2(9)  + $tsize} ] [expr {$y2(9)  + $tsize} ] -text "9" -fill white -tag ltext_id9
    }
    if {$x2(10) >= 0 && $y2(10) >= 0} { 		
    	$v2h create oval $x2(10) $y2(10) [expr { $x2(10)  + $csize} ] [expr {$y2(10)  + $csize} ] -fill blue -tag light_id10
       	$v2h create text [expr { $x2(10)  + $tsize} ] [expr {$y2(10)  + $tsize} ] -text "10" -fill white -tag ltext_id10
    }




    #--------------------------------------------------------------------------
    #  GET THE BOARD AND ELEMENT NUMBER
    #--------------------------------------------------------------------------


    for {set i 1 } { $i <= 10 } { incr i} {
 	set lboard$i -1
 	set lenum$i -1
    }    	



    if { [lindex $light_out(1) 1] == "MPL" } { set lboard1 1 }
    if { [lindex $light_out(1) 1] == "Gesout" } { set lboard1 0 }

    if { [lindex $light_out(2) 1] == "MPL" } { set lboard2 1 }
    if { [lindex $light_out(2) 1] == "Gesout" } { set lboard2 0 }

    if { [lindex $light_out(3) 1] == "MPL" } { set lboard3 1 }
    if { [lindex $light_out(3) 1] == "Gesout" } { set lboard3 0 }

    if { [lindex $light_out(4) 1] == "MPL" } { set lboard4 1 }
    if { [lindex $light_out(4) 1] == "Gesout" } { set lboard4 0 }
    	
    if { [lindex $light_out(5) 1] == "MPL" } { set lboard5 1 }
    if { [lindex $light_out(5) 1] == "Gesout" } { set lboard5 0 }
    	
    if { [lindex $light_out(6) 1] == "MPL" } { set lboard6 1 }
    if { [lindex $light_out(6) 1] == "Gesout" } { set lboard6 0}

    if { [lindex $light_out(7) 1] == "MPL" } { set lboard7 1 }      	
    if { [lindex $light_out(6) 1] == "Gesout" } { set lboard7 0 }

    if { [lindex $light_out(8) 1] == "MPL" } { set lboard8 1 }
    if { [lindex $light_out(8) 1] == "Gesout" } { set lboard8 0 }


    if { [lindex $light_out(9) 1] == "MPL" } { set lboard9 1 }
    if { [lindex $light_out(9) 1] == "Gesout" } { set lboard9 0 }


    if { [lindex $light_out(10) 1] == "MPL" } { set lboard10 1 }
    if { [lindex $light_out(10) 1] == "Gesout" } { set lboard10 0 }




    # Get the element number
    set lenum1 [lindex $light_out(1) 2]
    set lenum2 [lindex $light_out(2) 2]
    set lenum3 [lindex $light_out(3) 2]
    set lenum4 [lindex $light_out(4) 2]
    set lenum5 [lindex $light_out(5) 2]
    set lenum6 [lindex $light_out(6) 2]
    set lenum7 [lindex $light_out(7) 2]
    set lenum8 [lindex $light_out(8) 2]
    set lenum9 [lindex $light_out(9) 2]
    set lenum10 [lindex $light_out(10) 2]






    #--------------------------------------------------------------------------
    #  BINDING
    #--------------------------------------------------------------------------


   $v2h bind light_id1 <1>  {
    	global active_color(1)
    	global lboard1 lenum1 li1

    	if { $active_color(1) == "blue" } {
    		# if light is not isolated, Turn ON
    	              if {$li1 != 1} {
    			set active_color(1) "white"
    			change_color2 1 $active_color(1)
    			send_switches_down $lboard1 $lenum1 1
    		}
    		  		
    	} else {
    		# Turn OFF
    		set active_color(1) "blue"
    	 	change_color2 1 $active_color(1)         	 	
    	 	send_switches_down $lboard1 $lenum1 0   		
    	 	
    	}
    }


    $v2h bind light_id2 <1>  {
        	global active_color(2)
    	global lboard2 lenum2 li2
    	
    	if { $active_color(2) == "blue" } {
    	              # if light is not isolated, Turn ON
    	              if {$li2 != 1} {    		
    			set active_color(2) "white"
    			change_color2 2 $active_color(2)
    			send_switches_down $lboard2 $lenum2 1
    		 }
    				
    	} else {
    		set active_color(2) "blue"
    	 	change_color2 2 $active_color(2)
    	 	send_switches_down $lboard2 $lenum2 0
    	}
    }

      $v2h bind light_id3 <1>  {
    	global active_color(3)
    	global lboard3 lenum3 li3
    	
    	if { $active_color(3) == "blue" } {
    	              # if light is not isolated, Turn ON
    	              if {$li3 != 1} {    		
    			set active_color(3) "white"
    			change_color2 3 $active_color(3)
    			send_switches_down $lboard3 $lenum3 1
    		 }	
    	} else {
    		set active_color(3) "blue"
    	 	change_color2 3 $active_color(3)
    	 	send_switches_down $lboard3 $lenum3 0
    	}	
    }

    $v2h bind light_id4 <1>  {
    	global active_color(4)
    	global lboard4 lenum4 li4
    	
    	if { $active_color(4) == "blue" } {
    	              # if light is not isolated, Turn ON
    	              if {$li4 != 1} {    		
    			set active_color(4) "white"
    			change_color2 4 $active_color(4)
    			send_switches_down $lboard4 $lenum4 1  	
    		}	
    	} else {
    		set active_color(4) "blue"
    	 	change_color2 4 $active_color(4)
    	 	send_switches_down $lboard4 $lenum4 0
    	}

    }

    $v2h bind light_id5 <1>  {
    	global active_color(5)
    	global lboard5 lenum5 li5
    	
    	if { $active_color(5) == "blue" } {
    		# if light is not isolated, Turn ON
    	              if {$li5 != 1} {    		
    			set active_color(5) "white"
    			change_color2 5 $active_color(5)   		
    			send_switches_down $lboard5 $lenum5 1
    		}	
    	} else {
    		set active_color(5) "blue"
    	 	change_color2 5 $active_color(5)
    	 	send_switches_down $lboard5 $lenum5 0
    	}	

    }

    $v2h bind light_id6 <1>  {
    	global active_color(6)
    	global lboard6 lenum6 li6
    	
    	if { $active_color(6) == "blue" } {
    	              # if light is not isolated, Turn ON
    	              if {$li6 != 1} {    		
    			set active_color(6) "white"
    			change_color2 6 $active_color(6)   		
    	        		send_switches_down $lboard6 $lenum6 1
    	  	}	
    	} else {
    		set active_color(6) "blue"
    	 	change_color2 6 $active_color(6)
    	 	send_switches_down $lboard6 $lenum6 0
    	}	
    }

    $v2h bind light_id7 <1>  {
    	global active_color(7)
    	global lboard7 lenum7 li7
    	
    	if { $active_color(7) == "blue" } {
    	              # if light is not isolated, Turn ON
    	              if {$li7 != 1} {    		
    			set active_color(7) "white"
    			change_color2 7 $active_color(7)
    			send_switches_down $lboard7 $lenum7 1
    		}		
    	} else {
    		set active_color(7) "blue"
    	 	change_color2 7 $active_color(7)
    	 	send_switches_down $lboard7 $lenum7 0
    	}	
    }

    $v2h bind light_id8 <1>  {
    	global active_color(8)
        global lboard8 lenum8 li8 	
    	
    	if { $active_color(8) == "blue" } {
    	               # if light is not isolated, Turn ON
    	              if {$li8 != 1} {    		
    			set active_color(8) "white"
    			change_color2 8 $active_color(8)   		
    			send_switches_down $lboard8 $lenum8 1
    		}
    	} else {
    		set active_color(8) "blue"
    	 	change_color2 8 $active_color(8)
    	 	send_switches_down $lboard8 $lenum8 0
    	}	

    }

    $v2h bind light_id9 <1>  {

    	global active_color(9)
    	global lboard9 lenum9 	
    	
    	if { $active_color(9) == "blue" } {
    	
    			set active_color(9) "white"
    			change_color2 9 $active_color(9)   		
    			send_switches_down $lboard9 $lenum9 1
    			
    	} else {
    		set active_color(9) "blue"
    	 	change_color2 9 $active_color(9)
    		send_switches_down $lboard9 $lenum9 0
    	}	
    }

    $v2h bind light_id10 <1>  {
    	global active_color(10)
    	global lboard10 lenum10
    	
    	if { $active_color(10) == "blue" } {
    	
    			set active_color(10) "white"
    			change_color2 10 $active_color(10)
    			send_switches_down $lboard10 $lenum10 1
    					
    	} else {
    		set active_color(10) "blue"
    	 	change_color2 10 $active_color(10)
    	 	send_switches_down $lboard10 $lenum10 0
    	}	
    }


    #-----------------------------------------------------------------------------



   $v2h bind ltext_id1 <1>  {
    	global active_color(1)
    	global lboard1 lenum1 li1

    	if { $active_color(1) == "blue" } {
    		# if light is not isolated, Turn ON
    	              if {$li1 != 1} {
    			set active_color(1) "white"
    			change_color2 1 $active_color(1)
    			send_switches_down $lboard1 $lenum1 1
    		 }	
    	} else {
    		# Turn OFF
    		set active_color(1) "blue"
    	 	change_color2 1 $active_color(1)         	 	
    	 	send_switches_down $lboard1 $lenum1 0   		
    	 	
    	}
    }


    $v2h bind ltext_id2 <1>  {
        global active_color(2)
    	global lboard2 lenum2 li2
    	
    	if { $active_color(2) == "blue" } {
    		# if light is not isolated, Turn ON
    	              if {$li2 != 1} {
    			set active_color(2) "white"
    			change_color2 2 $active_color(2)
    			send_switches_down $lboard2 $lenum2 1
    		}	
    	} else {
    		set active_color(2) "blue"
    	 	change_color2 2 $active_color(2)
    	 	send_switches_down $lboard2 $lenum2 0
    	}
    }

      $v2h bind ltext_id3 <1>  {
    	global active_color(3)
    	global lboard3 lenum3 li3
    	
    	if { $active_color(3) == "blue" } {
    	              # if light is not isolated, Turn ON
    	              if {$li3 != 1} {    		
    			set active_color(3) "white"
    			change_color2 3 $active_color(3)
    			send_switches_down $lboard3 $lenum3 1
    		}  		
    	} else {
    		set active_color(3) "blue"
    	 	change_color2 3 $active_color(3)
    	 	send_switches_down $lboard3 $lenum3 0
    	}	
    }

    $v2h bind ltext_id4 <1>  {
    	global active_color(4)
    	global lboard4 lenum4 li4
    	
    	if { $active_color(4) == "blue" } {
    	              # if light is not isolated, Turn ON
    	              if {$li4 != 1} {    		
    			set active_color(4) "white"
    			change_color2 4 $active_color(4)
    			send_switches_down $lboard4 $lenum4 1
    		}		
    	} else {
    		set active_color(4) "blue"
    	 	change_color2 4 $active_color(4)
    	 	send_switches_down $lboard4 $lenum4 0
    	}

    }

    $v2h bind ltext_id5 <1>  {
    	global active_color(5)
    	global lboard5 lenum5 li5
    	
    	if { $active_color(5) == "blue" } {
    	              # if light is not isolated, Turn ON
    	              if {$li5 != 1} {    		
    			set active_color(5) "white"
    			change_color2 5 $active_color(5)   		
    			send_switches_down $lboard5 $lenum5 1  	
    		}	
    	} else {
    		set active_color(5) "blue"
    	 	change_color2 5 $active_color(5)
    	 	send_switches_down $lboard5 $lenum5 0
    	}	

    }

    $v2h bind ltext_id6 <1>  {
    	global active_color(6)
    	global lboard6 lenum6 li6
    	
    	if { $active_color(6) == "blue" } {
    	              # if light is not isolated, Turn ON
    	              if {$li6 != 1} {
    			set active_color(6) "white"
    			change_color2 6 $active_color(6)   		
    	        		send_switches_down $lboard6 $lenum6 1
    	  	} 	
    	} else {
    		set active_color(6) "blue"
    	 	change_color2 6 $active_color(6)
    	 	send_switches_down $lboard6 $lenum6 0
    	}	
    }

    $v2h bind ltext_id7 <1>  {
    	global active_color(7)
    	global lboard7 lenum7 li7
    	
    	if { $active_color(7) == "blue" } {
    	              # if light is not isolated, Turn ON
    	              if {$li7 != 1} {
    			set active_color(7) "white"
    			change_color2 7 $active_color(7)
    			send_switches_down $lboard7 $lenum7 1
    		}		
    	} else {
    		set active_color(7) "blue"
    	 	change_color2 7 $active_color(7)
    	 	send_switches_down $lboard7 $lenum7 0
    	}	
    }

    $v2h bind ltext_id8 <1>  {
    	global active_color(8)
        	global lboard8 lenum8 li8 	
    	
    	if { $active_color(8) == "blue" } {
    	              # if light is not isolated, Turn ON
    	              if {$li8 != 1} {
    			set active_color(8) "white"
    			change_color2 8 $active_color(8)   		
    			send_switches_down $lboard8 $lenum8 1    	
    		}
    	} else {
    		set active_color(8) "blue"
    	 	change_color2 8 $active_color(8)
    	 	send_switches_down $lboard8 $lenum8 0
    	}	

    }

    $v2h bind ltext_id9 <1>  {

    	global active_color(9)
    	global lboard9 lenum9 	
    	
    	if { $active_color(9) == "blue" } {
    	
    			set active_color(9) "white"
    			change_color2 9 $active_color(9)   		
    			send_switches_down $lboard9 $lenum9 1
    			
    	} else {
    		set active_color(9) "blue"
    	 	change_color2 9 $active_color(9)
    		send_switches_down $lboard9 $lenum9 0
    	}	
    }

    $v2h bind ltext_id10 <1>  {
    	global active_color(10)
    	global lboard10 lenum10
    	
    	if { $active_color(10) == "blue" } {
    	
    			set active_color(10) "white"
    			change_color2 10 $active_color(10)
    			send_switches_down $lboard10 $lenum10 1
    					
    	} else {
    		set active_color(10) "blue"
    	 	change_color2 10 $active_color(10)
    	 	send_switches_down $lboard10 $lenum10 0
    	}	
    }














    #--------------------------------------------------------------------------




   place $v2lf -x $lightx2 -y $lighty2
   place $v2h -x -1 -y -1

   proc change_color2 { id  new_color } {
   	
        global vh
        global v2h

        global x2
        global y2
        global csize
        global tsize
        set vlf .ventana.editor.vframe
      	set vh $vlf.vehicle_light

        #puts "before here $x($id) $y($id) "

	#set v2lf .ventana2.editor.v2frame
	#set v2h $v2lf.vehicle_light



        $v2h delete light_id$id
        $v2h delete ltext_id$id

        if { $new_color == "blue" } {
        	set text_color "white"
        } else {
        	set text_color "black"
        }

         #puts "after here $x($id) $y($id) "

        $v2h create oval $x2($id) $y2($id) [expr { $x2($id)  + $csize} ] [expr {$y2($id)  + $csize} ] -fill $new_color  -tag light_id$id
        $v2h create text [expr { $x2($id)  + $tsize} ] [expr {$y2($id)  + $tsize} ] -text "$id" -fill $text_color -tag ltext_id$id


        if [winfo exists $vh] {	
        	
        	$vh delete light_id$id
        	$vh delete ltext_id$id

        	$vh create oval $x($id) $y($id) [expr { $x($id)  + $csize} ] [expr {$y($id)  + $csize} ] -fill $new_color  -tag light_id$id
          	$vh create text [expr { $x($id)  + $tsize} ] [expr {$y($id)  + $tsize} ] -text "$id" -fill $text_color -tag ltext_id$id
        }

   #end of procedure "change_color2"

   }


# end of procedure "light"
}
