package in.co.sdslabs.cognizance;

import java.io.IOException;

import android.content.Intent;
import android.database.SQLException;
import android.graphics.Color;
import android.graphics.PointF;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class EventActivity extends ActionBarActivity implements OnTouchListener {

	public EventActivity() {
	}
	
	DatabaseHelper myDbHelper;
	Bundle b;
	boolean fav;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.event_details);

		TextView eName = (TextView) findViewById(R.id.event_name);
		TextView eOneLiner = (TextView) findViewById(R.id.event_oneliner);
		TextView eDescription = (TextView) findViewById(R.id.event_description);
		TextView eDate = (TextView) findViewById(R.id.event_date);
		TextView eTime = (TextView) findViewById(R.id.event_time);
		TextView eVenue = (TextView) findViewById(R.id.event_venue);
		
		ImageView eventIcon = (ImageView) findViewById(R.id.event_ImView);

		myDbHelper = new DatabaseHelper(this);
		try {
			myDbHelper.createDataBase();
		} catch (IOException ioe) {
			throw new Error("Unable to create database");
		}
		try {
			myDbHelper.openDataBase();
		} catch (SQLException sqle) {
			throw sqle;
		}

		b = getIntent().getExtras();
		
		CheckBox star = (CheckBox)findViewById(R.id.star);
		if(myDbHelper.isFavourite(b.getString("event"))){
			fav = true;
			star.setChecked(true);
		}else {
			fav = false;
			star.setChecked(false);
		}
		
		star.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				
				if(fav){
					myDbHelper.unmarkAsFavourite(b.getString("event"));
				}else{
					myDbHelper.markAsFavourite(b.getString("event"));
				}
			}
		});

		eName.setText(b.getString("event"));
		eOneLiner.setText(myDbHelper.getEventOneLiner(b.getString("event")));
		eDescription.setText(myDbHelper.getEventDescription(b
				.getString("event")));
		int x = myDbHelper.getImageX(b.getString("event"));
		int y = myDbHelper.getImageY(b.getString("event"));
		Log.v("Image", "x :" + x);
		Log.v("Image", "y :" + y);
		
		eventIcon.setImageResource(Drawables.eventsImages[x][y]);
		eDate.setText("DATE : " + myDbHelper.getEventDate(b.getString("event")));
		eTime.setText("TIME : " + myDbHelper.getEventTime(b.getString("event")));
		eVenue.setTextColor(Color.rgb(1,140,149));
		eVenue.setText("VENUE : "+myDbHelper.getVenueDisplay(b.getString("event")));
		
		eVenue.setOnTouchListener(this);
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		
		showZoomedMap(myDbHelper.getVenueMap(b.getString("event")));
		return false;
	}

	private void showZoomedMap(String place) {

		PointF coord = myDbHelper.searchPlaceForCoordinates(place);
		Bundle mapParams = new Bundle();
		mapParams.putInt("mode", 1); // mode = 0 for normal and mode = 1 for
										// zoomed
		mapParams.putFloat("X", (float) coord.x);
		mapParams.putFloat("Y", (float) coord.y);

		Intent i = new Intent(this, in.co.sdslabs.mdg.map.CampusMap.class);
		i.putExtras(mapParams);
		startActivity(i);
		// myDbHelper.close();
	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		super.onBackPressed();
		finish();
	}
	
	

}

@@ -344,24 +344,66 @@
 .view.View;%0A
+import android.view.View.OnClickListener;%0A
 import andro
@@ -632,37 +632,8 @@
 iew;
-%0Aimport android.widget.Toast;
 %0A%0Apu
@@ -696,29 +696,29 @@
 ments On
-Touch
+Click
 Listener
  %7B%0A%0A%09pub
@@ -709,17 +709,16 @@
 Listener
- 
 %7B%0A%0A%09publ
@@ -2974,21 +2974,21 @@
 ue.setOn
-Touch
+Click
 Listener
@@ -3003,158 +3003,8 @@
 %09%7D%0A%0A
-%09@Override%0A%09public boolean onTouch(View v, MotionEvent event) %7B%0A%09%09%0A%09%09showZoomedMap(myDbHelper.getVenueMap(b.getString(%22event%22)));%0A%09%09return false;%0A%09%7D%0A%0A
 %09pri
@@ -3584,16 +3584,171 @@
 sh();%0A%09%7D
+%0A%0A%09@Override%0A%09public void onClick(View v) %7B%0A%09%09// TODO Auto-generated method stub%0A%09%09%0A%09%09%0A%09%09showZoomedMap(myDbHelper.getVenueMap(b.getString(%22event%22)));%0A%09%09%0A%09%7D
 %0A%09%0A%09%0A%0A%7D%0A

Change onTouchListener to OnClickListener