package org.aplas.myshop;

import static org.junit.Assert.assertNotNull;

import android.widget.Button;
import android.widget.EditText;

import androidx.lifecycle.Lifecycle;
import androidx.test.core.app.ActivityScenario;

import org.junit.After;
import org.junit.Before;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.MethodSorters;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import com.google.android.material.textfield.TextInputLayout;

@RunWith(RobolectricTestRunner.class)
@Config(manifest= Config.NONE)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class TestLoginFail extends ViewTest{
    ActivityScenario<LoginActivity> scenario;
    private EditText nama, password;
    private TextInputLayout namaLayout, passwordLayout;
    private Button loginButton;

    @Before
    public void setUp() {
        scenario = ActivityScenario.launch(LoginActivity.class);
        scenario.moveToState(Lifecycle.State.CREATED);
        scenario.onActivity(activity -> {
            nama = activity.findViewById(R.id.textInputEditTextUsername);
            namaLayout = activity.findViewById(R.id.textInputLayoutUsername);
            password = activity.findViewById(R.id.textInputEditTextPassword);
            passwordLayout = activity.findViewById(R.id.textInputLayoutPassword);
            loginButton = activity.findViewById(R.id.appCompatButtonLogin);
        });
    }

    @After
    public void tearDown() {
        scenario.close();
    }

    @Test
    public void check_01_EmptyName() {
        scenario.onActivity(activity -> {
            nama.setText("");
            password.setText("password");
            loginButton.performClick();
            String expectedErrorMessage = "Enter Username";
            assertNotNull("The text layout should throw error when username is empty, please check your input validation inside LoginActivity and make sure it has the same message according to guide file", namaLayout.getError());
            String actualErrorMessage = namaLayout.getError().toString();
            testItem(expectedErrorMessage, actualErrorMessage, "Error message does not match, please check your input validation inside LoginActivity and make sure it has the same message according to guide file", 1);
        });
    }

    @Test
    public void check_02_EmptyPassword() {
        scenario.onActivity(activity -> {
            nama.setText("test@example.com");
            password.setText("");
            loginButton.performClick();
            String expectedErrorMessage = "Enter Password";
            assertNotNull("The text layout should throw error when password is empty, please check your input validation inside LoginActivity and make sure it has the same message according to guide file", passwordLayout.getError());
            String actualErrorMessage = passwordLayout.getError().toString();
            testItem(expectedErrorMessage, actualErrorMessage, "Error message does not match, please check your input validation inside LoginActivity and make sure it has the same message according to guide file", 1);
        });
    }
}
