package org.hisp.dhis.android.core;

import static com.google.common.truth.Truth.assertThat;

import android.support.test.runner.AndroidJUnit4;

import org.hisp.dhis.android.core.configuration.ConfigurationModel;
import org.hisp.dhis.android.core.data.api.BasicAuthenticatorFactory;
import org.hisp.dhis.android.core.data.database.AbsStoreTestCase;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.io.IOException;

import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;

@RunWith(AndroidJUnit4.class)
public class MetadataCallRealIntegrationShould extends AbsStoreTestCase {
    /**
     * A quick integration test that is probably flaky, but will help with finding bugs related to the
     * metadataSyncCall. It works against the demo server.
     */
    private D2 d2;
    Exception e;

    @Before
    @Override
    public void setUp() throws IOException {
        super.setUp();

        ConfigurationModel config = ConfigurationModel.builder()
                .serverUrl(HttpUrl.parse("https://play.dhis2.org/android-current/api/"))
                .build();

        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BASIC);
        d2 = new D2.Builder()
                .configuration(config)
                .databaseAdapter(databaseAdapter())
                .okHttpClient(
                        new OkHttpClient.Builder()
                                .addInterceptor(BasicAuthenticatorFactory.create(databaseAdapter()))
                                .addInterceptor(loggingInterceptor)
                                .build()
                ).build();
    }


   /* How to extract database from tests:
    edit: AbsStoreTestCase.java (adding database name.)
    DbOpenHelper dbOpenHelper = new DbOpenHelper(InstrumentationRegistry.getTargetContext().getApplicationContext(),
    "test.db");
    make a debugger break point where desired (after sync complete)

    Then while on the breakpoint :
    Android/platform-tools/adb pull /data/user/0/org.hisp.dhis.android.test/databases/test.db test.db

    in datagrip:
    pragma foreign_keys = on;
    pragma foreign_key_check;*/

    //This test is uncommented because technically it is flaky.
    //It depends on a live server to operate and the login is hardcoded here.
    //Uncomment in order to quickly test changes vs a real server, but keep it uncommented after.
    @Test
    public void response_successful_on_sync_meta_data_two_times() throws Exception {
        retrofit2.Response response = null;
        response = d2.logIn("android", "Android123").call();
        assertThat(response.isSuccessful()).isTrue();

        //first sync:
        response = d2.syncMetaData().call();
        assertThat(response.isSuccessful()).isTrue();

        //second sync:
        response = d2.syncMetaData().call();
        assertThat(response.isSuccessful()).isTrue();

        //TODO: add aditional sync + break point.
        //when debugger stops at the new break point manually change metadata online & resume.
        //This way I can make sure that additive (updates) work as well.
        //The changes could be to one of the programs, adding stuff to it.
        // adding a new program..etc.
    }

    //@Test
    public void response_successful_on_login_logout_and_login() throws Exception {
        retrofit2.Response response = null;
        response = d2.logIn("android", "Android123").call();
        assertThat(response.isSuccessful()).isTrue();

        d2.logOut().call();

        response = d2.logIn("android", "Android123").call();
        assertThat(response.isSuccessful()).isTrue();
    }

    @Test
    public void stub() {
    }
}

@@ -178,101 +178,15 @@
 e.co
-nfiguration.ConfigurationModel;%0Aimport org.hisp.dhis.android.core.data.api.BasicAuthenticator
+mmon.D2
 Fact
@@ -256,16 +256,80 @@
 stCase;%0A
+import org.hisp.dhis.android.core.data.server.RealServerMother;%0A
 import o
@@ -435,109 +435,8 @@
 n;%0A%0A
-import okhttp3.HttpUrl;%0Aimport okhttp3.OkHttpClient;%0Aimport okhttp3.logging.HttpLoggingInterceptor;%0A%0A
 @Run
@@ -641,16 +641,23 @@
 lated to
+%0A     *
  the%0A   
@@ -864,773 +864,69 @@
     
-ConfigurationModel config = ConfigurationModel.builder()%0A                .serverUrl(HttpUrl.parse(%22https://play.dhis2.org/android-current/api/%22))%0A                .build();%0A%0A        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();%0A        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BASIC);%0A        d2 = new D2.Builder()%0A                .configuration(config)%0A                .databaseAdapter(databaseAdapter())%0A                .okHttpClient(%0A                        new OkHttpClient.Builder()%0A                                .addInterceptor(BasicAuthenticatorFactory.create(databaseAdapter()))%0A                                .addInterceptor(loggingInterceptor)%0A                                .build()%0A                ).build(
+d2 = D2Factory.create(RealServerMother.url, databaseAdapter()
 );%0A 
@@ -1121,16 +1121,21 @@
 ontext()
+%0A    
 .getAppl
@@ -1365,16 +1365,20 @@
 /test.db
+%0A   
  test.db
@@ -2541,18 +2541,16 @@
  %7D%0A%0A    
-//
 @Test%0A  
@@ -2943,49 +2943,7 @@
    %7D
-%0A%0A    @Test%0A    public void stub() %7B%0A    %7D
 %0A%7D%0A

remove duplicate code creating d2 with d2Factory