
class GoObserver < GoReplay

  include IGSCP

  include IgsObserver


  attr_reader :top 

  def initialize( header,title=nil)

    super(nil)
    igs_init(header,title)

    @kind_of_talk= Defaults::DEFS['talk_observing']

    igscp_init

    @teach_game=false
    @last_move= nil


    if header
      @top.help_add('command','label'=>'Observing a game','command'=>proc { TkHtml.new("file://#{$current_path}/../help/gamewindow.html#observing") })

      @file_menu.entryconfigure('1','label'=>'Refresh','command'=>proc { refresh })
#  @file_menu.insert(3,'command', 'label' => 'Save','state'=>'disabled','command' => proc {save_file})
      @file_menu.delete(2)
      @timers = {'W' => TkAfter.new(1000,-1,proc {decrease_clock('W')}),
        'B' => TkAfter.new(1000,-1,proc {decrease_clock('B')})}
    end



  end

  def open_win
    open_win_basic
    open_cmd

    @cmdline=  CmdLine.new(@f2, proc { |msg| send_message(msg)}).pack('fill' =>'x','expand'=>'no')
    @top.title(TkDefaults::GUI['GoObserver.top']['title'] + $user)
  end

  def decrease_clock(who)
    @clocks[who] -= 1
    if  @clocks[who] <= 0 
      if @byos[who] == -1
        @clocks[who] = @byo_time 
        @byos[who] = 25
      else
      #guess? ;->     
      end

    end
    set_clock(who,@clocks[who])
    set_byo(who,@byos[who])
  end

  def stop_timers

    return if @goban.nil?
    @timers['W'].stop
    @timers['B'].stop
    @goban.unset_mark
    @w_p_inf.clear_all if @w_p_inf
    @b_p_inf.clear_all if @b_p_inf
  end

  def set_info (game_info)

    if game_info.teach
      @teach_game= true 
      @teacher = game_info.w_player# suppose white == black
      open_igscp_commands
    else
      open_win_info
    end


    igs_set_info(game_info)
    @last_move= -1
    enable_menus
    set_show_move_nb('0')

    @virtual_text= VirtualText.new(@goban.canvas,5)


  end

  def open_igscp_commands

    @igscp_cmd= VTexturedPane.new(@f1,Defaults::DEFS['CommonBackground'],10,5,1,7).pack('fill'=> 'both','expand'=>'yes','side'=>'left')

    @igscp_ctrl= TkLabel.new(@igscp_cmd,'text'=>'Control: OFF').pack

    TkButton.new( @igscp_cmd, 'text'=>'Control OFF', 'command'=> proc {
                 IGSCP.send(@teacher,'CONTROL:REQUEST',@number,'OFF')
                 show_control('OFF')
                 @igscp_control_color= 'OFF'
                 ready } ).pack

  end

  def	show_control(color)	
    @igscp_ctrl.configure('text'=>"Control: #{color}")
  end

  def refresh 
    $igsClient.send("refresh #@number")
  end

  def reload 
    $igsClient.send('moves '+ @number.to_s)
  end

  def quit
    @last_move = nil
    igscp_send(@teacher,'CONTROL:REQUEST','OFF') if  @igscp_control_color and @igscp_control_color != 'OFF'
    close
    $gamesobserver.unobserve(@number)
  end

  def set_adjourned(line,auto_load=true)
    return if @root.nil?
    return if @game_adjourned
    super(line)
    blink(@msg, line,16)
    stop_timers
    $console.send_command("auto_load #@white-#@black") if auto_load 
  end

  def set_blink(msg)
    blink(@node_name,msg,8)
  end

  def blink(w,m,nb)
    return if m.empty?
    @timer.stop if @timer
    if nb % 2 == 0
      w.configure('text'=>m)
    else
      w.configure('text'=>'')
    end

    if nb>0
      @timer=TkAfter.new(500,1,proc {blink(w,m,nb-1)}) 
      @timer.start
    end
  end

  def set_completed(g,line)
    return if @root.nil?
    super(g,line)

    blink(@msg, 'Game completed',16)
    stop_timers
    playsound('Applause.wav') 
  end

  def set_resigned(line)
    return if @root.nil?
    super(line)
    blink(@msg,  'Game terminated',16)
    stop_timers
    playsound('Boo.wav')
  end

  def send_message(msg)
    @buf_comments << msg << "\n"
    putline(msg , 'user_cmd')
    $igsClient.send( @kind_of_talk+ ' ' + @number.to_s + ' ' + msg)
  end

  def rec_message(line,tag)
    @buf_comments << line << "\n" unless tag == 'tell'
    putline(line,tag) 
    playsound('ding.wav')
  end

  def putline(line,tag)
    @talk.putline(line + "\n", tag)
    @virtual_text.putline(line) if @show_right.value == '0'

  end

  def set_header(header)
    return if @top.nil?

    super(header)
    playsound('Stone.wav')

  end

  def set_turn(color)
    @timers[@player_turn].stop if @player_turn and not @teach_game
    super(color)
    @timers[@player_turn].start unless (@game_over or @teach_game)
  end

  def ready
    return if @goban.nil? or @top.nil? or @last_move.nil?# or @game_over
    if @player_turn == @igscp_control_color or  @igscp_control_color == 'BOTH'
      @goban.drag_stone(@player_turn){ |col,row| igscp_send_move(col,row) } 
    else
      @goban.undrag_stone
    end

  end

def set_fast_move(move)
@last_move = move.number
    super(move)

  end

  def set_move (move)
    return if @goban.nil? or @top.nil? or @last_move.nil?
    return if move.number != @last_move+1

    @last_move = move.number
    super(move)

  end

  def undo (msg)
    return if @goban.nil? or @top.nil? or @last_move.nil?
    if @teach_game
        set_backward
        @last_node= @last_node.prev
    else
        super()
	end
	@last_move -= 1
	set_node_name(msg) if msg
   end

  def set_status (status)
    return if @goban.nil? or @last_move.nil?


    @clocks['W'] = status.w_clock
    @clocks['B'] = status.b_clock
    @byos['B'] = status.b_byo
    @byos['W'] = status.w_byo

    super(status)

    playsound('win.wav')
  end

  def clear_comment; end
  def add_comment(line); end

  def set_clock(who,clock)
    return if @teach_game
    super(who,clock)
  end

  def set_byo(who,byo)
    return if @teach_game
    super(who,byo)
  end

end



