#-------------------------------------------
# Initialize variables that are going to be used to display the
# x y coordinate for the "light" graph and its name
#-------------------------------------------


set lightx 0
set lighty 0
set lname ""

for {set i 1 } { $i <= 10 } { incr i} {
 	set lboard$i -1
 	set lenum$i -1
}    	

for {set i 1} { $i <= 10 } {incr i} {
 	set x($i) 100
 	set y($i) 100
 	set active_color($i) "blue"
}


#----------------------------------------------
# This procedure set the x y coordinate value to the global
# variable so that we can use the variables inside procedures
#----------------------------------------------

proc set_light_pos { x_light_pos y_light_pos } {
   global lightx
   global lighty

   set lightx $x_light_pos
   set lighty $y_light_pos
}

proc get_window_path_lname {name} {

   global lname
   set lname $name

}

#-----------------------------------------------------------
#
#
#-----------------------------------------------------------


proc light { xl1 xl2 xl3 xl4 xl5 xl6 xl7 xl8 xl9 xl10 yl1 yl2 yl3 yl4 yl5 yl6 yl7 yl8 yl9 yl10 } {

   # Declare global variables
   global lname
   global lightx
   global lighty
   global vh vlf
   global csize
   global tsize
   # this is textvariable of output coming from "light_menu.tcl"
   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 x
   global y

   set vlf .ventana.editor.vframe
   set vh $vlf.vehicle_light


   set x(1) $xl1
   set x(2) $xl2
   set x(3) $xl3
   set x(4) $xl4
   set x(5) $xl5
   set x(6) $xl6
   set x(7) $xl7
   set x(8) $xl8
   set x(9) $xl9
   set x(10) $xl10
   set y(1) $yl1
   set y(2) $yl2
   set y(3) $yl3
   set y(4) $yl4
   set y(5) $yl5
   set y(6) $yl6
   set y(7) $yl7
   set y(8) $yl8
   set y(9) $yl9
   set y(10) $yl10



   if {$lightx == ""} {
    	set lightx 0
   }

   if {$lighty == ""} {
   	set lighty 0
   }


   if [winfo exists $vlf] {
	destroy $vlf
   }

   set xmax 250
   set ymax 300

   #----------------------------#
   # Create the canvas here     #
   #----------------------------#
   frame $vlf -bg black -width 190 -height 230
   canvas $vh -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

    $vh create rectangle  \
    	[expr {$size * 2  + 14}]  [expr {$size + 15}] [expr {$size * 3 + 35}]  [expr {$size * 2 + 15}] \
     	-fill #f4d052


    $vh 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 	
    $vh create rectangle  \
    	[expr {$size + 15}] [expr {$size * 3 + 10}] [expr {$size * 4 + 35}] [expr {$size * 5 + 35}] \
     	-fill grey
     	
    # inner black rectangle frame
    $vh create rectangle  \
    	[expr {$size + 18}] [expr {$size * 3 + 13}] [expr {$size * 4 + 32}] [expr {$size * 5 + 32}] \
     	-fill black

     	
     # 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 {$x(1) >= 0 && $y(1) >= 0} { 		
    	$vh create oval $x(1) $y(1) [expr { $x(1)  + $csize} ] [expr {$y(1)  + $csize} ] -fill blue  -tag light_id1
    	$vh create text [expr { $x(1)  + $tsize} ] [expr {$y(1)  + $tsize} ] -text "1" -fill white -tag ltext_id1
    }
    if {$x(2) >= 0 && $y(2) >= 0} { 		
        $vh create oval $x(2) $y(2) [expr { $x(2)  + $csize} ] [expr {$y(2)  + $csize} ] -fill blue  -tag light_id2
    	$vh create text [expr { $x(2)  + $tsize} ] [expr {$y(2)  + $tsize} ] -text "2" -fill white -tag ltext_id2
     }
     if {$x(3) >= 0 && $y(3) >= 0} { 		
    	$vh create oval $x(3) $y(3) [expr { $x(3)  + $csize} ] [expr {$y(3)  + $csize} ] -fill blue  -tag light_id3
        $vh create text [expr { $x(3)  + $tsize} ] [expr {$y(3)  + $tsize} ] -text "3" -fill white -tag ltext_id3
     }
     if {$x(4) >= 0 && $y(4) >= 0} {
        $vh create oval $x(4) $y(4) [expr { $x(4)  + $csize} ] [expr {$y(4)  + $csize} ] -fill blue  -tag light_id4
        $vh create text [expr { $x(4)  + $tsize} ] [expr {$y(4)  + $tsize} ] -text "4" -fill white -tag ltext_id4
     }
     if {$x(5) >= 0 && $y(5) >= 0} {
    	$vh create oval $x(5) $y(5) [expr { $x(5)  + $csize} ] [expr {$y(5)  + $csize} ] -fill blue  -tag light_id5
             $vh create text [expr { $x(5)  + $tsize} ] [expr {$y(5)  + $tsize} ] -text "5" -fill white -tag ltext_id5
     }
     if {$x(6) >= 0 && $y(6) >= 0} { 		
    	$vh create oval $x(6) $y(6) [expr { $x(6)  + $csize} ] [expr {$y(6)  + $csize} ] -fill blue  -tag light_id6
     	$vh create text [expr { $x(6)  + $tsize} ] [expr {$y(6)  + $tsize} ] -text "6" -fill white -tag ltext_id6
     }
     if {$x(7) >= 0 && $y(7) >= 0} {
    	$vh create oval $x(7) $y(7) [expr { $x(7)  + $csize} ] [expr {$y(7)  + $csize} ] -fill blue  -tag light_id7
    	$vh create text [expr { $x(7)  + $tsize} ] [expr {$y(7)  + $tsize} ] -text "7" -fill white  -tag ltext_id7
     }
     if {$x(8) >= 0 && $y(8) >= 0} { 		
    	$vh create oval $x(8) $y(8) [expr { $x(8)  + $csize} ] [expr {$y(8)  + $csize} ] -fill blue  -tag light_id8
    	$vh create text [expr { $x(8)  + $tsize} ] [expr {$y(8)  + $tsize} ] -text "8" -fill white -tag ltext_id8
    }
    if {$x(9) >= 0 && $y(9) >= 0} { 		
    	$vh create oval $x(9) $y(9) [expr { $x(9)  + $csize} ] [expr {$y(9)  + $csize} ] -fill blue  -tag light_id9
    	$vh create text [expr { $x(9)  + $tsize} ] [expr {$y(9)  + $tsize} ] -text "9" -fill white -tag ltext_id9
    }
    if {$x(10) >= 0 && $y(10) >= 0} { 		
    	$vh create oval $x(10) $y(10) [expr { $x(10)  + $csize} ] [expr {$y(10)  + $csize} ] -fill blue -tag light_id10
       	$vh create text [expr { $x(10)  + $tsize} ] [expr {$y(10)  + $tsize} ] -text "10" -fill white -tag ltext_id10
    }




    # 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


    #--------------------------------------------------------------------------
    #  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
    #--------------------------------------------------------------------------

   $vh bind light_id1 <1>  {
    	global active_color(1)
    	global lboard1 lenum1 li1
    	if { $active_color(1) == "blue" } {
    		# if light is not isolated, then Turn ON
    		if {$li1 != 1} {          		
    			set active_color(1) "white"
    			change_color 1 $active_color(1)
    			send_switches_down $lboard1 $lenum1 1
    		}
    		  		
    	} else {
    		# Turn OFF
    		set active_color(1) "blue"
    	 	change_color 1 $active_color(1)         	 	
    	 	send_switches_down $lboard1 $lenum1 0   		
    	 	
    	}
    }


    $vh bind light_id2 <1>  {
        global active_color(2)
    	global lboard2 lenum2 li2
    	
    	if { $active_color(2) == "blue" } {
    	             # if light is not isolated, then Turn ON
    		if {$li2 != 1} {
    			set active_color(2) "white"
    			change_color 2 $active_color(2)
    			send_switches_down $lboard2 $lenum2 1
    		 }
    				
    	} else {
    		set active_color(2) "blue"
    	 	change_color 2 $active_color(2)
    	 	send_switches_down $lboard2 $lenum2 0
    	}
    }

      $vh bind light_id3 <1>  {
    	global active_color(3)
    	global lboard3 lenum3  li3
    	
    	if { $active_color(3) == "blue" } {
    		# if light is not isolated, then Turn ON
    		if {$li3 != 1} {
    	             	set active_color(3) "white"
    			change_color 3 $active_color(3)
    			send_switches_down $lboard3 $lenum3 1
    		 }
    		 	
    	} else {
    		set active_color(3) "blue"
    	 	change_color 3 $active_color(3)
    	 	send_switches_down $lboard3 $lenum3 0
    	}	
    }

    $vh bind light_id4 <1>  {
    	global active_color(4)
    	global lboard4 lenum4 li4
    	
    	if { $active_color(4) == "blue" } {
    		# if light is not isolated, then Turn ON
    		if {$li4 != 1} {
    			set active_color(4) "white"
    			change_color 4 $active_color(4)
    			send_switches_down $lboard4 $lenum4 1  	
    		}
    			
    	} else {
    		set active_color(4) "blue"
    	 	change_color 4 $active_color(4)
    	 	send_switches_down $lboard4 $lenum4 0
    	}

    }

    $vh bind light_id5 <1>  {
    	global active_color(5)
    	global lboard5 lenum5  li5
    	
    	if { $active_color(5) == "blue" } {
    		# if light is not isolated, then Turn ON
    		if {$li5 != 1 } {
    			set active_color(5) "white"
    			change_color 5 $active_color(5)   		
    			send_switches_down $lboard5 $lenum5 1
    		}
    			
    	} else {
    		set active_color(5) "blue"
    	 	change_color 5 $active_color(5)
    	 	send_switches_down $lboard5 $lenum5 0
    	}	

    }

    $vh bind light_id6 <1>  {
    	global active_color(6)
    	global lboard6 lenum6 li6
    	
    	if { $active_color(6) == "blue" } {
    		# if light is not isolated, then Turn ON
    		if {$li6 != 1 } {
    			set active_color(6) "white"
    			change_color 6 $active_color(6)   		
    	        		send_switches_down $lboard6 $lenum6 1
    	  	}
    	  		
    	} else {
    		set active_color(6) "blue"
    	 	change_color 6 $active_color(6)
    	 	send_switches_down $lboard6 $lenum6 0
    	}	
    }

    $vh bind light_id7 <1>  {
    	global active_color(7)
    	global lboard7 lenum7 li7
    	
    	if { $active_color(7) == "blue" } {
    		# if light is not isolated, then Turn ON
    		if {$li7 != 1 } {
    			set active_color(7) "white"
    			change_color 7 $active_color(7)
    			send_switches_down $lboard7 $lenum7 1     		
    		}
    		
    	} else {
    		set active_color(7) "blue"
    	 	change_color 7 $active_color(7)
    	 	send_switches_down $lboard7 $lenum7 0
    	}	
    }

    $vh bind light_id8 <1>  {
    	global active_color(8)
        	global lboard8 lenum8 li8	
    	
    	if { $active_color(8) == "blue" } {
    		# if light is not isolated, then Turn ON
    		if {$li8 != 1 } {
    			set active_color(8) "white"
    			change_color 8 $active_color(8)   		
    			send_switches_down $lboard8 $lenum8 1
    		}
    		
    	} else {
    		set active_color(8) "blue"
    	 	change_color 8 $active_color(8)
    	 	send_switches_down $lboard8 $lenum8 0
    	}	

    }

    $vh bind light_id9 <1>  {

    	global active_color(9)
    	global lboard9 lenum9 	
    	
    	if { $active_color(9) == "blue" } {
    			set active_color(9) "white"
    			change_color 9 $active_color(9)   		
    			send_switches_down $lboard9 $lenum9 1
    		
    	} else {
    		set active_color(9) "blue"
    	 	change_color 9 $active_color(9)
    		send_switches_down $lboard9 $lenum9 0
    	}	
    }

    $vh bind light_id10 <1>  {
    	global active_color(10)
    	global lboard10 lenum10
    	
    	if { $active_color(10) == "blue" } {
    			set active_color(10) "white"
    			change_color 10 $active_color(10)
    			send_switches_down $lboard10 $lenum10 1
    				
    	} else {
    		set active_color(10) "blue"
    	 	change_color 10 $active_color(10)
    	 	send_switches_down $lboard10 $lenum10 0
    	}	
    }


    #-----------------------------------------------------------------------------



   $vh bind ltext_id1 <1>  {
    	global active_color(1)
    	global lboard1 lenum1 li1

    	if { $active_color(1) == "blue" } {
    		
    		# if light is not isolated, then Turn ON
    		if {$li1 != 1 } {
    			set active_color(1) "white"
    			change_color 1 $active_color(1)
    			send_switches_down $lboard1 $lenum1 1
    	  	}
    		  		
    	} else {
    		# Turn OFF
    		set active_color(1) "blue"
    	 	change_color 1 $active_color(1)         	 	
    	 	send_switches_down $lboard1 $lenum1 0   		
    	 	
    	}
    }


    $vh bind ltext_id2 <1>  {
        global active_color(2)
    	global lboard2 lenum2 li2
    	
    	if { $active_color(2) == "blue" } {
    		# if light is not isolated, then Turn ON
    		if {$li2 != 1 } {
    			set active_color(2) "white"
    			change_color 2 $active_color(2)
    			send_switches_down $lboard2 $lenum2 1
    		}
    				
    	} else {
    		set active_color(2) "blue"
    	 	change_color 2 $active_color(2)
    	 	send_switches_down $lboard2 $lenum2 0
    	}
    }

      $vh bind ltext_id3 <1>  {
    	global active_color(3)
    	global lboard3 lenum3 li3
    	
    	if { $active_color(3) == "blue" } {
    		# if light is not isolated, then Turn ON
    		if {$li3 != 1 } {
    			set active_color(3) "white"
    			change_color 3 $active_color(3)
    			send_switches_down $lboard3 $lenum3 1
    		}   		
    	} else {
    		set active_color(3) "blue"
    	 	change_color 3 $active_color(3)
    	 	send_switches_down $lboard3 $lenum3 0
    	}	
    }

    $vh bind ltext_id4 <1>  {
    	global active_color(4)
    	global lboard4 lenum4 li4
    	
    	if { $active_color(4) == "blue" } {
    		# if light is not isolated, then Turn ON
    		if {$li4 != 1 } {
    			set active_color(4) "white"
    			change_color 4 $active_color(4)
    			send_switches_down $lboard4 $lenum4 1  	
    		}	
    	} else {
    		set active_color(4) "blue"
    	 	change_color 4 $active_color(4)
    	 	send_switches_down $lboard4 $lenum4 0
    	}

    }

    $vh bind ltext_id5 <1>  {
    	global active_color(5)
    	global lboard5 lenum5  li5
    	
    	if { $active_color(5) == "blue" } {
    		# if light is not isolated, then Turn ON
    		if {$li5 != 1 } {
    			set active_color(5) "white"
    			change_color 5 $active_color(5)   		
    			send_switches_down $lboard5 $lenum5 1  	
    		}
    	} else {
    		set active_color(5) "blue"
    	 	change_color 5 $active_color(5)
    	 	send_switches_down $lboard5 $lenum5 0
    	}	

    }

    $vh bind ltext_id6 <1>  {
    	global active_color(6)
    	global lboard6 lenum6 li6
    	
    	if { $active_color(6) == "blue" } {
    		# if light is not isolated, then Turn ON
    		if {$li6 != 1 } {
    			set active_color(6) "white"
    			change_color 6 $active_color(6)   		
    	        		send_switches_down $lboard6 $lenum6 1
    	  	}	
    	} else {
    		set active_color(6) "blue"
    	 	change_color 6 $active_color(6)
    	 	send_switches_down $lboard6 $lenum6 0
    	}	
    }

    $vh bind ltext_id7 <1>  {
    	global active_color(7)
    	global lboard7 lenum7 li7
    	
    	if { $active_color(7) == "blue" } {
    		# if light is not isolated, then Turn ON
    		if {$li7 != 1 } {
    			set active_color(7) "white"
    			change_color 7 $active_color(7)
    			send_switches_down $lboard7 $lenum7 1     	
    		}	
    	} else {
    		set active_color(7) "blue"
    	 	change_color 7 $active_color(7)
    	 	send_switches_down $lboard7 $lenum7 0
    	}	
    }

    $vh bind ltext_id8 <1>  {
    	global active_color(8)
        global lboard8 lenum8 li8	
    	
    	if { $active_color(8) == "blue" } {
    		# if light is not isolated, then Turn ON
    		if {$li8 != 1 } {
    			set active_color(8) "white"
    			change_color 8 $active_color(8)   		
    			send_switches_down $lboard8 $lenum8 1
    		}	
    	} else {
    		set active_color(8) "blue"
    	 	change_color 8 $active_color(8)
    	 	send_switches_down $lboard8 $lenum8 0
    	}	

    }

    $vh bind ltext_id9 <1>  {

    	global active_color(9)
    	global lboard9 lenum9 	
    	
    	if { $active_color(9) == "blue" } {
    		
    			set active_color(9) "white"
    			change_color 9 $active_color(9)   		
    			send_switches_down $lboard9 $lenum9 1
    		
    	} else {
    		set active_color(9) "blue"
    	 	change_color 9 $active_color(9)
    		send_switches_down $lboard9 $lenum9 0
    	}	
    }

    $vh bind ltext_id10 <1>  {
    	global active_color(10)
    	global lboard10 lenum10
    	
    	if { $active_color(10) == "blue" } {
    			set active_color(10) "white"
    			change_color 10 $active_color(10)
    			send_switches_down $lboard10 $lenum10 1
    	} else {
    		set active_color(10) "blue"
    	 	change_color 10 $active_color(10)
    	 	send_switches_down $lboard10 $lenum10 0
    	}	
    }


    #--------------------------------------------------------------------------


   place $vlf -x $lightx -y $lighty
   place $vh -x -1 -y -1

}

   proc change_color { id  new_color } {
        global vh
        global x
        global y
        global csize
        global tsize
        set v2lf .ventana2.editor.v2frame
      	set v2h $v2lf.vehicle_light
      	
      	$vh delete light_id$id
        $vh delete ltext_id$id

        if { $new_color == "blue" } {
        	set text_color "white"
        	puts "0"
        } else {
        	set text_color "black"
        	puts "1"
        }

        #puts "after here $x($id) $y($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




        if [winfo exists $v2h] {	
        	
        	$v2h delete light_id$id
        	$v2h delete ltext_id$id
        	
        	$v2h create oval $x($id) $y($id) [expr { $x($id)  + $csize} ] [expr {$y($id)  + $csize} ] -fill $new_color  -tag light_id$id
          	$v2h create text [expr { $x($id)  + $tsize} ] [expr {$y($id)  + $tsize} ] -text "$id" -fill $text_color -tag ltext_id$id
        }

   # end of proceedure "change_color"

# end of procedure "light"
}
