from prompt_toolkit import Application
from prompt_toolkit.interface import CommandLineInterface
from prompt_toolkit.shortcuts import create_eventloop
from prompt_toolkit.key_binding.manager import KeyBindingManager
from prompt_toolkit.keys import Keys
from prompt_toolkit.buffer import Buffer, AcceptAction
from prompt_toolkit.enums import DEFAULT_BUFFER, EditingMode
from prompt_toolkit.layout.containers import VSplit, Window, HSplit
from prompt_toolkit.layout.controls import BufferControl, FillControl, TokenListControl
from prompt_toolkit.layout.dimension import LayoutDimension as D
from prompt_toolkit.auto_suggest import AutoSuggestFromHistory
from pygments.token import Token


def get_titlebar_tokens(cli):
    return [
        (Token.Title, ' Hello world '),
        (Token.Title, ' (Press [Ctrl-Q] to quit.)'),
    ]


def handle_action(cli, buffer):
    



buffers = {
    DEFAULT_BUFFER: Buffer(is_multiline=True),
    'PROMPT': Buffer(
        accept_action=AcceptAction(handler=handle_action),
        enable_history_search=True,
        complete_while_typing=True,
        auto_suggest=AutoSuggestFromHistory()),
    'RESULT': Buffer(is_multiline=True),
}


def default_buffer_changed(cli):
    """
    When the buffer on the left (DEFAULT_BUFFER) changes, update the buffer on
    the right. We just reverse the text.
    """
    buffers['RESULT'].text = buffers[DEFAULT_BUFFER].text[::-1]


buffers[DEFAULT_BUFFER].on_text_changed += default_buffer_changed


def get_bottom_toolbar_tokens(cli):
    return [(Token.Toolbar, ' This is a toolbar. ')]


layout = VSplit([
                Window(content=BufferControl(
                    buffer_name=DEFAULT_BUFFER, focus_on_click=True)),
                Window(width=D.exact(1),
                       content=FillControl('|', token=Token.Line)),
                Window(content=BufferControl(buffer_name='RESULT'))
                ])
layout = HSplit([
                Window(height=D.exact(1),
                       content=TokenListControl(
                    get_titlebar_tokens, align_center=True
                )),
                Window(height=D.exact(1),
                       content=FillControl('-', token=Token.Line)),
                layout,
                Window(height=D.exact(1),
                       content=FillControl('-', token=Token.Line)),
                Window(height=D.exact(2),
                       content=BufferControl(
                    buffer_name='PROMPT',
                    focus_on_click=True)),
                ])

loop = create_eventloop()
manager = KeyBindingManager()
registry = manager.registry


@registry.add_binding(Keys.ControlQ, eager=True)
def exit_(event):
    event.cli.set_return_value(None)


application = Application(key_bindings_registry=registry, layout=layout,
                          buffers=buffers,
                          mouse_support=True,
                          use_alternate_screen=True,
                          editing_mode=EditingMode.VI
                          )
cli = CommandLineInterface(application=application, eventloop=loop)
cli.run()
print("Exiting")

' When enter is pressed in the Vi command line. '
    text = buffer.text  # Remember: leave_command_mode resets the buffer.
    buffer.delete_before_cursor(len(text))
    cli.focus(DEFAULT_BUFFER)

    # First leave command mode. We want to make sure that the working
    # pane is focussed again before executing the command handlers.
    # self.leave_command_mode(append_to_history=True)

    # Execute command.
    buffers[DEFAULT_BUFFER].insert_text(text)