module X_11
  require "X11/Display"

  def find_bg

    $d = X11::Display.new
    $s = $d.screens[0]
    $r= $s.root
    query= $r.query_tree
    list= query.windows


    w= list[0]


    if $r.get_geometry.rect == w.get_geometry.rect
      return w
    end
    nil


  end

  def lower(w)

    cfg= X11::WindowConfig.new(:stack_mode => :Below)
    w.configure(cfg)
    $d.flush
  end


end


class WallPaper < GoObserver

  include X_11

  @@wallpaper_top= nil


  def WallPaper.is_open
    @@wallpaper_top
  end

  def initialize( header = nil)

   #   debug header.inspect
    if header
      @last_node = Node.new

      @white= header.white
      @black= header.black
      @last_move= nil
      @game_over= false


      @collection= SGF.new
      @remove_captured= TkVariable.new(true)
      @player_turn= 'B'
      @move_nb = 0
      @show_move_nb= TkVariable.new(true) # sinon put_stone ne marche pas

    end

    @menu = Menu.new
    @menu.configure('tearoff'=>'false')
    @menu.add('command', 'font'=>'fixed', 'label' => 'show', 'command' => proc { $root.deiconify; GoWindow.raise_all} )
    @menu.add('command', 'font'=>'fixed', 'label' => 'hide', 'command' => proc { GoWindow.withdraw_all; $root.withdraw } )
    @menu.add('command', 'font'=>'fixed', 'label' => 'close', 'command' => proc { quit; $root.deiconify; GoWindow.raise_all; @@wallpaper_top= nil } )

    open_win
  end

  def popup( event )
    @menu.post( event.x_root, event.y_root)
  end

  def open_win

    buf=''
    root_id='' 


    if @@wallpaper_top
      @@wallpaper_top.destroy

    end


    @top= TkToplevel.new
    @top.overrideredirect(true)
    @top.bind('ButtonPress-3', proc  { |e| popup(e) })
    @top.bind('ButtonPress-1', proc  { |e| @menu.unpost })

    @@wallpaper_top= @top

    if Defaults::DEFS['WallPaper.geometry'] 

      @top.geometry(Defaults::DEFS['WallPaper.geometry'])
    else

      w=TkWinfo.screenwidth @top
      h= TkWinfo.screenheight @top

      mh= h/10

      size= h - mh*2

      @top.geometry("#{size}x#{size}+#{(w-size)/2}+#{mh}")
      Defaults::DEFS['connection_keep_alive']= true

    end

    bg= find_bg
    @top.lower
    lower(bg) if bg
    GoWindow.withdraw_all
    $root.withdraw


    @mid = TkFrame.new(@top,TkDefaults::GUI['Gowin.frame.mid']).pack('fill'=> 'both','expand'=>'yes')
    @node_bar= HTexturedPane.new(@mid, Defaults::DEFS['black'],0,2,10,0).pack('fill'=> 'x','side'=>'bottom')


    @msgline =  VTexturedPane.new(@mid,Defaults::DEFS['gowin-bg'],0,2,10,0).pack('fill'=>'x')
    @msgline.configure('relief'=>'raised', 'borderwidth'=>1)
    @msg = @msgline.add_text('fill'=> 'white')
    @msg.configure('text'=>'') # calcul de la hauteur



    @node_bar.configure('borderwidth'=>1, 'relief'=> 'raised')
    @node_name= @node_bar.add_text('fill'=>'white')
    @node_name.configure('text'=>'') #calcul de la hauteur
    @node_annot= @node_bar.add_text('fill'=>'white','text'=>'')

  end

  def decrease_clock(who)
  end

  def stop_timers
    @goban.unset_mark
  end

  def set_info (game_info)
    @title= "Game #{game_info.number}: #{game_info.w_player}[#{game_info.w_rank}](W) vs #{game_info.b_player}[#{game_info.b_rank}](B)"

    @number = game_info.number
    @size= game_info.size

    set_root(@last_node)

    @collection.push(@root)
    @last_move= -1
    @virtual_text= VirtualText.new(@goban.canvas,5)
  end

  def rec_message(line,tag)
    putline(line,tag) 

    playsound('ding.wav')
  end

  def putline(line,tag)
    @virtual_text.putline(line)
  end

  def set_header(header)
    @header_node = Node.new
    playsound('Stone.wav')

  end

  def set_komi(km)
  end

  def push_node(node)
    @last_node.next = node
    node.prev = @last_node
    node.c_var= ?A
    set_node(node) if @current_node == @last_node
    set_node_annot(@title)
    @last_node = node
    @header_node = nil
  end

  def set_turn(color)
  end

  def undo (msg)
    pop_node
    @last_move -= 1
  end

  def set_title(title)
    set_node_annot(title)
  end

  def clear_comment
  end

  def add_comment(line)

  end

  def add_comment(line)
  end

  def clear_turn(color); end
#def set_player_info(who)
#end
  def set_clock(who,clock); end
  def set_byo(who,byo); end

  def set_move_nb(nb)
    @move_nb = nb
  end

  def show_vars
  end

  def config_btns
  end

  def add_pris(color,col,row,nb)
    delete_stone(col,row)
  end


  def set_completed(g,line)
    super(g,line)
    $wallpaper_game=nil
  end

  def set_adjourned(line,auto_load=true)
    super(line,false)
    $wallpaper_game=nil
  end

  def set_resigned(line)
    super(line)
    $wallpaper_game=nil
  end



end

