package it.unipr.dsg.mobdev.logger;

import it.unipr.dsg.mobdev.logger.storage.LogDescriptor;
import it.unipr.dsg.mobdev.logger.storage.LogDescriptorDataSource;

import java.util.List;

import biz.guglielmo.logger.R;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

public class LoggerAppActivity extends ListActivity {

	public static String TAG = "ADSource5b";

	private LogDescriptorDataSource datasource = null;

	private List<LogDescriptor> storedLogs = null;

	private Context mContext = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		this.mContext = this;

		Button addLogButton = (Button)findViewById(R.id.addLogButton);
		addLogButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if(datasource != null)
				{
					Log.d(TAG, "Adding New Log ...");

					//Create an example log record 
					LogDescriptor logDescr = new LogDescriptor(System.currentTimeMillis(),1.0, 1.0,"TEST","test");

					if(datasource.createLogDescriptor(logDescr) != null)
						Toast.makeText(mContext, "New Log Correctly Added !", Toast.LENGTH_LONG).show();
					else
						Toast.makeText(mContext, "ERROR Adding new Log !", Toast.LENGTH_LONG).show();

					Log.d(TAG, "Log Count: " + datasource.getLogCount());
				}
			}
		});

		Button viewLogButton = (Button)findViewById(R.id.viewLogButton);
		viewLogButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				if(datasource != null)
				{
					Log.d(TAG, "View Logs ...");

					storedLogs = datasource.getAllLogsDescriptor();

					Log.d(TAG, "Stored Logs: " + storedLogs.size());

					updateLogListView(storedLogs);		
				}
			}
		});

		if(datasource == null)
		{
			datasource = new LogDescriptorDataSource(this);
			datasource.open();
		}
	}

	@Override
	protected void onResume() {
		datasource.open();
		super.onResume();
	}

	@Override
	protected void onPause() {
		datasource.close();
		super.onPause();
	}

	@Override  
	protected void onListItemClick(ListView l, View v, int position, long id) {  

		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle("Selected Log:")
		.setMessage(storedLogs.get(position).toString())
		.setCancelable(false)
		.setPositiveButton("Ok",new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int id) {
				dialog.cancel();
			}
		});
		AlertDialog alert = builder.create();
		alert.show();

		super.onListItemClick(l, v, position, id);  
	}  

	public void updateLogListView(List<LogDescriptor> values)
	{
		ArrayAdapter<LogDescriptor> adapter = new ArrayAdapter<LogDescriptor>(this,R.layout.list_item, values);
		setListAdapter(adapter);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}
}