

module IconBrowser

  def IconBrowser.open_browser(&action)
    @icon_action= action
    if @icon_browser
      @icon_browser.deiconify
    else
      IconBrowser.create_icon_browser
    end
  end

  def IconBrowser.create_icon_browser
    @icon_browser = GoWindow.new('icon_browser')#('title'=>'icon browser')
    @icon_browser.protocol('WM_DELETE_WINDOW', proc{ IconBrowser.close })
     #    @icon_canvas =  TkCanvas.new( @icon_browser).pack('fill'=>'both','expand'=>'yes')
    frame= TkFrame.new( @icon_browser).pack#('fill'=>'both','expand'=>'yes')
    col=row=0
    Defaults::HEADS.each { |file,img|
      b=TkButton.new( frame, 'image'=>img, 'command'=>proc{IconBrowser.selected(file)})
      b.grid('column'=> col,'row'=>row)
      col+=1
      if col == 10
        col=0
        row +=1
      end
    }
    frame= TkFrame.new(  @icon_browser).pack
    TkButton.new(frame,'text'=>'No icon','command'=> proc { IconBrowser.selected(nil) }
                ).grid('column'=> 0,'row'=>0)

    TkButton.new(frame,'text'=>'Cancel','command'=> proc { IconBrowser.close }
                ).grid('column'=> 1,'row'=>0)

  end

  def IconBrowser.close
    @icon_browser.withdraw
  end

  def IconBrowser.selected(file)
    @icon_action.call(file)
    @icon_browser.withdraw
  end

end


class Stats

  def initialize (stats)
    @stats= stats

    @notes= Defaults::NOTES[@stats.name]
    #@notes = {} unless @notes


    @form=  Form.new('Stats')
    @form.title(@stats.name)
   # @form.protocol('WM_DELETE_WINDOW', proc{ quit })

    frame= @form.add_frame('Stats.frame.stats',1,1)
    frame.grid('rowspan'=>2,'sticky'=>'ns')


    TkLabel.new(frame,TkDefaults::GUI['Stats.label.notes']).pack
    @text=TkText.new(frame,TkDefaults::GUI['Stats.text.notes']).pack('fill'=>'both','expand'=>'yes')


    frame= @form.add_frame('Stats.frame.stats',0,0)
    opts=TkDefaults::GUI['Stats.button.head'].update('command'=> proc {change_head},'image'=>Defaults::ICONS['unknown'])

    @btn_head= TkButton.new(frame,opts)
   # @btn_head.bind_balloon
    @form.add(@btn_head,0,0)


    @btn_head.grid('rowspan'=>3,'sticky'=>'ns')

    @notify= TkVariable.new('0')


    @decline= TkVariable.new('0')
    if @stats.name != $user
      @form.add_check(frame,'Stats.check.notify',@notify,1,0)
      @form.add_check(frame,'Stats.check.decline',@decline ,1,1)
    end

    if @notes
      @btn_head.configure('image'=>Defaults::HEADS[@notes['image']]) if @notes['image']
      @text.insert('end',@notes['text']) if @notes['text']
      @notify.value= @notes['notify'] if @notes['notify']
      @decline.value= @notes['decline'] if @notes['decline']
    end

    fr= TkFrame.new(@form.main).grid('column'=>1,'row'=>0)
    opts=  TkDefaults::GUI['Stats.button.close'].update('command'=>proc {  quit})
    TkButton.new(fr,opts).pack('expand'=>'yes')
    opts= TkDefaults::GUI['Stats.button.help'].update('command'=>proc {TkHtml.new("file://#{$current_path}/../help/client.html#stats")})
    TkButton.new(fr,opts).pack('fill'=>'x')


    #c=@print_form.add_check(frame,'stats.button.warn me when connected',@view_dvi,0,1)
    #c.configure('command'=> proc { @make_dvi.value= '1' if @view_dvi.value == '1'})
    frame= @form.add_frame('Stats.frame.stats',0,1)
    @row=0
    if @stats.last_access_GMT
      add_double(frame,'name',@stats.name,'not_on',nil)
    else
      add_double(frame,'name',@stats.name,'idle_time',@stats.idle_time)
    end
    add_double(frame,'rating',"#{@stats.rating} #{@stats.rating_nb}",'rank',"#{@stats.rank} #{@stats.rank_nb}")
    add_double(frame,'rated_games',@stats.rated_games,'language',@stats.language)
    add_double(frame,'wins',@stats.wins,'game',@stats.game)

    if  @stats.playing
      add_double(frame,'losses',@stats.losses,'playing',@stats.playing)
    else
      add_entry(frame,'losses',@stats.losses)
    end

    l=@form.add_empty(frame,0,@row,4,1)
    @row+=1

    if @stats.last_access_GMT
       #   l.configure(TkDefaults::GUI['Stats.label.not_on'])
       #       add_double(frame,'last_access',@stats.last_access_GMT,'last_local',@stats.last_access_local)
      add_entry(frame,'last_access',@stats.last_access_GMT,3)
      add_entry(frame,'last_local',@stats.last_access_local,3)
    end

    add_entry(frame,'address',@stats.address,3)
    add_entry(frame,'reg_date',@stats.reg_date,3)
    add_entry(frame,'info',@stats.info,3)
    add_entry(frame,'defaults',@stats.defaults,3)


    if @stats.name == $user
      @frame_opts= @form.add_frame('Stats.frame.stats',0,2)
      @form.add( TkLabel.new(@frame_opts,TkDefaults::GUI['Stats.label.key']),0,0)
      @form.add( TkLabel.new(@frame_opts,TkDefaults::GUI['Stats.label.value']),0,1)
    end

  end

  def quit

    if @notify.value == '1' or @decline.value== '1'
      @notes= {} unless @notes
    end


    if @notes
      @notes['notify']= @notify.value
      $chatobserver.notify(@stats.name) if @notify.value == '1'

      @notes['decline']= @decline.value
    end

    t=@text.get(1.0,'end').strip
    unless t.empty?
      @notes= {} unless @notes
      @notes['text']=t
    end
    Defaults::NOTES[@stats.name]= @notes if @notes
    @form.destroy
    @form= nil
  end

  def rec_opts(titles,values)
 # frame= @form.add_frame('Stats.frame.stats',0,2)
    i=1
    titles.split(' ').each {|v| @form.add( TkLabel.new(@frame_opts,TkDefaults::GUI['Stats.label.key'].update('text'=>v)),i,0)
      i+=1}
    i=1
    values.split(' ').each {|v| @form.add( TkLabel.new(@frame_opts,TkDefaults::GUI['Stats.entry.key'].update('text'=>v)),i,1)
      i+=1}
  end

  def add_double(frame,name,value,name2,value2)
    @form.add_label(frame,'Stats.label.'+name,0,@row)
    @form.add_field(frame,'Stats.entry.'+name,TkVariable.new(value),'disabled',1,@row)
    @form.add_label(frame,'Stats.label.'+name2,2,@row)
    @form.add_field(frame,'Stats.entry.'+name2,TkVariable.new(value2),'disabled',3,@row)

    @row+=1
  end

  def add_entry(frame,name,value,colspan=nil)
    @form.add_label(frame,'Stats.label.'+name,0,@row)
    f=@form.add_field(frame,'Stats.entry.'+name,TkVariable.new(value),'disabled',1,@row)
    f.grid('columnspan'=>colspan) if colspan
    @row+=1
  end

#DefaultFiles={'defaultextension'=>'.gif','filetypes'=> [['Image', '.gif']],'initialdir'=>Defaults::DEFS['heads_directory']}

  def change_head
    @notes= {} if @notes.nil?
    #file=@form.getOpenFile(DefaultFiles)
    #return if  file.empty?

    IconBrowser.open_browser{ |file|
      if @form
        if file
          @notes['image']=file
          @btn_head.configure('image'=>Defaults::HEADS[@notes['image']])
        else
          @notes.delete('image')
          @btn_head.configure('image'=>nil)
        end
      end
    }
  end

end

