
class GoSetup

  attr_reader :goban, :file_menu


  def quit
    @top.destroy
  end

  def initialize(editor,dimx=19,dimy=19)

    @dimx= dimx
    @dimy= dimy

    @editor= editor

    @top= GoWindow.new(self)

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

    @file_menu.add('command', 'label' => 'Add next','command' => proc{add_node})
    @file_menu.add('command', 'label' => 'replace node','command' => proc{replace_node})
    @file_menu.add('separator')
    @file_menu.add('command', 'label' => 'Cancel','command' => proc {quit})


    @top.menubar.add('cascade','menu' => @file_menu, 'label' => 'Node')

    @goban=Goban.new(@top,dimx,dimy).pack('fill'=> 'both','expand'=>'yes')

  end

  def add_node

    node= create_node(@editor.current_node)
    @editor.add_setup_node(node)
    @top.destroy
  end

  def replace_node
    if @editor.current_node.prev.nil?
 # don't put stones into a root node
      add_node
      return
    end

    @editor.undo_node # current_node not changed
    node= create_node(@editor.current_node.prev)

    if (node['W'] or  node['B']) and (node['AE'] or node['AB'] or node['AW'])

      Misc.warning("You can't setup a node containing a move.\n Use 'add as next' instead to add a setup node\nas a variation of this one.")
      return
    end

    @editor.replace_node(node)
    @top.destroy
  end


  def make_2p_coords(item)
    if item.col == item.c2 and item.row == item.r2
      Coords.to_a(item.col, item.row)
    else
      Coords.to_a(item.col, item.row)+ ':'+ Coords.to_a(item.c2, item.r2)
    end
  end

  def create_node(node_ref)
    node = Node.new

    board= goban.board
    gm=Array.new(goban.dimx)
    for i in 0...goban.dimx
      gm[i]=Array.new(goban.dimy)
      for j in 0...goban.dimy
        if board[i].nil? or board[i][j].nil?
          gm[i][j]=  ?E
        else
          gm[i][j]= (board[i][j].color== 'W' ? ?W : ?B)
        end
      end
    end

    og= @editor.game

    for col in 0...goban.dimx
      for row in 0...goban.dimy
        if og[col][row] != gm[col][row]
          if og[col][row] != ?E
            node['AE']= Coords.to_a(col, row)
          end
          node['AW']=  Coords.to_a(col, row) if gm[col][row]== ?W
          node['AB']=  Coords.to_a(col, row) if gm[col][row]== ?B
        end
      end
    end

    @goban.items.each_value { |item|


      case item.class.name

      when "Triangle"
        node['TR']=  Coords.to_a(item.col, item.row)

      when "Cross"
        node['MA']=  Coords.to_a(item.col, item.row)

      when "Rectangle"
        node['SQ']=  Coords.to_a(item.col, item.row)

      when "Circle"
        node['CR']=  Coords.to_a(item.col, item.row)

      when "TextBoard"
        node['LB']= Coords.to_a(item.col, item.row)+':'+item.text_value


      when "Dim"
        coords= make_2p_coords(item)
        node['DD']=  coords unless (not node_ref['DD'].nil?) and   node_ref['DD'].values.include?(coords)


      when  "View"
        coords= make_2p_coords(item)
        node['VW']=  coords unless (not node_ref['VW'].nil?) and   node_ref['VW'].values.include?(coords)

      when "Line"
        unless item.col == item.c2 and item.row == item.r2
          node['LN']=Coords.to_a(item.col, item.row)+ ':'+ Coords.to_a(item.c2, item.r2)
        end

      when "Arrow"
        unless item.col == item.c2 and item.row == item.r2
          node['AR']= Coords.to_a(item.col, item.row)+ ':'+ Coords.to_a(item.c2, item.r2)
        end

      when "Select"
        node['SL']=   make_2p_coords(item)


      end
    }

    return node

  end


end

