

#  bitmapRow(f,'error','gray25','gray50','hourglass')
#  bitmapRow(f,'info','question','questhead','warning')


class NewUserInfo < UserInfo
  def initialize(name,rank)
    @name=name
    @info=''
    @country=''
    @rank=rank
    @won=''
    @lost=''
    @observing=''
    @playing=''
    @idle= ''
    @messages=''
    @game='?'
    @language=''

  end

end


class Players

  attr_reader :not_opened, :list

  def initialize

    @not_opened= false
    @user_list = {}

    open_win if Defaults.get_default_value('auto_start_Players').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 players list','command'=>proc { TkHtml.new("file://#{$current_path}/../help/client.html#players") })



      @cmd_country = Defaults.get_default_value('players_cmd_country')
      @cmd_rank1 = Defaults.get_default_value('players_cmd_rank1')
      @cmd_rank2 = Defaults.get_default_value('players_cmd_rank2')

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

      opts=TkDefaults::GUI['Players.button.reload'].update('command'=>proc {send_refresh})
      b_reload = TkButton.new(tools,opts)


      opts=TkDefaults::GUI['Players.frame.sort']
      sort_frame= TkFrame.new(tools,opts)

      @players_sort_type=Defaults.get_default_value('players_sort_type')
      opts= TkDefaults::GUI['Players.check.all'].update('variable'=> @players_sort_type,'value'=>'all')
      RadioButton.new(sort_frame,opts).pack('fill'=>'x')

      fr= TkFrame.new(sort_frame)

      opts= TkDefaults::GUI['Players.check.country'].update('variable'=> @players_sort_type,'value'=>'country')
      RadioButton.new(fr,opts).pack('fill'=>'x','side'=>'left')

      opts= TkDefaults::GUI['Players.entry.country'].update('textvariable'=>  @cmd_country )
      w=TkEntry.new(fr,opts).pack('side'=>'right')
      w.bind('Return', proc { send_refresh})

      fr.pack('fill'=>'x')

      fr= TkFrame.new(sort_frame)

      opts= TkDefaults::GUI['Players.check.rank'].update('variable'=> @players_sort_type,'value'=>'rank')
      RadioButton.new(fr,opts).pack('fill'=>'x','side'=>'left')

      opts= TkDefaults::GUI['Players.entry.rank2'].update('textvariable'=>  @cmd_rank2 )
      w=TkEntry.new(fr,opts).pack('side'=>'right')
      w.bind('Return', proc { send_refresh})

      opts= TkDefaults::GUI['Players.entry.rank1'].update('textvariable'=>  @cmd_rank1 )
      w=TkEntry.new(fr,opts).pack('side'=>'right')
      w.bind('Return', proc { send_refresh})

      fr.pack('fill'=>'x')

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

      opts=TkDefaults::GUI['Players.frame.filter']

      filter_frame= TkFrame.new(tools,opts)
      @show_new_users =  Defaults.get_default_value('show_new_users') 
      opts= TkDefaults::GUI['Players.check.new'].update(
                                                        'variable' => @show_new_users , 'offvalue' => '0' , 'onvalue' => '1' )
      CheckButton.new(filter_frame,opts).pack('fill'=>'x')

      @show_closed_users =  Defaults.get_default_value('show_closed_users') 
      opts= TkDefaults::GUI['Players.check.closed'].update(
                                                           'variable' => @show_closed_users , 'offvalue' => '0' , 'onvalue' => '1')
      CheckButton.new(filter_frame,opts).pack('fill'=>'x')

      @show_playing_users =  Defaults.get_default_value('show_playing_users') 
      opts= TkDefaults::GUI['Players.check.playing'].update(
                                                            'variable' => @show_playing_users , 'offvalue' => '0' , 'onvalue' => '1')
      CheckButton.new(filter_frame,opts).pack('fill'=>'x')

      filter_frame.pack('side'=> 'left','padx'=>5,'pady'=>3,'fill'=>'both')


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


      @info = TkLabel.new(@top,TkDefaults::GUI['Players.label.info'])
      @info.pack('side'=> 'bottom','expand'=>'no','fill'=>'x')


      Defaults.set_default_value('auto_start_Players', '1')
    end

    @not_opened= false

  end


  def send_refresh
    case @players_sort_type.value
    when 'all'
      cmd='user'
    when 'country'
      cmd='user '+@cmd_country.value
    when 'rank'
      cmd="user #{@cmd_rank1.value}-#{@cmd_rank2.value}"
    else
      cmd= 'user'
    end

    $igsClient.send(cmd)

  end

  def quit
    @top.destroy if @top
  end

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

  def rec_info(line)

    return if @top.nil? or @not_opened
    if line =~ / \d+ Players \d+ Total Games / #and @updating
      @info.configure( 'text' => $&)
    #  @updating= false
    #  @list.end_update
    #  @top.cursor('')
    end
  end

  def  rec_status(lines)
    line = lines.shift
      @top.cursor('watch') if @top
      @list.begin_update if @list
      lines.each { |line| 
	  user = UserInfo.new(line)
    if @top and not @not_opened

	  @user_list[user.name]= user.rank
	  @list.add(user) if  (user.game != 'X' or @show_closed_users.value == '1') and (user.playing.nil? or @show_playing_users.value == '1')

    end
	  $console.update_user(user.game) if user.name == $user
	  if $notify_list.include?(user.name)
	    $chatobserver.new_chat(user.name,"#{user.name} is connected") 
	    $notify_list.delete(user.name)
	  end
      }

      @list.end_update if @list
      @top.cursor('') if @top
    end

    def rec_user_list(line)
      if line =~ /Players/

#f=  @user_list &  @notify_list
#f.each { |user| 
        $notify_list.each { |user|
          if  @user_list.key?(user)
            $chatobserver.new_chat(user,"#{user} is connected") 
            $notify_list.delete(user)
          end
        }
#@user_list = []
      else
        t=line.split
  #@user_list.push(t[1],t[3],t[5],t[7],t[9])
        @user_list[t[1]]=t[0]
        @user_list[t[3]]=t[2]
        @user_list[t[5]]=t[4]
        @user_list[t[7]]=t[6]
        @user_list[t[9]]=t[8]

      end

    end

    def rec_shout(line)
      return if @top.nil? or @not_opened

      if line =~ /\{(\S+) \[(\S+) *\] has connected/ 
          @user_list[$1]=$2
          if @show_new_users.value == '1'
            @list.insert NewUserInfo.new($1 ,$2) if line[1..5] != 'guest'
            @list.refresh
          end
      elsif line =~ /\{(\S+) has disconnected\}/
        @list.del_user($1) 
      end
    end

    def newgame(white,black,nb)
      return if @top.nil? or @not_opened
      @list.newgame(white,nb)
      @list.newgame(black,nb)
    end

    def endgame(white,black,nb)
      return if @top.nil? or @not_opened
      @list.endgame(white,nb)
      @list.endgame(black,nb)
    end

    def get_user_info(name)
      @list.names.userinfo[name]
    end

    def get_user_rank(name)
      @user_list[name]
    end
  end



