

include Debug

class Grid
  attr_reader :marge, :step, :width, :height, :dimx, :dimy

  Hlabels= ['A','B','C','D','E','F','G','H','J','K','L','M','N','O','P','Q','R','S','T']
  #   Vlabels= ['1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19']
  Vlabels= ['19','18','17','16','15','14','13','12','11','10','9','8','7','6','5','4','3','2','1']


  def Grid.to_int (col,row,dimy)
    # "goban" coordinates starts at 0
    [Hlabels.index(col), Vlabels.index(row) - (19 - dimy) ]
  end

  def Grid.to_coords(col,row,dimy)
    Hlabels[col]+Vlabels[row + 19 - dimy]
  end

  def initialize(goban)
    @goban=goban
    @dimx=goban.dimx
    @dimy=goban.dimy
    @cpath=@goban.canvas.path


    @step=0
    @marge=0

    @x0=0
    @y0=0

    @hlines=Array.new(@dimy)
    for i in 0...@dimy
      # @hlines[i]=TkcLine.new(goban.canvas,0,0,0,0, 'tags'=> ['Grid','board'])
      @hlines[i]=$ip._eval("#@cpath create line 0 0 0 0  -tags {Grid board}")
    end

    @vlines=Array.new(@dimx)
    for i in 0...@dimx
       #@vlines[i]=TkcLine.new(goban.canvas,0,0,0,0, 'tags'=> ['Grid','board'])
      @vlines[i]=$ip._eval("#@cpath create line 0 0 0 0  -tags {Grid board}")
    end

    @hcoords=Array.new(@dimx)
    for i in 0...@dimx
    #@hcoords[i]= TkcText.new(goban.canvas,0,0, 'text'=> Hlabels[i], 'tags'=> ['Coords','board'] ) 
      @hcoords[i]=$ip._eval("#@cpath create text 0 0 -text #{Hlabels[i]}  -tags {Coords board}")
    end

    @vcoords=Array.new(@dimy)
    for i in 0...@dimy
    #@vcoords[i]= TkcText.new(goban.canvas,0,0, 'text'=> Vlabels[i+19- @dimy], 'tags'=> ['Coords','board'] )
      @vcoords[i]=$ip._eval("#@cpath create text 0 0 -text #{Vlabels[i+19- @dimy]}  -tags {Coords board}")

    end

    @hHoshi=Array.new()
    if @dimx >9
      @hHoshi.push(3)
      @hHoshi.push(@dimx/2 ) if @dimx%2 == 1
      @hHoshi.push(@dimx - 4)
    end

    @vHoshi=Array.new()
    if @dimy >9
      @vHoshi.push(3)
      @vHoshi.push(@dimy/2 ) if @dimy%2 == 1
      @vHoshi.push(@dimy - 4)
    end

    @hoshis=[]
    @hHoshi.each { |row|
      @vHoshi.each { |col|
        @hoshis.push(Hoshi.new(goban.canvas,self,row,col))
      }
    }

  end

  def coords(x,y,step)
    @x0=x
    @y0=y
    @step = step
    @width = @step * (@dimx+4)
    @height = @step * (@dimy+4)

    @marge=  @step*2


    for i in 0...@dimy
      x1,y1= grid_put(0,i)
      x2,y2= grid_put(@dimx-1,i)
  # @hlines[i].coords(x1,y1,x2,y2)
      $ip._eval("#@cpath coords #{@hlines[i]} #{x1} #{y1} #{x2} #{y2}")
    end

    for i in 0...@dimx
      x1,y1= grid_put(i,0)
      x2,y2= grid_put(i,@dimy-1)
  # @vlines[i].coords(x1,y1,x2,y2)
      $ip._eval("#@cpath coords #{@vlines[i]} #{x1} #{y1} #{x2} #{y2}")

    end


 #  if Defaults.get_default_value('show_coordinates').value == '1'
    for i in 0...@dimx
      x,y= grid_put(i,-1)
     #  @hcoords[i].coords(x,y)
      $ip._eval("#@cpath coords #{@hcoords[i]} #{x} #{y}")
    end

    for i in 0...@dimy
      x,y= grid_put(-1,i)
    #   @vcoords[i].coords(x,y)
      $ip._eval("#@cpath coords #{@vcoords[i]} #{x} #{y}")

    end
#  end


    @hoshis.each{ |h|
      h.paint
    }# if Defaults.get_default_value('show_hoshis').value == '1'

  end

  def grid_put(col,row)
    return if col.nil? or row.nil?
    x= (col * @step + @marge + @x0).round
    y= (row * @step + @marge + @y0).round

    [x,y]
  end

  def grid_round(x,y)
    x=(x - @marge).to_f
    y=( y - @marge).to_f

    col=(x / @step).round
    row=(y / @step).round


    col= (col >= 0 ? col : nil ) 
    col= (col < @dimx ? col : nil ) unless col.nil?

    row= (row >= 0 ? row : nil) 
    row= (row < @dimy ? row : nil)  unless row.nil?
 #     row = @dimy - row - 1 unless row.nil?

    return col,row

  end


end


