package org.aplas.myshop;

import static org.junit.Assert.assertNotNull;

import androidx.appcompat.widget.AppCompatButton;
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.TextInputEditText;
import com.google.android.material.textfield.TextInputLayout;

@RunWith(RobolectricTestRunner.class)
@Config(manifest= Config.NONE)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)

public class TestRegisterFail extends ViewTest{
    ActivityScenario<RegisterActivity> scenario;

    private TextInputEditText nama, password, confirmPassword;
    private TextInputLayout namaLayout, passwordLayout, confPasswordLayout;
    private AppCompatButton registerButton;

    @Before
    public void setUp() {
        scenario = ActivityScenario.launch(RegisterActivity.class);
        scenario.moveToState(Lifecycle.State.CREATED);
        scenario.onActivity(activity -> {
            nama = activity.findViewById(R.id.textInputEditTextName);
            password = activity.findViewById(R.id.textInputEditTextPassword);
            confirmPassword = activity.findViewById(R.id.textInputEditTextConfirmPassword);
            namaLayout = activity.findViewById(R.id.textInputLayoutName);
            passwordLayout = activity.findViewById(R.id.textInputLayoutPassword);
            confPasswordLayout = activity.findViewById(R.id.textInputLayoutConfirmPassword);
            registerButton = activity.findViewById(R.id.appCompatButtonRegister);
        });
    }

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

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

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

    @Test
    public void check_03_PasswordNotMatch() {
        scenario.onActivity(activity -> {
            nama.setText("test@example.com");
            password.setText("password");
            confirmPassword.setText("differentpassword");
            registerButton.performClick();
            String expectedErrorMessage = "Password Does Not Matches";
            assertNotNull("The text layout should throw error when confirm password does not match during registration, please check your confirm password input validation in RegisterActivity and make sure it has the same error message according to guide file.", confPasswordLayout.getError());
            String actualErrorMessage = confPasswordLayout.getError().toString();
            testItem(expectedErrorMessage, actualErrorMessage, "Error message does not match, please check your confirm password input validation in RegisterActivity and make sure it has the same error message according to guide file.", 1);
        });
    }
}
