package io.trustbuilder.mfa.android.demoapp;

import static org.junit.Assert.assertEquals;

import android.app.Application;
import android.content.Intent;
import androidx.test.core.app.ApplicationProvider;

import org.robolectric.Shadows;
import org.robolectric.android.controller.ActivityController;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowActivity;
import org.robolectric.shadows.ShadowApplication;
import androidx.test.espresso.intent.rule.IntentsRule;
import androidx.test.ext.junit.rules.ActivityScenarioRule;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.filters.LargeTest;

import io.trustbuilder.mfa.android.demoapp.screen.*;
import io.trustbuilder.mfa.android.demoapp.utils.FirebaseService;

import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;

@RunWith(AndroidJUnit4.class)
@Config(application = TestApp.class)
@LargeTest
public class MainActivityTest {

    @Rule
    public ActivityScenarioRule<MainActivity> activityRule =
            new ActivityScenarioRule<>(MainActivity.class);

    @Rule
    public IntentsRule intentsRule = new IntentsRule();

    static class MockFirebaseService implements FirebaseService {
        @Override
        public void fetchToken(FirebaseCallback callback) {
            callback.onTokenReceived("fake-token");
        }
    }

    FirebaseService mockFirebaseService = callback ->
            callback.onTokenReceived("fake-token");

    @Test
    public void testOnResume_withMockFirebase() {
        Application app = ApplicationProvider.getApplicationContext();
        ShadowApplication shadowApp = Shadows.shadowOf(app);
        shadowApp.grantPermissions("android.permission.POST_NOTIFICATIONS");

        ActivityController<MainActivity> controller =
                Robolectric.buildActivity(MainActivity.class);

        controller.get()
                .setFirebaseServiceFactory(MockFirebaseService::new);

        MainActivity activity = controller.setup().get();

        activity.findViewById(R.id.SecureTokenListing).performClick();

        ShadowActivity shadowActivity = Shadows.shadowOf(activity);
        Intent startedIntent = shadowActivity.getNextStartedActivity();

        assertEquals(
                SecureTokenListingActivity.class.getName(),
                startedIntent.getComponent().getClassName()
        );
    }
    
    @Test
    public void clickOnRegisterPushId_shouldStartPushIdRegistrationWithFcmToken() {

        ActivityController<MainActivity> controller =
                Robolectric.buildActivity(MainActivity.class);

        controller.get()
                .setFirebaseServiceFactory(() -> callback ->
                        callback.onTokenReceived("fake-fcm-token"));

        MainActivity activity = controller.setup().resume().get();

        // WHEN
        activity.findViewById(R.id.PushIdRegistration).performClick();

        // THEN
        ShadowActivity shadowActivity = Shadows.shadowOf(activity);
        Intent startedIntent = shadowActivity.getNextStartedActivity();

        assertEquals(
                PushIdRegistrationActivity.class.getName(),
                startedIntent.getComponent().getClassName()
        );

        assertEquals(
                "fake-fcm-token",
                startedIntent.getStringExtra("FCM_TOKEN")
        );
    }
}
