

class NewGameInfo < GameInfo
  def initialize (number,wname,bname, wrank='', brank='',move = '')

    @number = number.to_i
    @w_player = wname
    @w_rank = wrank
    @b_player = bname
    @b_rank = brank
    @move = move
    @size = ''
    @handicap = ''
    @komi = ''
    @byo = ''
    @free = nil
    @teach = nil
    @tournament = nil
    @game = ''
    @observers = ''

  end
end


class Games

  attr_reader :not_opened


  def initialize

    @lock_update= true
    @not_opened = true

    open_win if Defaults.get_default_value('auto_start_Games').value == '1'

  end

  def open_win


    if @top
      @top.deiconify
    else

      @top=GoWindow.new(self)
      @top.protocol('WM_DELETE_WINDOW', proc { close })

      @top.help_add('command','label'=>'The games list','command'=>proc { TkHtml.new("file://#{$current_path}/../help/client.html#games") })


      tools= HTexturedPane.new(@top,Defaults::DEFS['CommonBackground'],10,5,4,0).pack('fill'=>'x')


      opts=TkDefaults::GUI['Games.button.reload'].update('command'=>proc {  @lock_update= false; $igsClient.send('games')})
      b_reload = TkButton.new(tools,opts)

      b_reload.pack('side'=> 'left','padx'=>5,'pady'=>3)

      @list = GamesList.new(@top).pack('fill'=>'both','expand'=>'yes')

    end

    Defaults.set_default_value('auto_start_Games', '1')
    @lock_update= false
    @not_opened= false
  end

  def quit
    @top.destroy if @top
  end

  def close
    if @top
      @top.withdraw 
    end
    @not_opened= true
    Defaults.set_default_value('auto_start_Games', '0')
  end

  def rec_prompt
    return   if @top.nil? or  @not_opened

    if  @updating
      @updating= false
      @list.end_update
      @top.cursor('')
    #@state.configure('image'=>Defaults::ICONS['ready'])
      @lock_update= true
    end
  end

  def  rec_status(line)

    return if  @lock_update or @not_opened

    if line[1..2] == '##'
    #@state.configure('image'=>Defaults::ICONS['update'])
        @updating= true
        @list.begin_update
        @top.cursor('watch')
    else
      begin
        game = GameInfo.new(line)
        @list.add(game)
      rescue Badline
        $stderr.puts(line)
      end
    end
  end

  def rec_shout(line)

    if m = /\{Game (\d+): (\S+) vs (\S+) :?(.*)\}/.match( line)
      if  m[4] =~ /@ Move (\d+)/
        @list.insert NewGameInfo.new(m[1],m[2], m[3],'','',$1) unless @updating or @not_opened
        $playerslist.newgame(m[2],m[3],$1) if $playerslist
      else
        @list.del_game(m[1].to_i,m[4]) unless @updating or @not_opened
        $playerslist.endgame(m[2],m[3],$1) if $playerslist
      end
      @list.refresh unless @updating or @not_opened

    elsif params= /\{Match (\d+): (\S+) \[ *(\S+)\] vs. (\S+) \[ *(\S+)\] \}/.match(line)
      if $wallpaper_mode and $wallpaper_game.nil? 
        if Defaults.show_game(params)
          $wallpaper_game = $1.to_i
          $igsClient.send('observe '+ $1)
        end
        return
      end
      unless @updating or @not_opened
        @list.insert NewGameInfo.new($1,$2,$4,$3,$5) 
        @list.refresh
      end
      $playerslist.newgame($2,$4,$1) if $playerslist
    end
  end


end




