package xiedong.systemdairy;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class SystemDairyActivity extends Activity {
	private EditText field_name = null;
	private EditText filed_pass = null;
	
	
	
	/**使用SharedPreferences 来储存与读取数据**/ 
	SharedPreferences mShared = null; 
	/**程序中可以同时存在多个SharedPreferences数据, 根据SharedPreferences的名称就可以拿到对象**/ 
	public final static String SHARED_MAIN = "main"; 
	/**SharedPreferences中储存数据的Key名称**/ 
	public final static String KEY_REGISTERNAME = "registername"; 
	public final static String KEY_REGISTERPASSWORD = "registerpassword"; 

	/**SharedPreferences中储存数据的路径**/ 
	public final static String DATA_URL = "/data/data/"; 
	public final static String SHARED_MAIN_XML = "main.xml"; 

	/*
	 * public static final String xiedong_password = "xiedong_password";
	 * 
	 * public static final String PASSWORD = "PASSWORD"; private static final
	 * String NAME = "NAME";
	 */

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		setTitle("谢栋日记系统");
		field_name = (EditText) findViewById(R.id.etUid);
		filed_pass = (EditText) findViewById(R.id.etPwd);
		
		
		
		Button button;
        Button xiedongregister;
		/*
		 * SharedPreferences settings = getSharedPreferences(xiedong_password,
		 * 0); settings.edit() .putString(NAME, "xiedong") .putString(PASSWORD,
		 * "198701") .commit();
		 */
		// String name = settings.getString(NAME, "xiedong");

		// String password = settings.getString(PASSWORD, "198701");
		// Set value

		// field_name.setText(name);

		// filed_pass.setText(password);

		button = (Button) findViewById(R.id.xiedong);
		xiedongregister=(Button)findViewById(R.id.xiemo);
		button.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				// SharedPreferences settings =
				// getSharedPreferences(xiedong_password, 0);
				mShared = getSharedPreferences(SHARED_MAIN, Context.MODE_PRIVATE); 
				String registername1 = mShared.getString(KEY_REGISTERNAME, ""); 
				String registerpassword1= mShared.getString(KEY_REGISTERPASSWORD, "");
				// String xiedong_name=settings.getString(NAME, "");
				String count = field_name.getText().toString();
				String pass = filed_pass.getText().toString();
				if ("".equals(count) || "".equals(pass)) {
					Toast.makeText(SystemDairyActivity.this,
							"^_^账户密码不能为空  请你注册^_^", 5).show();
				} else if(registername1.equals(count)&&registerpassword1.equals(pass)){ 
					Toast.makeText(SystemDairyActivity.this,
							"^_^账户信息验证成功!加载数据中... ...^_^", 10).show();
					new Thread()
			         {public void run()
			         {try {sleep(2000);     //等待三秒,自动进入软件主窗口   
			         Intent intent = new Intent();
			         intent.setClass(SystemDairyActivity.this, newMenu.class);   
			         startActivity(intent);        }
			         catch (Exception e) {
			         e.printStackTrace();
			         }
			         //progressDialog.dismiss();
			         
			         
			         }       
			         }.start();
//					Intent intent;
//					intent = new Intent(SystemDairyActivity.this, newMenu.class);
//					startActivity(intent);
//			         Toast.makeText(SystemDairyActivity.this,
//								"^_^加载数据成功^_^",1).show();
				}else{
					Toast.makeText(SystemDairyActivity.this,
							"^_^密码错误 请查看帮助^_^", 5).show();
				}
			}
		});
		xiedongregister.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent=new Intent();
				intent.setClass(SystemDairyActivity.this, Register.class);
				startActivity(intent);
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		menu.add(0, 1, 0, "帮助");
		menu.add(0, 2, 0, "关于");
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		Intent intent;
		switch (item.getItemId()) {
			case 1 :
				//
				intent = new Intent(this, help.class);
				startActivity(intent);
				break;

			case 2 :

				intent = new Intent(this, about.class);
				startActivity(intent);
				break;

		}
		return super.onOptionsItemSelected(item);
	}
	protected void dialog() {

		AlertDialog.Builder builder = new Builder(SystemDairyActivity.this);

		builder.setMessage("确定要退出吗?");

		builder.setTitle("提示");

		builder.setPositiveButton("确认",

		new android.content.DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				android.os.Process.killProcess(android.os.Process.myPid());
				dialog.dismiss();
				System.exit(0);
//				SystemDairyActivity.this.finish();

			}

		});

		builder.setNegativeButton("取消",

		new android.content.DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {

				dialog.dismiss();

			}

		});

		builder.create().show();

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {

		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {

			dialog();

			return false;

		}

		return false;

	}

}