

class GoGame < GoBasic

  attr_reader :mark, :show_move_nb
  attr_accessor :delta_move, :fast_move

  def initialize(parent,dimx=19, dimy=19)
    super(parent,dimx,dimy)

    @canvas.bind('Motion',proc  { |e| do_motion(e)}) 
    @canvas.bind('ButtonPress-1', proc  { |e| do_release(e)}) 
    @canvas.bind('Enter', proc  { do_enter  })
    @canvas.bind('Leave', proc  { do_leave  })

    @removed= []

    @show_move_nb = Defaults.get_default_value('show_move_nb')
    @delta_move = 0
   #@mark=Dame.new(@canvas,@grid).not_movable
  # add(@mark,0,0)
    @mark=LastMove.new(@canvas,@grid)
    move(@mark,-3,-3)
  end

  def drag_stone(color, &action)
    @stone_placed= action
    undrag_item
    @stone.delete if @stone
    @stone = Stone.new(@canvas,@grid,color)
    @stone.raise
    @canvas.cursor('hand2')
    Tk.update
  end

  def undrag_stone  
    if @stone
      @stone.delete
      @stone=nil
    end
    @canvas.cursor('')
    Tk.update
  end

  def drag_mark(obj, action)

    @item_placed=action
    undrag_stone
    @item.delete if @item
    @item= obj
    @item.raise

    Tk.update

  end

  def drag_item(klass, &action)
    drag_mark(klass.new(@canvas,@grid),action)
  end

  def drag_text(text,&action)
    drag_mark(Text.new(@canvas,@grid,text),action)
  end


  def undrag_item  
    if @item
      @item.delete
      @item=nil
    end
    Tk.update
  end

  def drag_nil(&action)

    undrag_stone
    undrag_item
    @item_placed=action
  end



# oulà set_click attention !
  def set_click (&action)
    @canvas.bind('ButtonPress-1', proc  { |e| do_click(e,action) })
  end

  def set_shift_click (&action)
    @canvas.bind('Shift-ButtonPress-1', proc  { |e| do_click(e,action) })
  end

  def unset_shift_click 
    @canvas.bind('Shift-ButtonPress-1','')
  end
    
 def set_ctrl_click(&action)
    @canvas.bind('Control-ButtonPress-1', proc  { |e| find_move(e,action)}) 
 end


   def unset_ctrl_click
     @canvas.bind('Control-ButtonPress-1','')
   end
 
   
  def do_click(event,action)
    col,row = @grid.grid_round(event.x,event.y)
    action.call(col,row)
  end

  def do_motion(event)
    if @stone
      @stone.do_redraw(event.x,event.y,@step)
    elsif @item
      @item.do_redraw(event.x,event.y,@step)
    end
  end

  def do_release(event)
    col,row = @grid.grid_round(event.x,event.y)
    if @stone and  @stone_placed  
      if col and row and @board[col][row].nil?
        @stone.delete
        @stone=nil
        @stone_placed.call(col,row)
      end
    elsif @item and @item_placed
      if col and row and @markups[col][row].nil?
        @item_placed.call(col,row)
      end
    elsif @stone_placed
      if col and row and @item_placed
        @item_placed.call(col,row)
      end
    end
  end

def find_move (event,action)
    col,row = @grid.grid_round(event.x,event.y)
    if col and row and @board[col][row]
      action.call( @board[col][row].nb)
    end
end


  def do_enter
    @stone.raise if @stone
  end

  def do_leave
    @stone.lower if @stone
  end

  def remove(col,row)
    removed_pris=1
    color= @board[col][row].color

    obj= Point.new(@canvas,@grid,color)
    @removed.push(obj)
    delete(@board[col][row])
    add(obj,col,row)

    removed_pris += see(color,col-1,row)
    removed_pris += see(color,col+1,row)
    removed_pris += see(color,col,row-1)
    removed_pris += see(color,col,row+1)
    removed_pris
  end

  def see(color,col,row)
    pris=0
    if col >=0 and col< @dimx and row>=0 and row < @dimy
      if  @board[col][row] and @board[col][row].color == color
        pris= remove(col,row)
      end
    end
    pris
  end

  def del_remove
    removed_pris= 0
    @removed.each{ |point|
      col,row= point.col,point.row
      color=point.color
      point.delete
      add(Stone.new(@canvas,@grid,color),col,row)
      removed_pris += 1
    }
    @removed=[]
    removed_pris
  end

  def put_stone(color,col,row,nb=nil)

    unless @board[col][row] 
      stone= Stone.new(@canvas,@grid,color,nb)
      add(stone,col,row,false)
    end

    if @show_move_nb.value == '0' 
      set_mark(col,row) unless @fast_move
    else
      if @delta_move < nb
        new_nb=(nb - @delta_move).to_s 
      else
        new_nb= nb.to_s
        @delta_move= 0
      end
      if stone
        obj= TextMove.new(@canvas,@grid,new_nb)
        add( obj, col, row)
        stone.move_nb= obj
      end
    end
    stone
  end


  def set_numbering(col,row,nb)
   #   debug " #{col} #{row} #{nb}"

    stone= @board[col][row]
    return unless stone
    obj= TextMove.new(@canvas,@grid,nb)
    add( obj, col, row)
    stone.move_nb= obj

  end


  def set_mark(col,row)

    return if @show_move_nb.value != '0'
  #  unless @mark
   #   @mark=LastMove.new(@canvas,@grid)

#    end
    move(@mark,col,row)
    @mark.raise
  end

  def unset_mark

   # return unless @mark
    move(@mark,-3,-3)
    @mark.lower
  end

  def remove_all
    unset_mark
    super
  end

  def resize_items
    super
    @mark.paint #if @mark
  end

  def delete_stone(col,row)
    stone= @board[col][row]
    return unless stone
    if  stone.move_nb.nil?
      if  col == @mark.col and row == @mark.row 
        unset_mark
      end
    else
      delete(stone.move_nb)
    end
    delete(stone)
  end

  def clear_move_numbers
    @items.each_value { |item|
      if item.class== Stone 
        delete(item.move_nb)
        item.move_nb= nil
      end
    }
      end

      def delete_point(col,row)
        @items.each_value { |item| delete(item) if item.col == col and item.row == row }
      end

  # called by property
      def set_point(klass,color,col,row)
        if color.empty?
          add(klass.new(@canvas,@grid),col,row)# if @board[col][row].nil? and not klass.nil?
        else
          add(klass.new(@canvas,@grid,color),col,row)# if @board[col][row].nil? and not klass.nil?
        end
      end

      def add_text(text,col,row)
        add( TextBoard.new(@canvas,@grid,text), col, row)
      end

      def old_add_move_number(number,col,row)
        obj= TextMove.new(@canvas,@grid,number.to_s)
        add( obj, col, row)

      end

      def add_2p(klass,c1,r1,c2,r2)
        obj= klass.new(@canvas,@grid)
        add(obj,c1,r1,c2,r2)
      end

      def add_mark(klass,col,row)
        add(klass.new(@canvas,@grid), col,row)
      end

      def set_stone(color,col,row)
   #debug "#{color} #{col} #{row}"
    if color == ?E
      delete_point(col,row)
    else
      add(Stone.new(@canvas,@grid,color),col,row)
    end
  end

end

