
module ReviewProtocol

  attr_reader :data_fill

	def set_data(toks)
		case toks[0]
		when 'BOARDSIZE'
			@size= toks[2].to_i
			@root['SZ']= toks[2]
		when 'KOMI'
			@root['KM']= toks[2]
		when 'SGFNAME'
			@root['GN']= toks[2]
		when 'WHITENAME'
			@root['PW']= toks[2]
		when 'BLACKNAME'
			@root['PB']= toks[2]
		when 'WHITERANK'
			@root['WR']= toks[2]
		when 'BLACKRANK'
			@root['BR']= toks[2]
		when 'GAMERESULT'
			@root['RE']= toks[2]
		end
				
	end

	def create_game
		@nodes=[]
		@collection= SGF.new
		@pos_node = Node.new
		@pos_node['AP']= 'RubyGo:'+ $version
      @pos_node['GM']= '1'
      @pos_node['FF']= '4'
      @pos_node['MN']= '0'
    #  @last_node['DT']= Time.now.strftime '%Y-%m-%d'
	 @buf_comments=''
	 @collection.push(@pos_node)
	 set_collection(@collection)
  end

def one_node(toks)
  #debug toks
#Tk.update
#sleep 0.5

data_node(toks)

# on y va
begin
	if list=fwd
		#debug "en avant"
		list.each { |n| set_fast_node(n) }
		set_node(@pos_node)
	elsif bwd
		#debug "en arrière"
		while @current_node != @pos_node
			undo_node 
			@current_node = @current_node.prev
		end
		set_node(@current_node )
	else
		#debug "du début"
		set_begin
		if list=fwd
			list.each { |n| set_fast_node(n) }
			set_node(@pos_node)
		else	
			debug "node not found"
			p toks
		end
	end

rescue 
	exit -1
end
end

def fwd
list=[]
	n= @pos_node
	while n.prev and n!= @current_node
		n = n.prev
		list.unshift(n) if n != @current_node
	end
		

	if n == @current_node
		return list
	else
		return nil
	end
	  
end

def bwd
	n= @current_node
	#debug n.move_nb.to_s+':'+n.to_s+'->'+@pos_node.move_nb.to_s
	 until (n == @pos_node or n.nil?)
		n = n.prev
		#puts n.move_nb.to_s+':'+n.to_s
	end
	return n 
end


def set_position	# seulement une fois au début
	 @data_fill= true
		list=find_path(	@pos_node)
		list.each { | n| set_fast_node(n) }
		set_node(@pos_node)
		#debug "position initiale:"+@pos_node.move_nb.to_s+@pos_node.to_s
end


def find_path(node)
#renvoit la liste dans l'ordre depuis root sans le dernier
list= []
cur=node

while cur= cur.prev
	list.unshift(cur)
end
list
end

def data_node(toks)

	@last_id= toks[3].to_i
if   node=@nodes.at(@last_id)  
		@pos_node= node
else
  create_node(toks)
end
end

def delete_node(toks)
	nb=toks[3].to_i
	if node= @nodes.at(nb)
		node.prev.next=nil 
	else
		debug "node not found for delete"
	end
end
	def create_node(toks)
	  # must guarantees a node is created
	node=Node.new
	@nodes[@last_id]=node
	pl= toks[4].to_i

	if pl == 0 
	  # begin and end nodes

	else
	  
	  color= (pl == 1 ? 'B' : 'W')
#TODO: vérifier le sens !!!
		node[color]= get_coords(toks[5],toks[6])
#debug @last_id.to_s+node.to_s
#	if line= @buf_comments.delete_at(@last_id)
#		line =~ /REVIEW COMMENT \d+ \d+ (.*)/
#		node['C']=$1 if $1
#		debug node
#	end
	end

	push_node(node)


	end

def get_coords(col,row)
	  x= col.to_i
	  y= row.to_i
	if x == 20 and y == 20
			'tt'
		else
			 Coords.to_a(x - 1 , @size - y )
		end

end

def comment(line)
#id doesn't count
#vérifier comments pas accumulés
line =~ /REVIEW COMMENT \d+ \d+ (.*)/
	 if $1
		@buf_comments << $1 << "\n"
		add_comment($1+ "\n")
	end

		
end

def mark(toks)
	case toks[1]
	when 'MARK'
		@pos_node['LB']= get_coords(toks[4],toks[5])+':'+toks[6]
	when 'TRIANGLE'
		@pos_node['TR']= get_coords(toks[4],toks[5])
	when 'SQUARE'
		@pos_node['SQ']= get_coords(toks[4],toks[5])
	when 'CROSS'
		@pos_node['MA']= get_coords(toks[4],toks[5])
	else
		debug toks
	end
end

 def push_node(node)
#debug "modif"
    node.add_comment(@buf_comments) unless @buf_comments.empty?
    @buf_comments= ''
    if @pos_node.next
       nxt= @pos_node.next
       nxt= nxt.var_next while nxt.var_next
       nxt.c_var= ?A if nxt.c_var.nil?
       node.c_var= nxt.c_var + 1
       node.var_prev = nxt
       node.prev = nxt.prev
	   nxt.var_next= node
    else
      @pos_node.next = node
      node.prev = @pos_node
      node.c_var= ?A

    end


    @pos_node = node
    @header_node = nil


  end

def info(toks)
	set_node_name(" #{toks[1]} #{toks[3]}")
end


  end
	

