package com.neofect.communicator.sample.dummy;

import android.util.Log;

import com.neofect.communicator.dummy.DummyConnection;
import com.neofect.communicator.dummy.DummyPhysicalDevice;

import static com.neofect.communicator.util.ByteArrayConverter.byteArrayToHex;

/**
 * Created by Neo on 2017. 11. 28..
 */

public class DummySimpleRemote extends DummyPhysicalDevice {

	private static final String LOG_TAG = "DummySimpleRemote";

	private Thread senderThread;

	private int lastPressedButton = 0;
	private long elapsedTimeForButtonPressed = 0;
	private long elapsedTimeForLowBatteryAlert = 0;
	private long endTimeForBeep = 0;

	public DummySimpleRemote(String deviceIdentifier, String deviceName) {
		super(deviceIdentifier, deviceName);
	}

	@Override
	protected void connect(final DummyConnection connection) {
		Log.d(LOG_TAG, "connect:");
		this.connection = connection;

		notifyConnected();
		startSenderThread();
	}

	@Override
	protected void disconnect() {
		Log.d(LOG_TAG, "disconnect:");
		stopSenderThread();
	}

	@Override
	protected void receive(byte[] data) {
		String message = byteArrayToHex(data);
		Log.d(LOG_TAG, "receive: Physical device received data. [" + message + "]");

		try {
			if (message.startsWith("9d 03")) {
				int timeDuration = data[2] << 8 | data[3] & 0x000000ff;
				startBeep(timeDuration);
			} else {
				Log.e(LOG_TAG, "receive: Undefined message!");
			}
		} catch (Exception e) {
			Log.e(LOG_TAG, "receive:", e);
		}
	}

	private void sendData(long elapsedTime) {
		elapsedTimeForButtonPressed += elapsedTime;
		elapsedTimeForLowBatteryAlert += elapsedTime;

		while (elapsedTimeForButtonPressed > 1000) {
			elapsedTimeForButtonPressed -= 1000;
			sendButtonPressedMessage(lastPressedButton);
			lastPressedButton = (lastPressedButton + 1) % 4;
		}

		while (elapsedTimeForLowBatteryAlert > 3000) {
			elapsedTimeForLowBatteryAlert -= 3000;
			sendLowBatteryAlertMessage();
		}
	}

	private void sendButtonPressedMessage(int lastPressedButton) {
		byte[] message = new byte[3];
		message[0] = (byte) 0x9d;
		message[1] = (byte) 0x01;
		message[2] = (byte) lastPressedButton;
		notifyRead(message);
	}

	private void sendLowBatteryAlertMessage() {
		byte[] message = new byte[2];
		message[0] = (byte) 0x9d;
		message[1] = (byte) 0x02;
		notifyRead(message);
	}

	private void startBeep(int timeDuration) {
		Log.i(LOG_TAG, "startBeep: timeDuration=" + timeDuration);
		if (endTimeForBeep != 0) {
			Log.i(LOG_TAG, "Start beep sound.");
		}
		endTimeForBeep = System.currentTimeMillis() + (timeDuration * 1000);
	}

	private void stopSenderThread() {
		if (senderThread != null) {
			senderThread.interrupt();
			senderThread = null;
		}
	}

	private void startSenderThread() {
		stopSenderThread();
		senderThread = new Thread(() -> {
			long timestamp = System.currentTimeMillis();
			while (true) {
				try {
					long now = System.currentTimeMillis();
					if (endTimeForBeep != 0) {
						if (now > endTimeForBeep) {
							Log.i(LOG_TAG, "Stop beep sound.");
							endTimeForBeep = 0;
						} else {
							Log.i(LOG_TAG, "BEEP!");
						}
					}
					sendData(now - timestamp);
					timestamp = now;
					Thread.sleep(100);
				} catch (InterruptedException e) {
					break;
				} catch (Exception e) {
					Log.e(LOG_TAG, "", e);
				}
			}
		});
		senderThread.start();
	}

}

@@ -71,63 +71,8 @@
 g;%0A%0A
-import com.neofect.communicator.dummy.DummyConnection;%0A
 impo
@@ -705,62 +705,56 @@
 e%0A%09p
-rotected void connect(final DummyConnection connection
+ublic void start(final TransferDelegate delegate
 ) %7B%0A
@@ -771,30 +771,28 @@
 G_TAG, %22
-connec
+star
 t:%22);%0A%09%09
 this.con
@@ -787,57 +787,28 @@
 ;%0A%09%09
-this.connection = connection;%0A%0A%09%09notifyConnected(
+super.start(delegate
 );%0A%09
@@ -850,32 +850,23 @@
 e%0A%09p
-rotected void disconnect
+ublic void stop
 () %7B
@@ -888,18 +888,12 @@
 G, %22
-disconnect
+stop
 :%22);
@@ -936,29 +936,22 @@
 e%0A%09p
-rotected void receive
+ublic void put
 (byt
@@ -1021,23 +1021,19 @@
 G_TAG, %22
-receive
+put
 : Physic
@@ -2003,33 +2003,36 @@
 edButton;%0A%09%09
-notifyRea
+delegate.sen
 d(message);%0A
@@ -2174,17 +2174,20 @@
 ;%0A%09%09
-notifyRea
+delegate.sen
 d(me

Fix build error