package com.example.tasks.screens

import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import androidx.compose.ui.test.performTextInput
import androidx.navigation.NavType
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import androidx.navigation.navArgument
import com.example.tasks.navigation.AppNavigationChief
import com.example.tasks.navigation.AppNavigationStaff
import com.example.tasks.navigation.Screen
import com.example.tasks.ui.theme.TasksTheme
import org.junit.Assert.*
import org.junit.Before
import org.junit.Rule
import org.junit.Test

class EntranceTest{
    @get:Rule(order = 0)
    var testRule = createComposeRule()

    @Before
    fun setup() {
        testRule.setContent {
            val navController = rememberNavController()
            TasksTheme {
                NavHost(navController = navController, startDestination = Screen.Entrance.route) {
                    composable(route = Screen.Entrance.route){
                        Entrance(navController = navController)
                    }
                    composable(
                        route = Screen.AppNavigationStaff.route + "/{surname}/{name}/{patronymic}",
                        arguments = listOf(
                            navArgument("surname") {
                                type = NavType.StringType
                            },
                            navArgument("name") {
                                type = NavType.StringType
                            },
                            navArgument("patronymic") {
                                type = NavType.StringType
                            },
                        )
                    )
                    {
                        val surname = it.arguments!!.getString("surname")
                        val name = it.arguments!!.getString("name")
                        val patronymic = it.arguments!!.getString("patronymic")
                        AppNavigationStaff(name = name!!, surname = surname!!, patronymic = patronymic!!, navController2 = navController)

                    }
                    composable(
                        route = Screen.AppNavigationChief.route + "/{surname}/{name}/{patronymic}/{division}",
                        arguments = listOf(
                            navArgument("surname") {
                                type = NavType.StringType
                            },
                            navArgument("name") {
                                type = NavType.StringType
                            },
                            navArgument("patronymic") {
                                type = NavType.StringType
                            },
                            navArgument("division") {
                                type = NavType.StringType
                            },
                        )
                    )
                    {
                        val surname = it.arguments!!.getString("surname")
                        val name = it.arguments!!.getString("name")
                        val patronymic = it.arguments!!.getString("patronymic")
                        val division = it.arguments!!.getString("division")
                        AppNavigationChief(name = name!!, surname = surname!!, patronymic = patronymic!!, division = division!!, navController2 = navController)

                    }
                }
            }


        }
    }


    @Test
    fun testEntrance_correctPasswordAndEmailStaff() {

        testRule.onNodeWithTag("CustomEmail").performTextInput("ivanov@mail.ru")
        testRule.onNodeWithTag("CustomPassword").performTextInput("1234567890!")
        testRule.onNodeWithTag("entrance").performClick()
        testRule.onNodeWithText("Новые задачи").assertIsDisplayed()

    }
    @Test
    fun testEntrance_incorrectPasswordStaff() {

        testRule.onNodeWithTag("CustomEmail").performTextInput("ivanov@mail.ru")
        testRule.onNodeWithTag("CustomPassword").performTextInput("1234567890")
        testRule.onNodeWithTag("entrance").performClick()
        testRule.onNodeWithText("Неверный пароль").assertIsDisplayed()

    }
    @Test
    fun testEntrance_incorrectEmailStaff() {

        testRule.onNodeWithTag("CustomEmail").performTextInput("ivanov@mal.ru")
        testRule.onNodeWithTag("CustomPassword").performTextInput("1234567890!")
        testRule.onNodeWithTag("entrance").performClick()
        testRule.onNodeWithText("Вы не зарегистрированы/неверно введена почта").assertIsDisplayed()

    }
    @Test
    fun testEntrance_correctPasswordAndEmailChief() {

        testRule.onNodeWithTag("CustomEmail").performTextInput("kbobarik@mail.ru")
        testRule.onNodeWithTag("CustomPassword").performTextInput("zvuki01")
        testRule.onNodeWithTag("entrance").performClick()
        testRule.onNodeWithText("Поставленные задачи").assertIsDisplayed()

    }
    @Test
    fun testEntrance_incorrectPasswordChief() {

        testRule.onNodeWithTag("CustomEmail").performTextInput("kbobarik@mail.ru")
        testRule.onNodeWithTag("CustomPassword").performTextInput("1234567890")
        testRule.onNodeWithTag("entrance").performClick()
        testRule.onNodeWithText("Неверный пароль").assertIsDisplayed()

    }
    @Test
    fun testEntrance_incorrectEmailChief() {

        testRule.onNodeWithTag("CustomEmail").performTextInput("kbobarik@mal.ru")
        testRule.onNodeWithTag("CustomPassword").performTextInput("zvuki01")
        testRule.onNodeWithTag("entrance").performClick()
        testRule.onNodeWithText("Неверный пароль").assertIsDisplayed()

    }

}