/*
 * Copyright 2018 mk
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package pl.mk5.gdx.fireapp.android.analytics;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.utils.GdxNativesLoader;
import com.google.firebase.analytics.FirebaseAnalytics;

import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
import org.mockito.internal.verification.VerificationModeFactory;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;

import java.util.HashMap;

import pl.mk5.gdx.fireapp.GdxFIRAnalytics;
import pl.mk5.gdx.fireapp.android.AndroidContextTest;

@PrepareForTest({FirebaseAnalytics.class, GdxNativesLoader.class})
public class AnalyticsTest extends AndroidContextTest {

    private FirebaseAnalytics firebaseAnalytics;

    @Before
    @Override
    public void setup() throws Exception {
        super.setup();
        PowerMockito.mockStatic(FirebaseAnalytics.class);
        firebaseAnalytics = PowerMockito.mock(FirebaseAnalytics.class);
        Mockito.when(FirebaseAnalytics.getInstance(Mockito.any(Context.class))).thenReturn(firebaseAnalytics);
    }

    @Test
    public void logEvent() {
        // Given
        GdxFIRAnalytics analytics = GdxFIRAnalytics.instance();

        // When
        analytics.logEvent("test", new HashMap<String, String>());

        // Then
        PowerMockito.verifyStatic(FirebaseAnalytics.class, VerificationModeFactory.times(1));
        FirebaseAnalytics.getInstance((Context) Gdx.app);
        Mockito.verify(firebaseAnalytics, VerificationModeFactory.times(1)).logEvent(Mockito.eq("test"), Mockito.any(Bundle.class));
        Mockito.verifyNoMoreInteractions(firebaseAnalytics);
    }

    @Test
    public void setScreen() {
        // Given
        GdxFIRAnalytics analytics = GdxFIRAnalytics.instance();

        // When
        analytics.setScreen("test", AnalyticsTest.class);

        // Then
        PowerMockito.verifyStatic(FirebaseAnalytics.class, VerificationModeFactory.times(1));
        FirebaseAnalytics.getInstance((Context) Gdx.app);
        Mockito.verify(firebaseAnalytics, VerificationModeFactory.times(1))
                .setCurrentScreen(Mockito.eq((Activity) Gdx.app), Mockito.eq("test"), Mockito.eq(AnalyticsTest.class.getSimpleName()));
        Mockito.verifyNoMoreInteractions(firebaseAnalytics);
    }

    @Test
    public void setUserProperty() {
        // Given
        GdxFIRAnalytics analytics = GdxFIRAnalytics.instance();

        // When
        analytics.setUserProperty("test_name", "test_value");

        // Then
        PowerMockito.verifyStatic(FirebaseAnalytics.class, VerificationModeFactory.times(1));
        FirebaseAnalytics.getInstance((Context) Gdx.app);
        Mockito.verify(firebaseAnalytics, VerificationModeFactory.times(1))
                .setUserProperty(Mockito.eq("test_name"), Mockito.eq("test_value"));
        Mockito.verifyNoMoreInteractions(firebaseAnalytics);
    }

    @Test
    public void setUserId() {
        // Given
        GdxFIRAnalytics analytics = GdxFIRAnalytics.instance();

        // When
        analytics.setUserId("test");

        // Then
        PowerMockito.verifyStatic(FirebaseAnalytics.class, VerificationModeFactory.times(1));
        FirebaseAnalytics.getInstance((Context) Gdx.app);
        Mockito.verify(firebaseAnalytics, VerificationModeFactory.times(1))
                .setUserId(Mockito.eq("test"));
        Mockito.verifyNoMoreInteractions(firebaseAnalytics);
    }
}
@@ -1135,51 +1135,30 @@
 ap;%0A
-%0A
 import 
-pl.mk5.gdx.fireapp.GdxFIRAnalytics;
+java.util.Map;%0A
 %0Aimp
@@ -1779,38 +1779,32 @@
 / Given%0A        
-GdxFIR
 Analytics analyt
@@ -1801,38 +1801,36 @@
 ics analytics = 
-GdxFIR
+new 
 Analytics.instan
@@ -1818,33 +1818,24 @@
 ew Analytics
-.instance
 ();%0A%0A       
@@ -2310,99 +2310,900 @@
 oid 
-setScreen() %7B%0A        // Given%0A        GdxFIRAnalytics analytics = GdxFIRAnalytics.instance
+logEvent_withParams() %7B%0A        // Given%0A        Analytics analytics = new Analytics();%0A        Map%3CString, String%3E params = new HashMap%3C%3E();%0A        params.put(%22param_string%22, %22lorem ipsum%22);%0A        params.put(%22param_string_starting_with_number%22, %2212:00 hour%22);%0A        params.put(%22param_number_int%22, %2215%22);%0A        params.put(%22param_number_float%22, %2215.54%22);%0A%0A        // When%0A        analytics.logEvent(%22test%22, params);%0A%0A        // Then%0A        PowerMockito.verifyStatic(FirebaseAnalytics.class, VerificationModeFactory.times(1));%0A        FirebaseAnalytics.getInstance((Context) Gdx.app);%0A        Mockito.verify(firebaseAnalytics, VerificationModeFactory.times(1)).logEvent(Mockito.eq(%22test%22), Mockito.any(Bundle.class));%0A        Mockito.verifyNoMoreInteractions(firebaseAnalytics);%0A    %7D%0A%0A    @Test%0A    public void setScreen() %7B%0A        // Given%0A        Analytics analytics = new Analytics
 ();%0A
@@ -3789,38 +3789,32 @@
 / Given%0A        
-GdxFIR
 Analytics analyt
@@ -3811,38 +3811,36 @@
 ics analytics = 
-GdxFIR
+new 
 Analytics.instan
@@ -3828,33 +3828,24 @@
 ew Analytics
-.instance
 ();%0A%0A       
@@ -4378,22 +4378,16 @@
         
-GdxFIR
 Analytic
@@ -4404,14 +4404,12 @@
 s = 
-GdxFIR
+new 
 Anal
@@ -4417,17 +4417,8 @@
 tics
-.instance
 ();%0A

fix Android API: Wrong string/number recognizing in logEvent from v 1.9.x