package org.aplas.myshop;
import static org.junit.Assert.assertTrue;

import android.widget.Spinner;

import androidx.appcompat.widget.AppCompatButton;
import androidx.lifecycle.Lifecycle;
import androidx.test.core.app.ActivityScenario;

import org.aplas.myshop.sql.Database;
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;

@RunWith(RobolectricTestRunner.class)
@Config(manifest= Config.NONE)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class TestRegisterSuccess extends ViewTest{
    ActivityScenario<RegisterActivity> scenario;

    private TextInputEditText nama, password, confirmPassword;
    private AppCompatButton registerButton;
    private Spinner spinnerRoles;
    private Database database;

    @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);
            registerButton = activity.findViewById(R.id.appCompatButtonRegister);
            spinnerRoles = activity.findViewById(R.id.listRoles);
            database = new Database(activity);
        });
    }

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

    @Test
    public void check_01_Seller() {
        scenario.onActivity(activity -> {
            nama.setText("seller1@test.com");
            password.setText("password");
            confirmPassword.setText("password");
            spinnerRoles.setSelection(1);
            registerButton.performClick();
//            assertTrue(database.checkUser("seller1@test.com"));
            testItem(true, database.checkUser("seller1@test.com"), "Your register function doesn't seem to work, please check your register button on click function inside RegisterActivity or check Add User function inside the Database class", 3);
        });
    }

    @Test
    public void check_02_Buyer() {
        scenario.onActivity(activity -> {
            nama.setText("buyer1@test.com");
            password.setText("password");
            confirmPassword.setText("password");
            spinnerRoles.setSelection(2);
            registerButton.performClick();
//            assertTrue(database.checkUser("buyer1@test.com"));
            testItem(true, database.checkUser("seller1@test.com"), "Your register function doesn't seem to work, please check your register button on click function inside RegisterActivity or check Add User function inside the Database class", 3);
        });
    }
}
