package de.tudarmstadt.informatik.hostage.ui;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Random;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
import de.tudarmstadt.informatik.hostage.R;
import de.tudarmstadt.informatik.hostage.logging.UglyDbHelper;
import de.tudarmstadt.informatik.hostage.sync.BluetoothSync;
import de.tudarmstadt.informatik.hostage.sync.NFCSync;

public class PlayGroundActivity extends Activity {

	BluetoothSync bs;

	public void createNetworkData(View view) {
		Random rnd = new Random();
		ArrayList<HashMap<String, Object>> fakeNetInfo = new ArrayList<HashMap<String, Object>>();
		for (int i = 0; i < 25; i++) {
			HashMap<String, Object> network = new HashMap<String, Object>();
			network.put(UglyDbHelper.KEY_BSSID, createRandomBSSID());
			network.put(UglyDbHelper.KEY_SSID,
					new BigInteger(130, rnd).toString(32));
			network.put(UglyDbHelper.KEY_LATITUDE, rnd.nextDouble() * 360);
			network.put(UglyDbHelper.KEY_LONGITUDE, rnd.nextDouble() * 360);
			network.put(UglyDbHelper.KEY_ACCURACY, rnd.nextFloat());
			network.put(UglyDbHelper.KEY_TIME, System.currentTimeMillis());
			fakeNetInfo.add(network);
		}

		setNetworkInfoText();
		new UglyDbHelper(this).updateNetworkInformation(fakeNetInfo);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	public void startNFC(View view) {
		startActivity(new Intent(this, NFCSync.class));
	}

	public void syncData(View view) {
		bs.syncData();
	}

	private String createRandomBSSID() {
		Random rnd = new Random();
		char[] symbols = new char[16];
		for (int idx = 0; idx < 10; ++idx)
			symbols[idx] = (char) ('0' + idx);
		for (int idx = 10; idx < 16; ++idx)
			symbols[idx] = (char) ('a' + idx - 10);

		char[] buf = new char[17];
		for (int i = 0; i < 18; i += 3) {
			buf[i] = symbols[rnd.nextInt(symbols.length)];
			buf[i + 1] = symbols[rnd.nextInt(symbols.length)];
			if (i < 15) {
				buf[i + 2] = ':';
			}
		}
		return new String(buf);
	}

	private void setNetworkInfoText() {
		UglyDbHelper dbh = new UglyDbHelper(this);
		TextView bssids = (TextView) findViewById(R.id.textView1);
		String text = "";
		ArrayList<HashMap<String, Object>> netInfo = dbh
				.getNetworkInformation();
		for (HashMap<String, Object> network : netInfo) {
			text = text + (String) network.get(UglyDbHelper.KEY_BSSID) + "\n"
					+ (String) network.get(UglyDbHelper.KEY_SSID) + "\n"
					+ network.get(UglyDbHelper.KEY_LATITUDE) + "\n"
					+ network.get(UglyDbHelper.KEY_LONGITUDE) + "\n"
					+ network.get(UglyDbHelper.KEY_ACCURACY) + "\n"
					+ network.get(UglyDbHelper.KEY_TIME) + "\n\n";
		}
		bssids.setText(text);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_playground);
		bs = new BluetoothSync(this);
		setNetworkInfoText();
	}
}