# -*- coding: utf-8 -*-
#
# Copyright © Spyder Project Contributors
# Licensed under the terms of the MIT License
#
"""Tests for the plugin."""
# Third party imports
import pytest
from qtpy.QtCore import QEvent, Qt
from qtpy.QtGui import QKeyEvent, QFocusEvent

# Local imports
from spyder_okvim.spyder.confpage import OkvimConfigPage


def test_conf_page(vim_bot):
    """Test conf_page.

    Call the methods that is difficult to make test case.
    """
    main, _, _, vim, _ = vim_bot
    vim.get_icon()
    vim.switch_to_plugin()
    conf_page = OkvimConfigPage(vim, main)
    conf_page.setup_page()

    old_leader_key = conf_page.leaderkey_viewer.textbox.text()
    new_event = QKeyEvent(QEvent.KeyPress, Qt.Key_Control, Qt.NoModifier)
    conf_page.leaderkey_edit.keyPressEvent(new_event)

    assert conf_page.leaderkey_viewer.textbox.text() == old_leader_key

    new_event = QKeyEvent(QEvent.KeyPress, Qt.Key_unknown, Qt.NoModifier)
    conf_page.leaderkey_edit.keyPressEvent(new_event)

    assert conf_page.leaderkey_viewer.textbox.text() == old_leader_key

    new_event = QKeyEvent(QEvent.KeyPress, Qt.Key_Meta, Qt.NoModifier)
    conf_page.leaderkey_edit.keyPressEvent(new_event)

    assert conf_page.leaderkey_viewer.textbox.text() == old_leader_key

    new_event = QKeyEvent(QEvent.KeyPress, Qt.Key_Enter, Qt.NoModifier)
    conf_page.leaderkey_edit.keyPressEvent(new_event)

    assert conf_page.leaderkey_viewer.textbox.text() == "Enter"


def test_apply_config(vim_bot):
    """Run apply_plugin_settings method."""
    _, _, editor, vim, qtbot = vim_bot
    editor.set_text("foo Foo foo Foo")
    vim.vim_cmd.vim_status.cursor.set_cursor_pos(0)
    vim.vim_cmd.vim_status.reset_for_test()

    # test refresh the color of search result.
    cmd_line = vim.vim_cmd.commandline
    qtbot.keyClicks(cmd_line, "/foo")
    qtbot.keyPress(cmd_line, Qt.Key_Return)

    vim.apply_plugin_settings("")


def test_ctrl_u_b(vim_bot):
    """Test ^u ^b."""
    _, _, editor, vim, qtbot = vim_bot
    editor.set_text("a\nb\n")
    vim.vim_cmd.vim_status.cursor.set_cursor_pos(0)
    vim.vim_cmd.vim_status.reset_for_test()

    cmd_line = vim.vim_cmd.commandline
    qtbot.keyClicks(cmd_line, "j")

    event = QKeyEvent(QEvent.KeyPress, Qt.Key_U, Qt.ControlModifier)
    vim.vim_cmd.commandline.keyPressEvent(event)

    assert cmd_line.text() == ""
    assert editor.textCursor().position() == 0

    qtbot.keyClicks(cmd_line, "j")

    event = QKeyEvent(QEvent.KeyPress, Qt.Key_B, Qt.ControlModifier)
    vim.vim_cmd.commandline.keyPressEvent(event)

    assert cmd_line.text() == ""
    assert editor.textCursor().position() == 0


def test_ctrl_d_f(vim_bot):
    """Test ^d ^f."""
    _, _, editor, vim, qtbot = vim_bot
    editor.set_text("a\nb\n")
    vim.vim_cmd.vim_status.cursor.set_cursor_pos(0)
    vim.vim_cmd.vim_status.reset_for_test()

    cmd_line = vim.vim_cmd.commandline

    event = QKeyEvent(QEvent.KeyPress, Qt.Key_D, Qt.ControlModifier)
    vim.vim_cmd.commandline.keyPressEvent(event)

    # assert cmd_line.text() == ""
    # assert editor.textCursor().position() == 2

    qtbot.keyClicks(cmd_line, "k")

    event = QKeyEvent(QEvent.KeyPress, Qt.Key_F, Qt.ControlModifier)
    vim.vim_cmd.commandline.keyPressEvent(event)

    # assert cmd_line.text() == ""
    # assert editor.textCursor().position() == 2


def test_message(vim_bot):
    """Test message."""
    _, _, editor, vim, qtbot = vim_bot
    editor.set_text("a\nb\nc\nd\ne")
    vim.vim_cmd.vim_status.cursor.set_cursor_pos(0)
    vim.vim_cmd.vim_status.reset_for_test()

    cmd_line = vim.vim_cmd.commandline
    qtbot.keyClicks(cmd_line, "y2j")
    assert vim.vim_cmd.msg_label.text() == "3 lines yanked"

    cmd_line = vim.vim_cmd.commandline
    qtbot.keyClicks(cmd_line, "p")
    assert vim.vim_cmd.msg_label.text() == "3 more lines"

    vim.vim_cmd.msg_label.clear()
    cmd_line = vim.vim_cmd.commandline
    qtbot.keyClicks(cmd_line, "vp")
    assert vim.vim_cmd.msg_label.text() == "4 more lines"

    vim.vim_cmd.msg_label.clear()
    cmd_line = vim.vim_cmd.commandline
    qtbot.keyClicks(cmd_line, "Vp")
    assert vim.vim_cmd.msg_label.text() == "2 more lines"

    qtbot.keyClicks(cmd_line, "i")
    cmd_line.focusInEvent(QFocusEvent(QEvent.FocusIn, Qt.OtherFocusReason))
    assert vim.vim_cmd.msg_label.text() == ""

    qtbot.keyClicks(cmd_line, "d2j")
    assert vim.vim_cmd.msg_label.text() == "3 fewer lines"

    editor.set_text("a\nb\nc\nd\ne")
    qtbot.keyClicks(cmd_line, "c2j")
    assert vim.vim_cmd.msg_label.text() == "2 fewer lines"

    qtbot.keyClicks(cmd_line, ":")
    qtbot.keyPress(cmd_line, Qt.Key_Enter)
    assert vim.vim_cmd.msg_label.text() == ""

    vim.vim_cmd.msg_label.setText("a")
    qtbot.keyClicks(cmd_line, "/")
    qtbot.keyPress(cmd_line, Qt.Key_Enter)
    assert vim.vim_cmd.msg_label.text() == ""

    vim.vim_cmd.msg_label.setText("a")
    qtbot.keyClicks(cmd_line, "v/")
    qtbot.keyPress(cmd_line, Qt.Key_Enter)
    assert vim.vim_cmd.msg_label.text() == ""

    vim.vim_cmd.msg_label.setText("a")
    qtbot.keyClicks(cmd_line, "V/")
    qtbot.keyPress(cmd_line, Qt.Key_Enter)
    qtbot.keyPress(cmd_line, Qt.Key_Escape)
    assert vim.vim_cmd.msg_label.text() == ""

    vim.vim_cmd.msg_label.setText("a")
    editor.set_text("a\nb\nc\nd\ne")
    qtbot.keyClicks(cmd_line, "hx")
    qtbot.keyClicks(cmd_line, "u")
    assert vim.vim_cmd.msg_label.text() == "1 changes"

    vim.vim_cmd.msg_label.setText("a")
    qtbot.keyPress(cmd_line, Qt.Key_R, Qt.ControlModifier)
    assert vim.vim_cmd.msg_label.text() == "1 changes"

    qtbot.keyClicks(cmd_line, "d3j")
    qtbot.keyClicks(cmd_line, "u")
    assert vim.vim_cmd.msg_label.text() == "4 more lines"

    qtbot.keyPress(cmd_line, Qt.Key_R, Qt.ControlModifier)
    assert vim.vim_cmd.msg_label.text() == "4 fewer lines"

    qtbot.keyClicks(cmd_line, "p")
    qtbot.keyClicks(cmd_line, "u")
    assert vim.vim_cmd.msg_label.text() == "4 fewer lines"

    qtbot.keyPress(cmd_line, Qt.Key_R, Qt.ControlModifier)
    assert vim.vim_cmd.msg_label.text() == "4 more lines"

    editor.set_text("aaaaaa")
    qtbot.keyClicks(cmd_line, "/a")
    qtbot.keyPress(cmd_line, Qt.Key_Enter)
    assert vim.vim_cmd.msg_label.text() == "/a"

    vim.vim_cmd.msg_label.setText("a")
    qtbot.keyClicks(cmd_line, "n")
    assert vim.vim_cmd.msg_label.text() == "/a"

    qtbot.keyClicks(cmd_line, "N")
    assert vim.vim_cmd.msg_label.text() == "?a"
