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.preference.PreferenceManager;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
import de.tudarmstadt.informatik.hostage.R;
import de.tudarmstadt.informatik.hostage.commons.HelperUtils;
import de.tudarmstadt.informatik.hostage.logging.NetworkRecord;
import de.tudarmstadt.informatik.hostage.logging.SyncInfoRecord;
import de.tudarmstadt.informatik.hostage.persistence.HostageDBContract;
import de.tudarmstadt.informatik.hostage.persistence.HostageDBOpenHelper;
import de.tudarmstadt.informatik.hostage.sync.bluetooth.BluetoothSyncActivity;
import de.tudarmstadt.informatik.hostage.sync.nfc.NFCSync;
import de.tudarmstadt.informatik.hostage.sync.tracing.TracingSyncActivity;

public class PlayGroundActivity extends Activity {


	public void createNetworkData(View view) {
		Random rnd = new Random();
		ArrayList<NetworkRecord> fakeNetInfo = new ArrayList<NetworkRecord>();
		for (int i = 0; i < 25; i++) {
			NetworkRecord record = new NetworkRecord();
			record.setBssid(createRandomBSSID());
			record.setSsid(new BigInteger(130, rnd).toString(32));
			record.setLatitude(rnd.nextDouble() * 360);
			record.setLongitude(rnd.nextDouble() * 360);
			record.setAccuracy(rnd.nextFloat());
			record.setTimestampLocation(System.currentTimeMillis());
			fakeNetInfo.add(record);
		}

		setNetworkInfoText();
		new HostageDBOpenHelper(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, TracingSyncActivity.class));
	}

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

	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() {
		HostageDBOpenHelper dbh = new HostageDBOpenHelper(this);
		TextView text_view = (TextView) findViewById(R.id.textView1);
		String text = "";
		ArrayList<SyncInfoRecord> syncInfo = dbh.getSyncInfo();
		HashMap<String, Long> syncDevices = dbh.getSyncDevices();

		for (SyncInfoRecord info: syncInfo) {
			text = text + info.getBSSID() + "\n"
					+ info.getDeviceID() + "\n" +
					+ info.getNumber_of_attacks() + "\n"
					+ info.getNumber_of_portscans() + "\n\n";
		}
		
		for(String device: syncDevices.keySet()){
			text = text + device + "\n"
					+ syncDevices.get(device) + "\n\n";
		}
		text_view.setText(text);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_playground);
		setNetworkInfoText();
	}
}