
class GoEditor < GoReplay

  def initialize(collection=nil)
    super

    @top.help_add('command','label'=>'edit a game','command'=>proc { TkHtml.new("file://#{$current_path}/../help/editor.html#editor") })

    @file_menu.insert(0,'command', 'label' => 'New','command' => proc{new_file})

    @node_menu= Menu.new
    @node_menu.add('command', 'label' => 'Setup','command' => proc {edit})
    @node_menu.add('command', 'label' => 'Cut','command' => proc {cut})
    @node_menu.add('command', 'label' => 'Name','command' => proc {node_set_name})

    @turn_menu= Menu.new
    @turn_menu.add('command', 'label' => 'Black','command' => proc {change_turn('B')})
    @turn_menu.add('command', 'label' => 'White','command' => proc {change_turn('W')})
    @node_menu.add('cascade','menu' =>  @turn_menu,'label' => 'Set turn')

    @node_menu.add('command', 'label'=> 'Pass', 'command'=> proc {click(-1,-1)})
    @node_menu.add('command', 'label' => 'Move number','command' => proc {set_move})


    @annot_menu= Menu.new

    @node_annotation.trace('w',proc { |v,v2,op| change_node_annot(v)})

    @annot_menu.add('radiobutton','label'=> 'Nothing','variable'=> @node_annotation, 'value'=> '')
    @annot_menu.add('radiobutton','label'=> 'Good for W','variable'=> @node_annotation, 'value'=> 'GW')
    @annot_menu.add('radiobutton','label'=> 'Good for B','variable'=> @node_annotation, 'value'=> 'GB')
    @annot_menu.add('radiobutton','label'=> 'Even position','variable'=> @node_annotation, 'value'=> 'DM')
    @annot_menu.add('radiobutton','label'=> 'Unclear position','variable'=> @node_annotation, 'value'=> 'UC')
    @annot_menu.add('radiobutton','label'=> 'Interesting  move','variable'=> @node_annotation, 'value'=> 'HO')
#TODO: 'V' score
    @node_menu.add('cascade','menu' =>  @annot_menu,'label' => 'Annotations')

    @collection_menu= Menu.new
    @collection_menu.add('command', 'label' => 'New','command' => proc {new_collection})
    @collection_menu.add('command', 'label' => 'Name','command' => proc {set_collection_name})


    @top.menubar.add('cascade','menu' => @node_menu, 'label' => 'Node','state'=>'disabled')
    @top.menubar.add('cascade','menu' => @collection_menu, 'label' => 'Collection','state'=>'disabled')
    @edit_sgf_info=true

    set_collection(collection) if collection

    @changed=false

  end

  def change_node_annot(v)
    if v.value.empty?
      set_node_annot('')
    else
      p=PNode.new(v.value)
      p.values.push('1')
      p.action(self)
    end
  end

  def node_set_name
    Misc.get_text('Enter name') { |str| @current_node.set('N',str);
      @current_node['N'].action(self)}
  end

  def new_collection
    Misc.get_text('New game name:') { |str| node= Node.new
      node.set('GN',str)
      @collect_nb.value= @collection.length
      @collection.push(node)
      set_collection_names
      set_root (node)
    }

  end

  def open_cmd
    super()

    @top.bind('KeyPress'){ |e| keypressed(e.keysym) }
  end

  def set_collection_name
    Misc.get_text('Game name:') { |str| @root.set('GN',str)
      set_collection_names
    }

  end

  def set_move
    Misc.get_text('Move number:') { |nb|  
      unless nb.nil?
        @current_node.set('MN',nb)
        node=@current_node
        undo_node
        set_node(node)
      end
    }
  end

  def open_file(file=nil)
    if file.nil? or File.readable?(file)
      super(file)
    else
      new_file 
      @file=file
      DefaultFiles.update('initialfile'=>  @file )
    end
  end

  def click(col,row)

    return if col.nil? or row.nil?

  # choose var if there is already a move there

    if col == -1
      move= ''
    else
      return if choose_var(col,row)
      move = Coords.to_a(col,row)
    end

  #debug "#{col} #{row} #{move}"

    @changed=true
    node= Node.new
    node[@player_turn]= move
    if @current_node.next.nil?
      node.c_var = ?A
      @current_node.next= node
      node.prev= @current_node
    else
      last_node = @current_node.next
      last_node = last_node.var_next until last_node.var_next.nil?
      last_node.c_var = ?A if last_node.c_var.nil?
      node.c_var= last_node.c_var + 1
      last_node.var_next= node
      node.var_prev = last_node
      node.prev = last_node.prev
    end

    set_node(node)
  end

# Doesn't accept two colors at the same variation(B&W at the same point)
  def choose_var(col,row)

    refnode = @current_node.next
    return false if refnode.nil?

    node= find_var(refnode,col,row)
    return false if node.nil?

    set_node node
    return true
  end

  def save_file

    if @current_node and @talk.modified
      text= @talk.get_text
      @current_node.delete('C')
      @current_node['C']= text unless text.empty?
    end

    super
  end

  def set_node (node)
#comment
    if @current_node 
      if @talk.modified 
        text= @talk.get_text if @talk.modified
        @current_node.delete('C')
        @current_node['C']= text unless text.empty?
      end
#annotation
      if @node_annotation.value != ''
        @current_node.set( @node_annotation.value,'1')
        @node_annotation.value=''
      end

#markups
      @current_node.delete('TR')
      @current_node.delete('CR')
      @current_node.delete('SQ')
      @current_node.delete('MA')
      @current_node.delete('LB')


      @goban.items.each_value { |item| 
        if item.kind_of?(Markup) # pas les Two_points ?
          id =  case item.class.name
           when "Triangle"
	     'TR'
	   when "Circle"
	     'CR'
	   when "Rectangle"
	     'SQ'
	   when "Cross"
	     'MA'
	   when "TextBoard"
	     'LB'
	   else 
	     break
	   end 

	   move = Coords.to_a(item.col,item.row)
	   if id == 'LB'
	     @current_node[id]= move+':'+item.text_value 
	   else
	     @current_node[id]=move
	   end

      end
   }
end
super(node)
end


def set_turn(color)
  super(color)
  ready unless @goban.fast_move
end

def ready
  if @edit_item 
    if @edit_item == Gomme
      @goban.drag_nil { |col,row|  @goban.delete_mark_at(col,row); @changed=true }
      @goban.cursor('pirate')
    elsif @edit_item == Text
      @goban.drag_text(@edit_char) { |col,row|
        @goban.add_text(@edit_char,col,row)
        @edit_char= @edit_char.succ
	 @changed=true
      }

    else
      @goban.drag_item(@edit_item){ |col,row|
        @goban.add_mark(@edit_item,col,row)
	 @changed=true
      }
    end
  else
    @goban.drag_stone(@player_turn) { |col,row| click(col,row) } 
  end

end

def add_setup_node(s_node)
@changed=true
  n= @current_node.next
  if n.nil?
    s_node.prev= @current_node
    @current_node.next= s_node
    s_node.c_var= ?A
  else

    n=n.var_next until n.var_next.nil?
    n.var_next= s_node
    s_node.var_prev= n
    s_node.prev= n.prev
    s_node.c_var= n.c_var + 1
  end
  set_node(s_node)
end

def replace_node(s_node)
@changed=true

  s_node.prev=  @current_node.prev
  @current_node.prev.next= s_node unless  @current_node.prev.nil? or @current_node.prev.next != @current_node
  s_node.next = @current_node.next
  @current_node.next.prev= s_node unless  @current_node.next.nil?
  s_node.var_prev=  @current_node.var_prev
  @current_node.var_prev.var_next= s_node unless  @current_node.var_prev.nil?
  s_node.var_next=  @current_node.var_next
  @current_node.var_next.var_prev= s_node unless  @current_node.var_next.nil?
  s_node.c_var=@current_node.c_var

  set_node(s_node)
end

def new_file
  form= Form.new('new_goban')
  frame= form.add_frame('GoEditor.frame.new',0,0)

  cols=TkVariable.new(19)
  form.add_label(frame,'GoEditor.label.new.Columns',0,0)
  form.add_field(frame,'GoEditor.entry.new',cols,'normal',1,0)
  rows=TkVariable.new(19)
  form.add_label(frame,'GoEditor.label.new.Rows',0,1)
  form.add_field(frame,'GoEditor.entry.new',rows,'normal',1,1)

  frame= form.add_frame('GoEditor.frame.new',0,1)
  frame.grid('ipadx'=>0, 'ipady'=>0)
  opts=TkDefaults::GUI['GoEditor.button.new.ok'].update('command'=> proc { create_file(cols.value,rows.value); form.destroy})
  ok= TkButton.new(frame,opts).pack('side'=>'left')

  opts=TkDefaults::GUI['GoEditor.button.new.cancel'].update('command'=> proc { form.destroy} )
  TkButton.new(frame,opts).pack('side'=>'left')

  form.bind('Return', proc { ok.configure('state'=>'active','relief'=>'sunken'); Tk.update; ok.invoke })


@changed=true


end


def cut

  return if @current_node== @root

@changed=true

  undo_node(@current_node)

  if not @current_node.var_prev.nil?
    node= @current_node.var_prev
    node.var_next= @current_node.var_next
    @current_node.var_next= node unless @current_node.var_next.nil?
  elsif not @current_node.var_next.nil?
    node= @current_node.var_next
    node.var_prev= nil
    node.prev.next= node
  else
    node=@current_node.prev
    node.next= nil
  end

  set_node(node)
end

def quit
if @changed
message='Save before closing?'
#rep=Tk::messageBox( 'icon'=> 'question', 'message'=> message, 'type'=>'yesno')
rep= Misc.ask(message,'yesnocancel')
   if rep == 'yes'
	save_file
   end
end

if rep != 'cancel'
	super
end
end
end


