require "InputHandler.rb"

module RubyMud
class More < InputHandler
    # This input handler is used to display a large buffer, page-by-page
    # The user will be able to skip to a certain page, or see the next
    # page
    def initialize(clientconnection, text,pagesize=10)
        super(clientconnection)
        @theline = ""
        @pagesize = pagesize # 10 lines on a page
        @curpage = 1
        
        @page = []
        lines = text.split("\n")
        # we store the given text in pages
        start = 0
        part = lines[0,@pagesize]
        print "len = #{lines.length}"
        while part != nil
            @page.push(part)
            part = lines[start,@pagesize]
            start += @pagesize
        end
        
        self.DisplayPage(@curpage)
    end
    
    def Recieve(line)
        line = line.strip
        line = line.downcase
        if line == 'quit'
            SignOff()
            return
        end
        
        @curpage+=1
        DisplayPage(@curpage)
    end
    
    def SignOff
        @clientconnection.RemoveInputHandler(self)
        @clientconnection.Send("...finished\n")
    end
    
    def DisplayPage(pagenum)
        # Display the given page number
        page = @page[pagenum]
        if page == nil:
            SignOff()
            return
        end
        
        page.each { |line| @clientconnection.Send(line+"\n") }
        #@clientconnection.Send(page)
        DisplayFooter()
        
    end
    
    def DisplayFooter
        @clientconnection.Send("\n"+"---[white]<quit>[] to stop,any other for next page---\n")
    end
        
end
end