class GoMatch


  def initialize ( title,vars)
    @send_message=TkVariable.new

    if vars.class == String
      @name= vars
      @color = Defaults.get_default_value('game_color')
      @size = Defaults.get_default_value('game_size')
      @time =  Defaults.get_default_value('game_time')
      @byo = Defaults.get_default_value('game_byo-yomi')
      @byo_stones = TkVariable.new('25') # ouie
      @send_message.value=1
    else
      @name= vars[1]
      @color = TkVariable.new(vars[2])
      @size= TkVariable.new(vars[3])
      @time= TkVariable.new(vars[4])
      @byo = TkVariable.new(vars[5])
      @byo_stones = TkVariable.new(vars[6])
      @send_message.value=0
    end

    @message= Defaults::get_default_value('match_message')

    @set_komi= TkVariable.new('0')
    @komi= TkVariable.new
    @set_free= TkVariable.new('0')

    @top= GoWindow.new(self)

#@top.help_menu.add('command','label'=>'The match window','command'=>proc { TkHtml.new("file://#{$current_path}/../help/manual.html#match") })

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

    f=frame

    if notes= Defaults::NOTES[@name] and notes['image']
      TkLabel.new(f, 'image'=>Defaults::HEADS[notes['image']]).grid('columnspan'=>2,'pady'=>10)
    end

    TkButton.new(f,TkDefaults::GUI['GoMatch.label.title'].update('text'=>title, 'command'=> proc {get_stats(@name)})).grid('columnspan'=>2,'pady'=>10,'row'=>2)

      #add_entry(f,'color',@color,1)
    TkLabel.new(f,TkDefaults::GUI['GoMatch.label.color'].update('text'=>$user+':')).grid(
                                                                                         'column'=>0,'row'=>3,'sticky'=>'ew')
=begin
    rb=TkFrame.new(f)
    bw=RadioButton.new(rb, 'text'=>'W','variable'=>@color,
                       'value'=>'W','command'=>proc {color_selected}).pack('side'=>'left')
                       bb=RadioButton.new(rb, 'text'=>'B','variable'=>@color,
                                          'value'=>'B','command'=>proc {color_selected}).pack('side'=>'left')

                                          rb.grid('column'=>1,'row'=>3)
=end


#TkButton.new(f,'image'=> img , 'command'=>proc {color_selected}).grid('column'=>1,'row'=>3)
    TkCheckbutton.new(f,'image'=> Defaults::WStones[20], 'selectimage'=>  Defaults::BStones[20] ,
                      'onvalue'=> 'B', 'offvalue'=>'W','variable'=>@color,
                      'indicatoron'=>'false','selectcolor'=>'','borderwidth'=>0,
                      'command'=>proc {color_selected}).grid('column'=>1,'row'=>3,'sticky'=>'ew')

    add_entry(f,'size',@size,4)
    add_entry(f,'time',@time,5)
    add_entry(f,'byo',@byo,6)
    add_entry(f,'byo_stones',@byo_stones,7)


    opts=TkDefaults::GUI['GoMatch.checkbutton.set_komi'].update('variable'=>@set_komi)
    cbk=CheckButton.new(f,opts).grid('column'=>0,'row'=>8,'pady'=>5,'sticky'=>'ew')

    opts= TkDefaults::GUI['GoMatch.entry.komi'].update('textvariable'=>@komi)
    kme=TkEntry.new(f,opts).grid('column'=>1,'row'=>8,'sticky'=>'ew')

    cbk.configure('command'=>proc {  if @set_komi.value == '1' 
					kme.configure('state'=>'normal')
					@komi.value='0.5'
				else
					kme.configure('state'=>'disabled')
					@komi.value=''
				end })

    opts=TkDefaults::GUI['GoMatch.checkbutton.set_free'].update('variable'=>@set_free)
    CheckButton.new(f,opts).grid('column'=>0,'row'=>9,'sticky'=>'w')

    opts=TkDefaults::GUI['GoMatch.checkbutton.message'].update('variable'=>@send_message)
    CheckButton.new(f,opts).grid('column'=>0,'row'=>10,'pady'=>5,'sticky'=>'w')
    opts= TkDefaults::GUI['GoMatch.entry.message'].update('text'=>@message)
    TkEntry.new(f,opts).grid('column'=>0,'row'=>11,'columnspan'=>2,'sticky'=>'ew')

    

    if vars.class == Array and vars[2].nil?
      @match_type= 'automatch'
    else
      @match_type= 'match'
    end

    fb=TkFrame.new(f).grid('column'=>0,'row'=>12,'columnspan'=>3,'pady'=>10)

    @btn_match= TkButton.new(fb,'text'=>@match_type, 'borderwidth'=> 4,'command'=> proc {do_match}).grid('column'=>0,'row'=>0)
    TkButton.new(fb,TkDefaults::GUI['GoMatch.button.decline'].update('command'=> proc {do_decline})).grid('column'=>1,'row'=>0)
    TkButton.new(fb,TkDefaults::GUI['GoMatch.button.help'].update('command'=> proc {do_help})).grid('column'=>2,'row'=>0)
    @top.bind('Return', proc { do_match})

    end

    def do_help
      TkHtml.new("file://#{$current_path}/../help/client.html#match")
    end

    def color_selected
      @match_type= 'match'
      @btn_match.configure('text'=>@match_type)
    end

    def add_entry(f,label,var,row)
      TkLabel.new(f,TkDefaults::GUI['GoMatch.label.'+label]).grid(
                                                                  'column'=>0,'row'=>row,'sticky'=>'ew')
      TkEntry.new(f,TkDefaults::GUI['GoMatch.entry.'+label].update('textvariable'=> var)).grid(
                                                                                               'column'=>1,'row'=>row,'sticky'=>'ew')

    end

    def do_match
      if @match_type == 'automatch'
        cmd= "automatch #{@name}"
      else
        cmd= "match #{@name} #{@color.value} #{@size.value} #{@time.value} #{@byo.value}"
      end
      if @set_komi== '1'
	 $set_komi= @komi
      else
	 $set_komi= nil
      end

      if @set_free == '1'
	 $set_free= '1'
      else
	 $set_free= nil
      end
      $igsClient.send("tell #@name #{@message.value}") if @message

      $console.send_command(cmd)
      close

    end

    def do_decline
      cmd= "decline #{@name}"
      $console.send_command(cmd)
      close
    end

    def get_stats(name)
      $console.send_command("stats #{name}")
    end

    def close

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

    end

  end

