
module Browser

  def browse_url(adr, &action)
    @browse_action= action
    @browse_form= Form.new('browser')
    @browse_form.protocol('WM_DELETE_WINDOW', proc{ close_browser })

    frame= @browse_form.add_frame('browser.frame.bookmarks',0,0)

    @browse_form.add_label(frame,'browser.label.bookmarks',0,0)
    opts=TkDefaults::GUI['browser.list.bookmarks']
    fr=  TkFrame.new(frame)

    @b_list = TkListbox.new(fr,opts).pack('side'=>'left','expand'=>'yes','fill'=>'both')
    opts= TkDefaults::GUI['browser.scrollbar.bookmarks'].update( 'command' => proc { |*args| @b_list.yview *args} )
    scrollbar=TkScrollbar.new(fr,opts).pack( 
                                            'side' => 'left',
                                            'fill' => 'y')
    @b_list.yscrollcommand(proc { |*args| scrollbar.set( *args) })

    Defaults::BOOKMARKS.each { | a | @b_list.insert('end',a) }
    @browse_form.add(fr,0,1)

    @url= TkVariable.new
    @browse_form.add_field(frame,'browser.entry.url',@url,'normal',0,2)

    fr= TkFrame.new(frame)
    opts=TkDefaults::GUI['browser.button.add'].update('command'=> proc { @b_list.insert('end',@url.value) unless @url.value.empty?; @url.value='' })
    TkButton.new(fr,opts).pack('side'=>'left')  
    opts=TkDefaults::GUI['browser.button.delete'].update('command'=> proc { @b_list.delete('active') })
    TkButton.new(fr,opts).pack('side'=>'left')  
    @browse_form.add(fr,0,3)


    frame= @browse_form.add_frame('browser.frame.players',1,0)

    opts=TkDefaults::GUI['browser.button.browse'].update('command'=> proc { browse })
    #TkButton.new(frame,opts)  
    @browse_form.add(TkButton.new(frame,opts),0,0)

    frame= @browse_form.add_frame('browser.frame.games',2,0)

    @browse_form.add_label(frame,'browser.label.games',0,0)

    fr= TkFrame.new(frame)

    opts=TkDefaults::GUI['browser.list.games']
    @g_list = TkListbox.new(fr,opts).pack('side'=>'left','expand'=>'yes','fill'=>'both')


    opts= TkDefaults::GUI['browser.scrollbar.games'].update( 'command' => proc { |*args| @g_list.yview *args} )
    scrollbar2=TkScrollbar.new(fr,opts).pack( 
                                             'side' => 'left',
                                             'fill' => 'y')
    @g_list.yscrollcommand(proc { |*args| scrollbar2.set( *args) })

    @browse_form.add(fr,0,1)

    fr= TkFrame.new(frame)
    opts=TkDefaults::GUI['browser.button.open'].update('command'=> proc { open })
    TkButton.new(fr,opts).pack('side'=>'left')  
    opts=TkDefaults::GUI['browser.button.cancel'].update('command'=> proc { close_browser })
    TkButton.new(fr,opts).pack('side'=>'left')  
    @browse_form.add(fr,0,3)


    browse(adr) if adr

  end

  def close_browser
    Defaults::BOOKMARKS.clear
    @b_list.get(0,'end').each { | adr | Defaults::BOOKMARKS.push(adr) }
    @browse_form.destroy

  end

  def open
    return if @selected_url.nil?
    link= @g_list.get('active')
    return if link.empty?
    pair=@links.assoc(link)
    return unless pair
    adr= pair[1]
    return if adr.empty?
    path=  @selected_url.relative(adr)
    begin
      page= JF_HTML.new(path)
      case page.mime
      when 'text/html'
        @g_list.delete(0,'end')
        @links=[]
        display(page)
      when 'application/x-go-sgf','application/octet-stream'
        @browse_action.call( page.data)
        close_browser
      else
        Misc.warning("mime type #{page.mime} unknown")
      end
    rescue BadUrl => detail
      Misc.warning(detail.to_s)
    end
  end

  def browse(adr=nil)
    if adr
      @b_list.insert('end',adr)
    else
      adr= @b_list.get('active') 
    end

    return if adr.empty?
    adr = 'http://'+adr unless /^http/.match(adr)
    @g_list.delete(0,'end')
    @links=[]
    @selected_url=  JF_URL.new(adr)
    begin
      page= JF_HTML.new(@selected_url)
      display(page)
    rescue BadUrl => detail
      Misc.warning(detail.to_s)
    end
  end

  def display(page)

    page.links.each {|pair|
      if pair[1]  =~ /\.sgf$/i
        pair[0]= pair[1] if pair[0].empty?
        @g_list.insert('end',pair[0]) 
        @links.push(pair)
      end
    }


    page.frames.each { |adr|
      display(JF_HTML.new( @selected_url.relative(adr)))
    }

  end


end

