
class PlayerInfo < VTexturedPane

  include Misc
  attr_reader :pris

  def initialize(parent,color)
    @color=color
    @name= ''
    @rank= ''
    @fg =(color == 'white' ? 'black' : 'white')
    super(parent, Defaults::DEFS[color], 10,5,0,0)
    set_fixed_size(120,120)
     #@turn= TkcOval.new(self,8,8,15,15,'fill'=> @fg)

    @img_stone=(color == 'white' ? Defaults::BStones[12] : Defaults::WStones[12])
    @pris_img= TkcImage.new(self,40,110, 'image'=>@img_stone,'anchor'=>'nw')
    @pris_img.raise
    @turn= TkcImage.new(self,8,40, 'image'=> Defaults::ICONS['free'],'anchor'=>'nw')
    open_win

    @pris = 0
    @c_time = 0
    @current_byo = 0
    @name= ''
    @rank= ''
  end

  def highlight
    @turn.raise
  end

  def normal
    @turn.lower
  end

  def open_win
     #   str= @color + ': ' + @name + '(' + @rank + ')'
  #   @name_label= add_text('text'=>str,'fill'=>@fg)
    @name_label= TkButton.new(self,'text'=>'','foreground'=>@fg,'background'=>@color,
                              'padx'=>0,'pady'=>0)
    add(@name_label)

    @rank_label= add_text('text'=>'____','fill'=>@fg)

    str= '00:00:00'
    opts=TkDefaults::GUI['PlayerInfo.label.clock'].update( {'text'=>str,'background' =>  @color,'foreground'=>@fg})
    @clock=TkLabel.new(self,opts).pack('fill'=>'x','padx'=>5)
    add(@clock)
    @clock_bg =  @clock.cget('background')

    @byo= add_text('text'=> ' ','fill'=>@fg)
    @lpris= add_text('text'=>  '0','fill'=>@fg)

     

    reconfigure
   #   @fond.configure('width'=>128,'height'=>128)

  end

  def set_rank(rank)
    @rank= rank
    @rank_label.configure('text'=> rank)

  end

  def set_name(name)
    tr,id= PNames.to_english(name)
    @name= tr || name
    @name_label.configure('text'=>@name)
    if id
      @name_label.configure('command'=>proc{PNames.open_url(id)})
   elsif $igsClient
      @name_label.configure('command'=>proc{$igsClient.send("stats #@name")})
#     @name_label.flash
    end
    reconfigure
  end

  def inc_pris(nb=1)
    set_pris( @pris + nb)
  end

  def dec_pris(nb = 1)
    set_pris( @pris - nb)
  end

  def set_pris(pris)
    @pris = pris
    if @pris
      str=  pris.to_s
    else
      str =''
    end
    @lpris.configure('text'=> str)
  end

  def set_clock(clock)
    @c_time = clock

    if @c_time < 0
      @clock.configure('text'=> '__:__:__')
    else
      str =  to_string(clock)
      tags = Defaults.clock_alarm( @c_time,  @current_byo)
      tags = {'background'=> @clock_bg, 'foreground'=> @fg} if tags.nil?
      tags.update('text'=> str)
      @clock.configure(tags)
    end
  end

  def set_byo(byo)	

    @current_byo = byo
    if byo != -1
      str=  byo.to_s
      @byo.configure('text'=> str)
    else
      @byo.configure('text'=> '')
    end
  end

  def clear_all

    @c_time =-1
    @clock.configure('text'=> '__:__:__')
    set_byo -1
    set_pris 0
  end

  def to_string(c)
    s = c % 60
    r =( c - s ) / 60
    m = r % 60
    h = (r - m ) / 60
    sprintf("%02d:%02d:%02d",h,m,s)
  end

  def addtime(secs)
    set_clock( @c_time + secs)
  end

end



