require 'drb'
require "tk"
require "tkfont"
require "tkcanvas"

class TkcRoomWidget<TkcItem
	CItemTypeToClass['roomwidget'] = self
	def create_self(*args)
		@room = nil
		tk_call(@path, 'create', 'rectangle', *args)	
	end
  
	def Room
		@room
	end
	def Room=(room)
		@room = room
	end 
  
  private :create_self
end

class MapBuilder

	def bindRect rect
	
			rect.bind('Button-3') do |event|
                @mouseExx = event.x
				@mouseEyy = event.y
                rect.bind('Motion') do |event|
						rect.move(event.x - @mouseExx, event.y - @mouseEyy)
						$rubyMud.GetRoom(rect.Room.Room.Num).X += event.x - @mouseExx
						$rubyMud.GetRoom(rect.Room.Room.Num).Y += event.y - @mouseEyy
						@mouseExx = event.x
						@mouseEyy = event.y
				end
            end

			rect.bind('ButtonRelease-1') do
				@finalRect = rect
				rect.bind('Motion') do
				end
			end

			rect.bind('Button-1') do |event|
				@currentRect = rect
				@finalRect = nil
            end
			
		
			rect.bind('ButtonRelease-3') do
                rect.bind('Motion') do
                    end
            end
	end

	def GetAreaFromRoom(num)
		rooms = {}
		queue = {}
		
		# Put the first item on the queue
		queue[num] = $rubyMud.GetRoom(num)		
		
		# Loop through the available rooms
		while true
			# Stop when the queue is empty
			if (queue.size == 0)
				break
			end
				
			# Get the top room
			topRoom = queue[queue.keys[0]]
			# Get the top room's exits
			exits = topRoom.Room.Exits
			
			# Loop through all the exits in the top room
			for exit in exits
				n = exit.Room.Num
				
				# Check if we already have that room
				if (rooms[n] != nil)
					next
				end
				
				# Add the room to the queue
				queue[n] = $rubyMud.GetRoom(n)
			end
			
			# Delete the top room from the queue
			queue.delete(topRoom.Room.Num)

			# Create the room
			mycheck = TkVariable.new
			rect = TkcRoomWidget.new(@canvas,topRoom.X,topRoom.Y,topRoom.X+topRoom.SizeX,topRoom.Y+topRoom.SizeY,'fill' => %w{ red green blue white }[rand(4)])			
			rect.Room = topRoom
			
			#print rect.id.to_s+"\n"
			bindRect(rect)

			
			# Add the room to the already seen room list
			rooms[topRoom.Room.Num] = topRoom			
		end
		
		# Loop through all the rooms to connect
		
		@rooms = rooms
		
	end

	def do_press(x, y)
		@start_x = x
		@start_y = y
		@current_line = TkcLine.new(@canvas, x, y, x, y) do
		arrow 'last'
		end
	end
	
	def do_motion(x, y)
		if @current_line
			#print @currentRect.Room.Room.Num
			@current_line.coords @start_x, @start_y, x, y
		end
	end
	
	def do_release(x, y)
		if @finalRect == nil
			@current_line = nil
			return
		end
		
		print "!"+@finalRect.Room.Room.Num.to_s
		
		if @current_line
			@current_line.coords @start_x, @start_y, x, y
			@current_line.fill 'black'
			@current_line = nil
		end
	end


	def initialize(parent)
		@rooms = {}
		
		@top = TkFrame.new(parent,'background'=>'black').pack #('anchor'=>'center')
		
		@canvas = TkCanvas.new(@top,'background'=>'light blue')
								#'xscrollcommand' => proc{|idx| @scroll_barH.set *idx},
								#'yscrollcommand' => proc{|idy| @scroll_barV.set *idy}								
								#)										
		
		# Vertical Scrollbar
		@scroll_barV = TkScrollbar.new(@top,'command' => proc { |*args| @canvas.yview *args })
		@scroll_barV.grid('column'=>0, 'row'=>0,'rowspan'=>2, 'sticky'=>'ns')
		
		# Horizontal Scrollbar
		@scroll_barH = TkScrollbar.new(@top,'command' => proc { |*args| @canvas.xview *args },'orient'=>'horizontal')						
		@scroll_barH.grid('column'=>1, 'row'=>0, 'sticky'=>'we')
		
		# Textbox
		@textBox = TkEntry.new(@top)
		@textBox.grid('column'=>0, 'row'=>2, 'sticky'=>'we','columnspan'=>2)
		
		# Canvas
		@start_x = @start_y = 0
		@canvas.grid('column'=>1, 'row'=>1,'sticky'=>'nesw')
		@canvas.bind("1", proc{|e| do_press(e.x, e.y)})		
		@canvas.bind("B1-Motion", proc{|x, y| do_motion(x, y)}, "%x %y")
		@canvas.bind("ButtonRelease-1",proc{|x, y| do_release(x,y)}, "%x %y")
		
		@currentRect = nil		
		
		GetAreaFromRoom(1)
		
		#TkcRectangle.new(@canvas, 0, 0, '3.5i', '3.5i', 'fill' => 'black')
		
	end
end

# Get the drb service
DRb.start_service()
$rubyMud = DRbObject.new(nil, 'druby://localhost:9000')

# Start the tk mapbuilder
root = TkRoot.new{ title 'Canvas'
				   background 'white'}
MapBuilder.new(root)
Tk.mainloop