package it.andoma.legocarcontroller;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

import java.util.Set;
import java.util.UUID;

public class MainActivity extends AppCompatActivity {

    private BluetoothAdapter btAdapter;
    private BluetoothDevice btServer;

    private static final String BT_SERVER_NAME = "HC-05";
    private static final String APP_UUID = "00001101-0000-1000-8000-00805F9B34FB";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new DrawView(this));

        btAdapter = BluetoothAdapter.getDefaultAdapter();

        Set<BluetoothDevice> pairedDevices = btAdapter.getBondedDevices();

        if(pairedDevices.size() > 0) {
            for(BluetoothDevice device : pairedDevices) {
                if(device.getName().equals(BT_SERVER_NAME)) {
                    btServer = device;
                    Log.d("legocar", "arduino found");
                }
            }
        }
    }

    @Override
    protected void onStart() {
        super.onStart();

        new ConnectionTask(btServer, UUID.fromString(APP_UUID)).execute();
    }

    public void send(String command) {
        Log.d("legocar", "inviando: " + command);
        String msg = command;
        msg += "\n";
        ConnectionManager.getInstance().write(msg.getBytes());
    }

}
