package com.nbusy.app;

import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;

import com.nbusy.app.data.Chat;
import com.nbusy.app.data.DB;
import com.nbusy.app.data.UserProfile;
import com.nbusy.app.data.callbacks.CreateProfileCallback;
import com.nbusy.app.data.callbacks.GetProfileCallback;
import com.nbusy.app.data.sqldb.SQLDB;

import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.ArrayList;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

@RunWith(AndroidJUnit4.class)
public class SQLDBTest {
    public static void awaitThrows(CountDownLatch cdl) throws InterruptedException, TimeoutException {
        if (!cdl.await(5, TimeUnit.SECONDS)) {
            throw new TimeoutException("CountDownLatch timed out after awaiting for 5 seconds.");
        }
    }

    @Test
    public void getEmptyProfile() throws Exception {
        DB db = new SQLDB(InstrumentationRegistry.getTargetContext());

        final CountDownLatch cbCounter = new CountDownLatch(1);
        db.getProfile(new GetProfileCallback() {
            @Override
            public void profileRetrieved(UserProfile userProfile) {
            }

            @Override
            public void error() {
                cbCounter.countDown();
            }
        });
        awaitThrows(cbCounter);

        // todo: call getProfile again and verify that it still errors
    }

    @Test
    public void createThenGetProfile() throws TimeoutException, InterruptedException {
        DB db = new SQLDB(InstrumentationRegistry.getTargetContext());
        UserProfile profile = new UserProfile(
                "1234",
                "sadfsdgfgafdg",
                "chuck@nbusy.com",
                "chuck norris",
                new byte[]{0, 2, 3},
                new ArrayList<Chat>());

        final CountDownLatch cbCounter = new CountDownLatch(1);
        db.createProfile(profile, new CreateProfileCallback() {
            @Override
            public void success() {
                cbCounter.countDown();
            }

            @Override
            public void error() {

            }
        });
        awaitThrows(cbCounter);
    }
}

@@ -591,16 +591,54 @@
 ption;%0A%0A
+import static org.junit.Assert.fail;%0A%0A
 @RunWith
@@ -1283,24 +1283,113 @@
 rProfile) %7B%0A
+//                fail(%22expected empty profile%22);%0A                cbCounter.countDown();%0A
             
@@ -2335,16 +2335,121 @@
 rror() %7B
+%0A//                fail(%22didn't expect profile creation to fail%22);%0A                cbCounter.countDown();
 %0A%0A      
@@ -2460,32 +2460,34 @@
   %7D%0A        %7D);%0A
+//
         awaitThr

add failure conditions for profile info