package com.punchthrough.bean.sdk;

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 static org.assertj.core.api.Assertions.assertThat;

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

    public void testBeanDeviceInfo() throws Exception {
        /** Read device information from a bean
         *
         * Warning: This test requires a nearby bean named "TESTBEAN"
         */

        Bean bean = discoverBean("TESTBEAN");
        synchronousConnect(bean);

        // Get Device Information
        final CountDownLatch deviceInfoLatch = new CountDownLatch(1);
        bean.readDeviceInfo(new Callback<DeviceInfo>() {
            @Override
            public void onResult(DeviceInfo deviceInfo) {
                assertThat(deviceInfo).isNotNull();
                deviceInfoLatch.countDown();
            }
        });
        deviceInfoLatch.await();

        // Always disconnect at end of test so that other tests will pass
        synchronousDisconnect(bean);
    }

    public void testBeanReadWriteScratchBank() throws Exception {
        /** Test Scratch characteristic functionality
         *
         * Warning: This test requires a nearby bean named "TESTBEAN"
         */
        Bean bean = discoverBean("TESTBEAN");
        synchronousConnect(bean);

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

        // read BANK_1
        final CountDownLatch scratch1Latch = new CountDownLatch(1);
        bean.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);
        bean.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();

        // Always disconnect at end of test so that other tests will pass
        synchronousDisconnect(bean);
    }

}

@@ -592,16 +592,426 @@
 Case %7B%0A%0A
+    private boolean validHardwareVersion(String version) %7B%0A        return (%0A            version.equals(%22E%22) %7C%7C%0A            version.startsWith(%221%22) %7C%7C%0A            version.startsWith(%222%22)%0A        );%0A    %7D%0A%0A    private boolean validFirmwareVersion(String version) %7B%0A        return version.length() %3E 0;%0A    %7D%0A%0A    private boolean validSoftwareVersion(String version) %7B%0A        return version.length() %3E 0;%0A    %7D%0A%0A
     publ
@@ -1280,25 +1280,24 @@
 (bean);%0A
-%0A
         
 // Get D
@@ -1292,112 +1292,63 @@
     
-// Get 
 Device
- 
 Info
-rmation%0A        final CountDownLatch deviceInfoLatch = new CountDownLatch(1
+ info = getDeviceInformation(bean
 );%0A
+%0A
         
 bean
@@ -1347,136 +1347,130 @@
     
-bean.readDeviceInfo(new Callback%3CDeviceInfo%3E() %7B%0A            @Override%0A            public void onResult(DeviceInfo deviceInfo) %7B
+if (!validHardwareVersion(info.hardwareVersion())) %7B%0A            fail(%22Unexpected HW version: %22 + info.hardwareVersion());
 %0A   
@@ -1470,32 +1470,34 @@
 ));%0A        
+%7D%0A
         
 assertThat(d
@@ -1488,87 +1488,129 @@
     
-assertThat(deviceInfo).isNotNull();%0A                deviceInfoLatch.countDow
+if (!validFirmwareVersion(info.firmwareVersion())) %7B%0A            fail(%22Unexpected FW version: %22 + info.firmwareVersio
 n()
+)
 ;%0A  
@@ -1607,36 +1607,32 @@
 ion());%0A        
- 
-   
 %7D%0A        %7D);%0A  
@@ -1629,119 +1629,141 @@
     
-%7D);%0A        deviceInfoLatch.await();%0A%0A        // Always disconnect at end of test so that other tests will pass
+if (!validSoftwareVersion(info.softwareVersion())) %7B%0A            fail(%22Unexpected SW version: %22 + info.softwareVersion());%0A        %7D%0A
 %0A   

improve Device Information test