package bou.amine.apps.readerforselfossv2.android.tests.robolectric

import android.widget.Button
import android.widget.EditText
import androidx.core.view.isVisible
import bou.amine.apps.readerforselfossv2.android.LoginActivity
import bou.amine.apps.readerforselfossv2.android.R
import com.google.android.material.switchmaterial.SwitchMaterial
import org.junit.Assert.assertEquals
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.Robolectric


@RunWith(RobotElectriqueRunnerclass::class)
class LoginActivityTest {

    @Test
    fun login_shouldDisplay() {
        Robolectric.buildActivity(LoginActivity::class.java).use { controller ->
            controller.setup() // Moves the Activity to the RESUMED state

            val activity = controller.get()
            assert(activity.findViewById<EditText>(R.id.urlView).isVisible)
            assert(activity.findViewById<SwitchMaterial>(R.id.selfSigned).isVisible)
            assert(activity.findViewById<SwitchMaterial>(R.id.selfSigned).isChecked.not())
            assert(activity.findViewById<SwitchMaterial>(R.id.withLogin).isVisible)
            assert(activity.findViewById<SwitchMaterial>(R.id.withLogin).isChecked.not())
        }
    }

    @Test
    fun urlError() {
        Robolectric.buildActivity(LoginActivity::class.java).use { controller ->
            controller.setup() // Moves the Activity to the RESUMED state
            val activity = controller.get()

            val urlView = activity.findViewById<EditText>(R.id.urlView)
            urlView.setText("172.17.0.1:8888")

            activity.findViewById<Button>(R.id.signInButton).performClick()

            urlView.performClick()
            assertEquals(activity.getString(R.string.login_url_problem), urlView.error)
        }
    }

    @Test
    fun multiError() {
        Robolectric.buildActivity(LoginActivity::class.java).use { controller ->
            controller.setup() // Moves the Activity to the RESUMED state
            val activity = controller.get()

            val signInButton = activity.findViewById<Button>(R.id.signInButton)
            repeat(3) { signInButton.performClick() }

            // Vérifie que l'avertissement est affiché
            assertEquals(activity.getString(R.string.text_wrong_url), dialogMessage())
        }
    }

    /* @Test
     fun connect() {
         Robolectric.buildActivity(LoginActivity::class.java).use { controller ->
             controller.setup() // Moves the Activity to the RESUMED state
             val activity = controller.get()
             val signInButton = activity.findViewById<Button>(R.id.signInButton)
             val urlView = activity.findViewById<EditText>(R.id.urlView)
             urlView.setText("http://10.0.2.2:8888")
             signInButton.performClick()

             val expectedIntent = Intent(activity, HomeActivity::class.java)
             val actual = shadowOf(activity).nextStartedActivity
             assertEquals(expectedIntent.component, actual.component)
         }
     }*/
}