
class GoProbs < GoWin

        def initialize
                super()
                @top.bind('Left',proc {set_backward;set_backward})

                @top.help_add('command','label'=>'solve problems','command'=>proc { TkHtml.new("file://#{$current_path}/../help/problems.html") })

                @file_menu=Menu.new
                @file_menu.add('command', 'label' => 'Open','command' => proc {open_file})
                @file_menu.add('command', 'label' => 'Previous','command' => proc {open_previous})
                @file_menu.add('command', 'label' => 'Next','command' => proc {open_next})
                @file_menu.add('separator')
                @file_menu.add('command', 'label' => 'Edit','command' => proc { open_editor})
                @file_menu.add('command', 'label' => 'View sgf','command' => proc { view_sgf})

                @file_menu.add('separator')
                @file_menu.add('command', 'label' => 'Quit','command' => proc { quit})
                @top.menubar.add('cascade','menu' => @file_menu, 'label' => 'File')

                @menu_turn= TkVariable.new('B')
                @turn_menu= Menu.new
                @turn_menu.add('radiobutton', 'label' => 'Black','variable'=>@menu_turn,'value'=>'B','command' => proc {change_turn('B')})
                @turn_menu.add('radiobutton', 'label' => 'White','variable'=>@menu_turn,'value'=>'W','command' => proc {change_turn('W')})
                @top.menubar.add('cascade','menu' =>  @turn_menu,'label' => 'Set turn')

        end

        def open_win
            open_win_basic
            open_win_btns
        end

        def change_turn(color)
            super( color)
            @menu_turn.value= color
        end

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

            opts=TkDefaults::GUI['GoProbs.button.begin'].update( {'command'=> proc {set_begin}})
            b=TkButton.new(@cmd2,opts).pack('side'=>'left')
            @cmd2.add(b)

            opts=TkDefaults::GUI['GoProbs.button.backward'].update( {'command'=> proc {set_backward;set_backward}})
            b=TkButton.new(@cmd2,opts).pack('side'=>'left')
            @cmd2.add(b)

            opts=TkDefaults::GUI['GoProbs.button.previous'].update( {'command'=> proc {open_previous}})
            b=TkButton.new(@cmd2,opts)
            @cmd2.add(b)


            opts=TkDefaults::GUI['GoProbs.button.next'].update( {'command'=> proc {open_next}})
            b=TkButton.new(@cmd2,opts).pack('side'=>'left')
            @cmd2.add(b)

            @cmd2.reconfigure
        end

        def set_begin
            set_backward  while @current_node['AW'].nil?
        end

        def set_root(root)
            super(root)
            goban.set_click { |col,row|  play(col,row)}
            set_node(@current_node.next) while @current_node['AW'].nil?
            @start_node= @current_node
	 		if @current_node.next['W']
        	  change_turn('W')
			else 
				change_turn('B')
			end
        end

        def play(col,row)
            if node=find_node(col,row)
                set_node(node)
                if node.next
                    Tk.update
                    sleep(1)
                    set_node(node.next)

                end
			else
				Tk.bell
            end
        end

        def find_node(col,row)
            node= @current_node.next
            while node
                prop= node[@player_turn]
                if prop and prop.col == col and prop.row == row
                    return node
                end
                node = node.var_next
            end
		
            nil
        end

        def set_node(node)

            @goban.remove_tag('Graphics')

            clear_msg
            clear_comment
            set_node_name('')
            set_node_annot('')

            super(node)
            @goban.raise_all

            set_msg("end of variation") if node.next.nil?
        end

        def set_backward
            return if  @current_node.prev.nil?  or @current_node == @start_node
            undo_node
            set_node @current_node.prev
        end

        def prob_list(offset)

            dir=File.dirname(@file)
            name= File.basename(@file)
            list=Dir.entries(dir).sort
            index= list.index(name)
            if f=list[index+offset] and f != '.' and f != '..'
                File.join(dir,f)
            else
                nil
            end

        end

        def open_next
            if f= prob_list(+1)
                open_file(f)
            end
        end

        def open_previous
            if f=prob_list(-1)
                open_file(f)
            end
        end

    end

