package xiedong.systemdairy;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

/**
 * @author jinyan
 * 
 */
public class lookDairy extends ListActivity {
	private static final int ACTIVITY_CREATE = 0;
	private static final int ACTIVITY_EDIT = 1;

	private static final int INSERT_ID = Menu.FIRST;
	private static final int DELETE_ID = Menu.FIRST + 1;

	private DiaryDbHelper mDbHelper;
	private Cursor mDiaryCursor;
	// int x;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list_diary);
		setTitle("日记查看  操作提示:直接点击日记列表");
		mDbHelper = new DiaryDbHelper(this);
		mDbHelper.open();
		renderListView();

	}

	private void renderListView() {
		mDiaryCursor = mDbHelper.getAllNotes();
		startManagingCursor(mDiaryCursor);
		String[] from = new String[]{DiaryDbHelper.KEY_TITLE,
				DiaryDbHelper.KEY_CREATED};
		int[] to = new int[]{R.id.text1, R.id.created};
		SimpleCursorAdapter notes = new SimpleCursorAdapter(this,
				R.layout.lookdairy, mDiaryCursor, from, to);
		setListAdapter(notes);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		menu.add(0, INSERT_ID, 0, "新建日记");
		menu.add(0, DELETE_ID, 0, "返回Menu");
		return true;
	}

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		switch (item.getItemId()) {
			case INSERT_ID :
				createDiary();
				return true;
			case DELETE_ID :
				Intent intent = new Intent(this, newMenu.class);
				startActivity(intent);
		}
		return super.onMenuItemSelected(featureId, item);
	}

	private void createDiary() {
		Intent i = new Intent(this, createDairy.class);
		startActivityForResult(i, ACTIVITY_CREATE);
	}

	// @Override
	// 需要对position和id进行一个很好的区分
	// position指的是点击的这个ViewItem在当前ListView中的位置
	// 每一个和ViewItem绑定的数据,肯定都有一个id,通过这个id可以找到那条数据。
	protected void onListItemClick(ListView l, View v, final int position,
			final long id) {
		super.onListItemClick(l, v, position, id);
		AlertDialog.Builder builder = new AlertDialog.Builder(lookDairy.this);
		builder.setMessage("你选择的是?");
		builder.setTitle("欢迎");
		builder.setPositiveButton("修改", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				Cursor c = mDiaryCursor;
				c.moveToPosition(position);
				Intent i = new Intent();
				i.setClass(lookDairy.this, createDairy.class);
				i.putExtra(DiaryDbHelper.KEY_ROWID, id);
				i.putExtra(DiaryDbHelper.KEY_TITLE, c.getString(c
						.getColumnIndexOrThrow(DiaryDbHelper.KEY_TITLE)));
				i.putExtra(DiaryDbHelper.KEY_BODY, c.getString(c
						.getColumnIndexOrThrow(DiaryDbHelper.KEY_BODY)));
				startActivityForResult(i, ACTIVITY_EDIT);
				dialog.dismiss();

			}

			// @Override
			// public void onClick(DialogInterface dialog, int which) {
			// // TODO Auto-generated method stub
			//
			// }
		});
		builder.setNegativeButton("删除",
				new android.content.DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						mDbHelper.deleteDiary(getListView()
								.getItemIdAtPosition(position));
						renderListView();
						dialog.dismiss();

					}
				});

		builder.create().show();

		// show(x);//弹出对话框
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode,
			Intent intent) {
		super.onActivityResult(requestCode, resultCode, intent);
		renderListView();
	}
//	@Override
//	protected void onDestroy() {
//		finish();
//		// TODO Auto-generated method stub
//		super.onDestroy();
//	}
}