
class GoTeacher < GoPlayer


  def initialize( header)
    super(header,"Teach Game #{header.number}")

    @node_menu= Menu.new
    @node_menu.add('command', 'label' => 'Setup','command' => proc {edit})
    @top.menubar.add('cascade','menu' => @node_menu, 'label' => 'Marks','state'=>'disabled')

    @teacher_color = ['W','B']
  end

  def open_win
    open_win_basic

    @f1.pack('expand'=>'yes')

    @cmd2= VTexturedPane.new(@f1,Defaults::DEFS['CommonBackground'],10,5,2,6).pack('fill'=> 'both','expand'=>'yes','side'=>'right')

    navpane = VTexturedPane.new(@cmd2, Defaults::DEFS['goreplay-bg'], 10,5,1,0).pack('pady'=>10,'padx'=>5,'fill'=>'x')

    navpane.add_text(TkDefaults::GUI['GoTeacher.title.moves'])

    navigate = TkFrame.new(navpane,TkDefaults::GUI['GoReplay.frame.navigate'])

    opts=TkDefaults::GUI['GoTeacher.button.begin'].update( {'command'=> proc {$igsClient.send('mark')}})
    TkButton.new(navigate,opts).pack('side'=>'left')

    opts=TkDefaults::GUI['GoTeacher.button.backward'].update( {'command'=> proc {$igsClient.send('undo')}})
    TkButton.new(navigate,opts).pack('side'=>'left')

    opts=TkDefaults::GUI['GoTeacher.button.forward'].update( {'command'=> proc {$igsClient.send('forward')}})
    TkButton.new(navigate,opts).pack('side'=>'left')

    navpane.add(navigate)
    navpane.configure(TkDefaults::GUI['GoTeacher.pane.navigate'])
    navpane.reconfigure

  @cmdline=  CmdLine.new(@f2, proc { |msg| send_message(msg)}).pack('fill' =>'x','expand'=>'no')


    
# TODO: mettre sur goban
    @top.bind('KeyPress'){ |e| keypressed(e.keysym) }
    @top.bind('Right',proc {$igsClient.send('forward')})
    @top.bind('Left',proc {$igsClient.send('undo')})
    @top.bind('Home',proc {$igsClient.send('mark')})


    opts=TkDefaults::GUI['GoPlayer.button.pass'].update( {'command'=> proc {send_pass}})
    TkButton.new(@cmd2,opts).pack('padx'=>5,'pady'=>5)

    opts=TkDefaults::GUI['GoPlayer.button.adjourn'].update( {'command'=> proc {send_adjourn}})
    TkButton.new(@cmd2,opts).pack('padx'=>5,'pady'=>5)

    @cmd2.focus

  end

  def open_igscp_commands

    @igscp_cmd= VTexturedPane.new(@f1,Defaults::DEFS['CommonBackground'],10,5,2,7).pack('fill'=> 'both','expand'=>'yes','side'=>'left')
    @player1= TkVariable.new
    TkEntry.new(@igscp_cmd,'textvariable'=>@player1).pack
    @ctrl1= TkVariable.new
    TkOptionMenubutton.new(@igscp_cmd,@ctrl1,'OFF','BLACK','WHITE','BOTH').pack
    @player2= TkVariable.new
    TkEntry.new(@igscp_cmd,'textvariable'=>@player2).pack
    @ctrl2= TkVariable.new
    TkOptionMenubutton.new(@igscp_cmd,@ctrl2,'OFF','BLACK','WHITE','BOTH').pack

    @ctrl1.trace('w',proc {control_changed( @player1,@ctrl1,@player2,@ctrl2) })
    @ctrl2.trace('w',proc {control_changed( @player2,@ctrl2,@player1,@ctrl1) })
  end

  def control_off(player)
    if @player1.value == player
      p_changed = @player1
      v_changed= @ctrl1
    elsif @player2.value == player
      p_changed = @player2
      v_changed= @ctrl2
    else 
      return
    end

    v_changed.value= 'OFF'
    set_teacher_colors

  end

  def control_changed( p_changed,v_changed,p_other,v_other)

    if p_changed.value.empty?
      v_changed.value='OFF' if v_changed.value != 'OFF'
      return
    end



    IGSCP.send( p_changed.value,'CONTROL:COLOR',@number,v_changed.value)

#debug "+#{p_changed.value}+"

    case v_changed.value
    when 'BLACK'
      case v_other.value
      when 'BLACK'
        v_other.value='OFF'
        IGSCP.send( p_other.value,'CONTROL:COLOR',@number,v_other.value)
      when 'BOTH'
        v_other.value='WHITE'
        IGSCP.send( p_other.value,'CONTROL:COLOR',@number,v_other.value)
      end
    when 'WHITE'
      case v_other.value
      when 'WHITE'
        v_other.value='OFF'
        IGSCP.send( p_other.value,'CONTROL:COLOR',@number,v_other.value)
      when 'BOTH'
        v_other.value='BLACK'
        IGSCP.send( p_other.value,'CONTROL:COLOR',@number,v_other.value)
      end

    when 'BOTH'
      case v_other.value
      when 'WHITE','BLACK','BOTH'
        v_other.value='OFF'
        IGSCP.send( p_other.value,'CONTROL:COLOR',@number,v_other.value)
      end

    end

    set_teacher_colors

    ready
  end

  def set_teacher_colors
    @teacher_color = ['W','B']
    @teacher_color.delete(IGSCP.to_color(@ctrl1.value))
    @teacher_color.delete(IGSCP.to_color(@ctrl2.value))

  end

  def set_turn(color)
    @player_turn = color
#debug 'sleep'
#sleep 1
  end

  def ready
    return if @goban.nil? or @top.nil? or @last_move.nil?# or @game_over
    if @edit_item 
      if @edit_item == Gomme
        @goban.drag_nil { |col,row|  @goban.delete_mark_at(col,row)
          igscp_remove_mark(col,row) }
        @goban.cursor('pirate')
      else
        @goban.drag_item(@edit_item){ |col,row|
          @goban.add_mark(@edit_item,col,row)
          igscp_send_mark(@edit_item,col,row)}
      end
    else
      if @teacher_color.include?(@player_turn)
        @goban.drag_stone(@player_turn){ |col,row| send_move(col,row) } 
      else
        @goban.undrag_stone
      end
    end
    Tk.update
  end

  def blink(w,m,nb)
  end

  def edit
    gos=super()
    gos.file_menu.entryconfigure(1,'label'=>'Put marks')
    gos.file_menu.delete(2)
  end

  def add_setup_node(s_node)
    @goban.remove_tag('Graphics')
    igscp_remove_all
    s_node.props.each_value { |prop| prop.action(self) }
    igscp_send_node(s_node)
  end

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

  end

  def set_move_nb(nb)
    @move_nb=nb
  end


  def set_node_name(txt);end
  def set_node_annot(txt);end
  def set_annot(txt);end
  def set_byo_time(v);end
  def set_time(v);end
  def set_number(v);end
  def set_type(v);end
  def set_handi(h);end
  def set_komi(k);end
  def set_rank(who,r);end
  def set_name(who,n);end
  def show_vars;end
  def config_btns;end
  def set_byo(who,byo);end
  def set_clock(who,clock);end


end
