package com.view;

import java.util.Calendar;

import basicClass.Location;
import basicClass.User;

import net.HttpActionManager;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.TimePicker;

public class AddEvent extends Activity{
	EditText et_eventName;
	EditText et_eventPlace;
	EditText et_detail;
	Button bt_snap;
	Button bt_return;
	Button bt_browse;
	Button bt_publish;
	Button bt_dlt_photo;
	
	Button bt_sd,bt_st,bt_ed,bt_et;
	
	TextView tv_sdt,tv_edt;
	
	boolean parti_status;//true for public
	String event_name=new String();	//cannot be null
	String event_addr=new String();	//cannot be null
	String event_detail="";
	
	private int syear,smonth,sday,shr,smin;
	private int eyear,emonth,eday,ehr,emin;
	private int cyear,cmonth,cday,chr,cmin;
	
	static final int START_DATE_DIALOG_ID=0;
	final static int END_DATE_DIALOG_ID=1; 
	static final int START_TIME_DIALOG_ID=2;
	static final int END_TIME_DIALOG_ID=3;
	
	private HttpActionManager mManager=new HttpActionManager();
	
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.addevent);
		
		et_eventName=(EditText)findViewById(R.id.event_name_in_add);
		et_eventPlace=(EditText)findViewById(R.id.event_place_in_add);
		this.et_detail=(EditText)findViewById(R.id.detail_in_add);
		
		bt_snap=(Button)findViewById(R.id.take_photo_in_add);
		bt_return=(Button)findViewById(R.id.return_in_add);
		bt_browse=(Button)findViewById(R.id.browse_in_add);
		bt_publish=(Button)findViewById(R.id.publish_in_add);
		bt_publish.setOnClickListener(click_publish);
		bt_dlt_photo=(Button)findViewById(R.id.delete_photo_in_add);
		
		bt_sd=(Button)findViewById(R.id.get_sdate_in_add);
		bt_st=(Button)findViewById(R.id.get_stime_in_add);
		bt_ed=(Button)findViewById(R.id.get_edate_in_add);
		bt_et=(Button)findViewById(R.id.get_etime_in_add);
		
		tv_sdt=(TextView)findViewById(R.id.sdt_in_add);
		tv_edt=(TextView)findViewById(R.id.edt_in_add);
		
		bt_snap.setOnClickListener(click_snap);
		
		
		final Calendar c=Calendar.getInstance();
		cyear=c.get(Calendar.YEAR);
		cmonth=c.get(Calendar.MONTH);
		cday=c.get(Calendar.DAY_OF_MONTH);
		chr=c.get(Calendar.HOUR_OF_DAY);
		cmin=c.get(Calendar.MINUTE);
		
		bt_sd.setOnClickListener(new OnClickListener()
		{
			public void onClick(View view)
			{
				showDialog(AddEvent.START_DATE_DIALOG_ID);
			}
		});
		bt_st.setOnClickListener(new OnClickListener()
		{
			public void onClick(View view)
			{
				showDialog(AddEvent.START_TIME_DIALOG_ID);
			}
		});
		bt_ed.setOnClickListener(new OnClickListener()
		{
			public void onClick(View view)
			{
				showDialog(AddEvent.END_DATE_DIALOG_ID);
			}
		});
		bt_et.setOnClickListener(new OnClickListener()
		{
			public void onClick(View view)
			{
				showDialog(AddEvent.END_TIME_DIALOG_ID);
			}
		});
	}

	public String getStartTime()
	{
		String time=tv_sdt.getText().toString();
		return time;
			
	}
	public String getEndTime()
	{
		String time=tv_edt.getText().toString();
		return time;
	}
	
	public void onClick(View view)
	{
		switch(view.getId())
		{
		case R.id.take_photo_in_add:
		{
			
		}
		break;
		case R.id.return_in_add:
		{
			
		}
		break;
		case R.id.browse_in_add:
		{
			
		}
		break;
		default:break;
		}
	}
	/*
	 * Methods to get corresponding event attributes. Not including getting 
	 * text from EditTexts, which is done by listeners. 
	 */
	public String getEventName()
	{
		return event_name;
	}
	public String getEventAddr()
	{
		return event_addr;
	}
	public String getEventDetail()
	{
		return event_detail;
	}
	
	OnClickListener click_snap=new OnClickListener()
	{
		public void onClick(View view)
		{
			Intent i=new Intent(AddEvent.this,CameraView.class);
			startActivity(i);
		}
	};
	
	protected Dialog onCreateDialog(int id)
	{
		switch(id)
		{
		case START_DATE_DIALOG_ID:
			return new DatePickerDialog(this,start_set,syear,smonth,sday);

		case END_DATE_DIALOG_ID:
			return new DatePickerDialog(this,end_set,eyear,emonth,eday);
		case START_TIME_DIALOG_ID:
			return new TimePickerDialog(this,start_tset,shr,smin,false);
		case END_TIME_DIALOG_ID:
			return new TimePickerDialog(this,end_tset,ehr,emin,false);
			
		}
		return null;
	}
	
	private DatePickerDialog.OnDateSetListener start_set=new DatePickerDialog.OnDateSetListener()
	{
		public void onDateSet(DatePicker view,int year,int month,int day)
		{
			syear=year;
			smonth=month;
			sday=day;
			updateDisplay(new String(syear+"-"+smonth+"-"+sday),0);
		}
	};
	private DatePickerDialog.OnDateSetListener end_set=new DatePickerDialog.OnDateSetListener()
	{
		public void onDateSet(DatePicker view,int year,int month,int day)
		{
			eyear=year;
			emonth=month;
			eday=day;
			updateDisplay(new String(eyear+"-"+emonth+"-"+eday),1);
		}
	};
	
	private TimePickerDialog.OnTimeSetListener start_tset=new TimePickerDialog.OnTimeSetListener()
	{
		public void onTimeSet(TimePicker view,int hr,int min)
		{
			shr=hr;
			smin=min;
			updateDisplay(new String(" "+shr+":"+smin),0);
		}
	};
	
	private TimePickerDialog.OnTimeSetListener end_tset=new TimePickerDialog.OnTimeSetListener()
	{
		public void onTimeSet(TimePicker view,int hr,int min)
		{
			ehr=hr;
			emin=min;
			updateDisplay(new String(" "+ehr+":"+emin),1);
		}
	};

	protected void updateDisplay(String added,int mode) {
		// TODO Auto-generated method stub
		
		if(mode==0)
		{
			String tmp=tv_sdt.getText().toString();
			tmp+=added;
			tv_sdt.setText(tmp);
		}
		else if(mode==1)
		{
			String tmp=tv_edt.getText().toString();
			tmp+=added;
			tv_edt.setText(tmp);
		}
		
	}
	
	public OnClickListener click_publish=new OnClickListener()
	{
		public void onClick(View view)
		{
			event_name=et_eventName.getText().toString();
			event_addr=et_eventPlace.getText().toString();
			event_detail=et_detail.getText().toString();
			String start_time=syear+""+smonth+""+sday+""+shr+""+smin;
			String end_time=eyear+""+emonth+""+eday+""+ehr+""+emin;
			Location loc=new Location();
			loc.setLocation(event_addr);
			
			//mManager.addEvent(event_name, new User(), loc, start_time, end_time, event_detail);
			
			AddVideo.requestUpdate(AddEvent.this);
		}
	};
}
