package com.punchthrough.bean.sdk;

import com.punchthrough.bean.sdk.message.BatteryLevel;
import com.punchthrough.bean.sdk.message.ScratchBank;
import com.punchthrough.bean.sdk.message.ScratchData;
import com.punchthrough.bean.sdk.util.BeanTestCase;

import com.punchthrough.bean.sdk.message.Callback;
import com.punchthrough.bean.sdk.message.DeviceInfo;

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

import static org.assertj.core.api.Assertions.assertThat;

/**
 * Integration tests for the Bean.
 *
 * Prerequisites:
 * - Bean within range
 * - Android device connected over USB
 */
public class TestBeanSimple extends BeanTestCase {

    public void setUp() {
        super.setUp();
        setUpTestBean();
    }

    public void tearDown() {
        super.tearDown();
        tearDownTestBean();
    }

    private boolean validHardwareVersion(String version) {
        System.out.println("Validating hardware version: " + version);
        return (
            version.equals("E") ||
            version.startsWith("1") ||
            version.startsWith("2")
        );
    }

    private boolean validFirmwareVersion(String version) {
        System.out.println("Validating firmware version: " + version);
        return version.length() > 0;
    }

    private boolean validSoftwareVersion(String version) {
        System.out.println("Validating software version: " + version);
        return version.length() > 0;
    }


    public void testBeanDeviceInfo() throws Exception {
        /** Read device information from a bean
         *
         * Warning: This test requires a nearby Bean
         */
        DeviceInfo info = getDeviceInformation(testBean);

        if (!validHardwareVersion(info.hardwareVersion())) {
            fail("Unexpected HW version: " + info.hardwareVersion());
        }
        if (!validFirmwareVersion(info.firmwareVersion())) {
            fail("Unexpected FW version: " + info.firmwareVersion());
        }
        if (!validSoftwareVersion(info.softwareVersion())) {
            fail("Unexpected SW version: " + info.softwareVersion());
        }
    }

    public void testReadFirmwareVersion() throws InterruptedException {
        final CountDownLatch l = new CountDownLatch(1);
        testBean.readFirmwareVersion(new Callback<String>() {
            @Override
            public void onResult(String result) {
                assertThat(validFirmwareVersion(result)).isTrue();
                l.countDown();
            }
        });
        l.await(20, TimeUnit.SECONDS);
    }

    public void testReadHardwareVersion() throws InterruptedException {
        final CountDownLatch l = new CountDownLatch(1);
        testBean.readHardwareVersion(new Callback<String>() {
            @Override
            public void onResult(String result) {
                assertThat(validHardwareVersion(result)).isTrue();
                l.countDown();
            }
        });
        l.await(20, TimeUnit.SECONDS);
    }

    public void testBeanReadWriteScratchBank() throws Exception {
        /** Test Scratch characteristic functionality
         *
         * Warning: This test requires a nearby Bean
         */

        // write to BANK_1 and BANK_5
        testBean.setScratchData(ScratchBank.BANK_1, new byte[]{11, 12, 13});
        testBean.setScratchData(ScratchBank.BANK_5, new byte[]{51, 52, 53});

        // read BANK_1
        final CountDownLatch scratch1Latch = new CountDownLatch(1);
        testBean.readScratchData(ScratchBank.BANK_1, new Callback<ScratchData>() {
            @Override
            public void onResult(ScratchData result) {
                assertThat(result.number()).isEqualTo(1);
                assertThat(result.data()[0]).isEqualTo((byte) 11);
                assertThat(result.data()[1]).isEqualTo((byte)12);
                assertThat(result.data()[2]).isEqualTo((byte) 13);
                scratch1Latch.countDown();
            }
        });

        scratch1Latch.await();

        // read BANK_5
        final CountDownLatch scratch5Latch = new CountDownLatch(1);
        testBean.readScratchData(ScratchBank.BANK_5, new Callback<ScratchData>() {
            @Override
            public void onResult(ScratchData result) {
                assertThat(result.number()).isEqualTo(5);
                assertThat(result.data()[0]).isEqualTo((byte) 51);
                assertThat(result.data()[1]).isEqualTo((byte) 52);
                assertThat(result.data()[2]).isEqualTo((byte) 53);
                scratch5Latch.countDown();
            }
        });

        scratch5Latch.await();
    }

    public void testBatteryProfile() throws Exception {

        final CountDownLatch tlatch = new CountDownLatch(1);
        testBean.readBatteryLevel(new Callback<BatteryLevel>() {
            @Override
            public void onResult(BatteryLevel result) {
                assertThat(result.getPercentage()).isGreaterThan(0);
                tlatch.countDown();
            }
        });
        tlatch.await(20, TimeUnit.SECONDS);
    }
}

@@ -29,16 +29,42 @@
 n.sdk;%0A%0A
+import android.util.Log;%0A%0A
 import c
@@ -110,16 +110,16 @@
 yLevel;%0A
-
 import c
@@ -692,24 +692,78 @@
 TestCase %7B%0A%0A
+    private static final String TAG = %22BeanManager%22;%0A%0A
     public v
@@ -988,35 +988,27 @@
         
-System.out.println(
+Log.i(TAG, 
 %22Validat
@@ -1255,35 +1255,27 @@
         
-System.out.println(
+Log.i(TAG, 
 %22Validat
@@ -1425,27 +1425,19 @@
     
-System.out.println(
+Log.i(TAG, 
 %22Val
@@ -2178,24 +2178,78 @@
 );%0A        %7D
+%0A%0A        Log.i(TAG, %22TEST PASSED: Read Device Info%22);
 %0A    %7D%0A%0A    
@@ -2596,32 +2596,98 @@
  l.countDown();%0A
+                Log.i(TAG, %22TEST PASSED: Read Firmware Version%22);%0A
             %7D%0A  
@@ -3094,32 +3094,98 @@
  l.countDown();%0A
+                Log.i(TAG, %22TEST PASSED: Read Hardware Version%22);%0A
             %7D%0A  
@@ -4174,32 +4174,91 @@
 ch.countDown();%0A
+                Log.i(TAG, %22TEST PASSED: Scratch Bank 1%22);%0A
             %7D%0A  
@@ -4845,32 +4845,91 @@
 ch.countDown();%0A
+                Log.i(TAG, %22TEST PASSED: Scratch Bank 5%22);%0A
             %7D%0A  
@@ -5307,16 +5307,16 @@
 han(0);%0A
-
         
@@ -5335,32 +5335,92 @@
 ch.countDown();%0A
+                Log.i(TAG, %22TEST PASSED: Battery Profile%22);%0A
             %7D%0A  

add some logging to TestBeanSimple