class GoClient

  def initialize(connect=false)
    @server= Defaults.get_default_value('Server')
    @port= Defaults.get_default_value('Port')
    @user=Defaults.get_default_value('User')
    @password=Defaults.get_default_value('Password')

    if connect
      do_ok
      return
    end

    @top = GoWindow.new(self)

#@top.help_menu.add('command','label'=>'Connecting','command'=>proc { TkHtml.new("file://#{$current_path}/../help/index.html#goclient") })
#@top.help_menu.add('command','label'=>'about IGS','command'=>proc { TkHtml.new("file://#{$current_path}/../help/IGSreadme.txt") })




#   @help_file= $current_path+ '../IGSreadme'

    frame=  VTexturedPane.new(@top,Defaults::DEFS['CommonBackground'], 20,10,2,2).pack(
                                                                                       'fill'=>'both','expand'=>'yes','ipadx'=>20,'ipady'=>20)

    #TkLabel.new(frame, TkDefaults::GUI['GoClient.label.server']).grid( 'column'=>0,'row'=>0,'padx'=>5,'pady'=>5)
	@bserv=TkVariable.new
	TkOptionMenubutton.new(frame,@bserv,*Defaults::DEFS['Servers']).grid( 'column'=>0,'row'=>0,'padx'=>5,'pady'=>5)


    opts= TkDefaults::GUI['GoClient.entry.server'].update( { 'textvariable'=>@server})
    server= TkEntry.new(frame,opts).grid('column'=>1,'row'=>0)


    opts= TkDefaults::GUI['GoClient.entry.port'].update( { 'textvariable'=>@port})
    port= TkEntry.new(frame,opts).grid('column'=>2,'row'=>0)

	@bserv.trace('w',proc {  @server.value=@bserv.value 
								i= Defaults::DEFS['Servers'].index(@server.value)
								@port.value=  Defaults::DEFS['Ports'][i]
							})
	@bserv.value=	@server.value

    TkLabel.new(frame,TkDefaults::GUI['GoClient.label.user']).grid('column'=>0,'row'=>2,'padx'=>5,'pady'=>5)

    opts= TkDefaults::GUI['GoClient.entry.user'].update( { 'textvariable'=>@user})
    user= TkEntry.new(frame,opts).grid('column'=>1,'row'=>2)

    user.focus

    TkLabel.new(frame,TkDefaults::GUI['GoClient.label.password']).grid('column'=>0,'row'=>3,'padx'=>5,'pady'=>5)



    opts=TkDefaults::GUI['GoClient.entry.password'].update( { 'textvariable'=>@password, 'show'=>'*'})
    passwd= TkEntry.new(frame,opts).grid('column'=>1,'row'=>3)

    bottom=TkFrame.new(frame,TkDefaults::GUI['GoClient.frame.buttons']).grid('row'=>4,'columnspan'=>3,'padx'=>5,'pady'=>10)

    ok= TkButton.new(bottom,
                     TkDefaults::GUI['GoClient.button.ok'].update(  'borderwidth'=> 4,'command'=> proc {do_ok}  )
                    ).pack('side'=>'left')
    TkButton.new(bottom,
                 TkDefaults::GUI['GoClient.button.cancel'].update( 'command'=> proc {do_cancel })
                ).pack('side'=>'left')

    TkButton.new(bottom,
                 TkDefaults::GUI['GoClient.button.help'].update( 'command'=> proc {do_help })
                ).pack('side'=>'left')

#     user.bind('Tab', proc { passwd.focus })
    @top.bind('Return', proc { ok.configure('state'=>'active','relief'=>'sunken'); Tk.update; ok.invoke })


  end

  def do_ok

    $user= @user.value
    $server_ready = false

    begin

      $connection=IGSSocket.new(@server.value,@port.value,@user.value,@password.value)
    rescue
      Misc.warning( "Error\n"+$!)
      $stderr.puts 'Error while connecting to the server:'
      $stderr.puts $!
      return
    end

    close

    $server_name=@server.value


=begin
    Dir[$current_path + 'client/Plugins/*.rb'].each { |file|
      begin
        load file
      rescue Exception
        $stderr.puts "Error while loading #{file}"
        $stderr.puts $!
        $stderr.puts $@
      end
    }
    user_plugins=File.join($home,'Plugins')
    if test(?r,user_plugins)
      user_plugs= File.join(user_plugins,'*.rb')
      Dir[user_plugs].each { |file|
        begin
          load file
        rescue Exception
          $stderr.puts "Error while loading #{file}"
          $stderr.puts $!
        end
      }
      $stderr.puts "user Plugins loaded"
    else
      $stderr.puts "user Plugins not found"
    end

=end


 #  inp=Thread.new(){

#     }
#   puts "connected to server"


    $chatobserver= ChatObserver.new
    $console= Console.new
    $gameslist=  Games.new
    $gamesobserver= GamesObserver.new
    $playerslist = Players.new
    $statobserver= StatObserver.new
	$revobs=ReviewObserver.new
	#$reviewslist= Reviews.new
	$wlist= Wlist.new

    $igsClient = IGSClient.new($connection.socket)
    $igsClient.do_callbacks('banner',$connection.banner)


#TkAfter.new('10',-1,proc {$igsClient.getlines}).start
    $ip._eval('after 100 ruby igsclient_readlines')

#   $console.console_init_session
end

def do_cancel
  close
end

def do_help
  TkHtml.new("file://#{$current_path}/../help/IGSreadme.txt")
#    Help.new(@help_file) unless @help_file.nil?
end

def close

#  TkDefaults.save_top_options(self,@top)
  @top.destroy if  @top

end
end



