"""Модуль тестов для проверки операций с компонентом ввода даты.

Содержит тесты для для проверки операций с компонентом ввода даты.
"""

import pytest
from playwright.sync_api import Page
from pages.main_page import MainPage
from pages.login_page import LoginPage


# @pytest.mark.smoke
class TestDateInputComponent:
    """Класс тестов для проверки операций с компонентом ввода даты.

    Атрибуты:
        browser: Фикстура для работы с браузером.
    """

    # @pytest.mark.develop
    def test_date_input_content(self, browser: Page) -> None:
        """Проверяет содержимое компонента ввода даты.
put
        Args:
            browser: Экземпляр страницы Playwright.
        """

        lp = LoginPage(browser)
        lp.do_login()

        mp = MainPage(browser)
        mp.should_be_event_panel()

        system_log_events_container = mp.click_events_panel_system_log_tab()
        sidebar_filter = system_log_events_container.click_filter_button()
        browser.wait_for_timeout(500)

        date_input = sidebar_filter.get_start_time_filter()
        date_input.check_content("Дата начала")

    # @pytest.mark.develop
    def test_date_input_by_text_positive(self, browser: Page) -> None:
        """Проверяет возможность задания даты с помощью текстового ввода для корректного входного значения.

        Args:
            browser: Экземпляр страницы Playwright.
        """

        lp = LoginPage(browser)
        lp.do_login()

        mp = MainPage(browser)
        mp.should_be_event_panel()

        system_log_events_container = mp.click_events_panel_system_log_tab()
        sidebar_filter = system_log_events_container.click_filter_button()
        browser.wait_for_timeout(500)

        date_input = sidebar_filter.get_start_time_filter()

        date_input.click_switch_mode_button()
        browser.wait_for_timeout(500)

        if date_input.is_text_input_mode():
            # выбираем 15 января 2024, проверяем результат
            expected_date = "15.01.2024"
            date_input.input_date(expected_date)
            browser.wait_for_timeout(300)
            actual_date = date_input.get_date_field_value()
            assert expected_date == actual_date, \
                f"Expected date {expected_date} is not equal actual date {actual_date}"
        else:
            assert False, "Should be text date input mode"

    # @pytest.mark.develop
    def test_date_input_by_text_negative(self, browser: Page) -> None:
        """Проверяет возможность задания даты с помощью текстового ввода для некорректных входных значений.

        Args:
            browser: Экземпляр страницы Playwright.
        """

        lp = LoginPage(browser)
        lp.do_login()

        mp = MainPage(browser)
        mp.should_be_event_panel()

        system_log_events_container = mp.click_events_panel_system_log_tab()
        sidebar_filter = system_log_events_container.click_filter_button()
        browser.wait_for_timeout(500)

        date_input = sidebar_filter.get_start_time_filter()

        date_input.click_switch_mode_button()
        browser.wait_for_timeout(500)

        if date_input.is_text_input_mode():
            try:
                date_input.input_date("1.01.2020")
            except AssertionError as e:
                actual_err = f"{e}"
                expected_err = "Incorrect day format: should be 'dd.mm.yyyy'"
                assert expected_err == actual_err, \
                    f"Expected error message: '{expected_err}' is nor equal actual error message: '{actual_err}'"

            try:
                date_input.input_date("ff.01.2020")
            except AssertionError as e:
                actual_err = f"{e}"
                expected_err = "Incorrect day value ff for selection"
                assert expected_err == actual_err, \
                    f"Expected error message: '{expected_err}' is nor equal actual error message: '{actual_err}'"

            try:
                date_input.input_date("01.1.2020")
            except AssertionError as e:
                actual_err = f"{e}"
                expected_err = "Incorrect month format: should be 'dd.mm.yyyy'"
                assert expected_err == actual_err, \
                    f"Expected error message: '{expected_err}' is nor equal actual error message: '{actual_err}'"

            try:
                date_input.input_date("01.o1.2020")
            except AssertionError as e:
                actual_err = f"{e}"
                expected_err = "Incorrect month value o1 for selection"
                assert expected_err == actual_err, \
                    f"Expected error message: '{expected_err}' is nor equal actual error message: '{actual_err}'"

            try:
                date_input.input_date("01.01.20")
            except AssertionError as e:
                actual_err = f"{e}"
                expected_err = "Incorrect year format: should be 'dd.mm.yyyy'"
                assert expected_err == actual_err, \
                    f"Expected error message: '{expected_err}' is nor equal actual error message: '{actual_err}'"

            try:
                date_input.input_date("01.01.2o2o")
            except AssertionError as e:
                actual_err = f"{e}"
                expected_err = "Incorrect year value 2o2o for selection"
                assert expected_err == actual_err, \
                    f"Expected error message: '{expected_err}' is nor equal actual error message: '{actual_err}'"
        else:
            assert False, "Should be text date input mode"

    # @pytest.mark.develop
    def test_time_input_after_text_positive(self, browser: Page) -> None:
        """Проверяет возможность задания времени для корректного входного значения.

        Args:
            browser: Экземпляр страницы Playwright.
        """

        lp = LoginPage(browser)
        lp.do_login()

        mp = MainPage(browser)
        mp.should_be_event_panel()

        system_log_events_container = mp.click_events_panel_system_log_tab()
        sidebar_filter = system_log_events_container.click_filter_button()
        browser.wait_for_timeout(500)

        date_input = sidebar_filter.get_start_time_filter()

        date_input.click_switch_mode_button()
        browser.wait_for_timeout(500)

        if date_input.is_text_input_mode():
            # выбираем 15 января 2024 10:11, проверяем результат
            date_input.input_date("15.01.2024")
            browser.wait_for_timeout(300)

            expected_time = "10:11"
            date_input.input_time(expected_time)

            actual_time = date_input.get_time_field_value()
            assert expected_time == actual_time, \
                f"Expected date {expected_time} is not equal actual date {actual_time}"
        else:
            assert False, "Should be text date input mode"

    # @pytest.mark.develop
    def test_time_input_after_date_picker_positive(self, browser: Page) -> None:
        """Проверяет возможность задания времени для корректного входного значения.

        Args:
            browser: Экземпляр страницы Playwright.
        """

        lp = LoginPage(browser)
        lp.do_login()

        mp = MainPage(browser)
        mp.should_be_event_panel()

        system_log_events_container = mp.click_events_panel_system_log_tab()
        sidebar_filter = system_log_events_container.click_filter_button()
        browser.wait_for_timeout(500)

        date_input = sidebar_filter.get_start_time_filter()

        date_input.click_switch_mode_button()
        browser.wait_for_timeout(500)
        date_input.click_switch_mode_button()
        browser.wait_for_timeout(500)

        if date_input.is_text_input_mode():
            assert False, "Should be date input mode by date picker"
        else:
            # выбираем 16 января 2024 08:11, проверяем результат
            date_input.input_date("16.01.2024")
            browser.wait_for_timeout(300)

            expected_time = "08:11"
            date_input.input_time(expected_time)

            actual_time = date_input.get_time_field_value()
            assert expected_time == actual_time, \
                f"Expected date {expected_time} is not equal actual date {actual_time}"

    # @pytest.mark.develop
    def test_time_input_negative(self, browser: Page) -> None:
        """Проверяет возможность задания времени для некорректного входного значения.

        Args:
            browser: Экземпляр страницы Playwright.
        """

        lp = LoginPage(browser)
        lp.do_login()

        mp = MainPage(browser)
        mp.should_be_event_panel()

        system_log_events_container = mp.click_events_panel_system_log_tab()
        sidebar_filter = system_log_events_container.click_filter_button()
        browser.wait_for_timeout(500)

        date_input = sidebar_filter.get_start_time_filter()

        date_input.click_switch_mode_button()
        browser.wait_for_timeout(500)

        if date_input.is_text_input_mode():
            # выбираем 15 января 2024 10:11, проверяем результат
            date_input.input_date("15.01.2024")
            browser.wait_for_timeout(300)

            try:
                date_input.time_date("1:01")
            except AssertionError as e:
                actual_err = f"{e}"
                expected_err = "Incorrect time format: should be 'hh:mm'"
                assert expected_err == actual_err, \
                    f"Expected error message: '{expected_err}' is nor equal actual error message: '{actual_err}'"

            try:
                date_input.input_date("o2.01")
            except AssertionError as e:
                actual_err = f"{e}"
                expected_err = "Incorrect hours value o2 for selection"
                assert expected_err == actual_err, \
                    f"Expected error message: '{expected_err}' is nor equal actual error message: '{actual_err}'"

            try:
                date_input.input_time("01:1")
            except AssertionError as e:
                actual_err = f"{e}"
                expected_err = "Incorrect time format: should be 'hh:mm'"
                assert expected_err == actual_err, \
                    f"Expected error message: '{expected_err}' is nor equal actual error message: '{actual_err}'"

            try:
                date_input.input_date("01:o1")
            except AssertionError as e:
                actual_err = f"{e}"
                expected_err = "Incorrect minutes value o1 for selection"
                assert expected_err == actual_err, \
                    f"Expected error message: '{expected_err}' is nor equal actual error message: '{actual_err}'"
        else:
            assert False, "Should be text date input mode"
