package com.example.newdemo;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.newdemo.util.MyToast;

public class LoginActivity extends Activity implements OnClickListener {
	private EditText tel_et;
	private EditText pswd_et;
	private Button login_bt;
	private CheckBox remeber_pswd;
	
	private SharedPreferences pref;
	private SharedPreferences.Editor editor;
	private String userName, password;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.login);
		
		initModle();
		initView();
	}

	private void initModle() {
		pref = PreferenceManager.getDefaultSharedPreferences(this);
	}

	private void initView() {
		RelativeLayout titleBar = (RelativeLayout)findViewById(R.id.layout_titlebar);
		((TextView)titleBar.findViewById(R.id.tv_titlebar_title)).setVisibility(View.GONE);
		Button rightBtn = (Button)titleBar.findViewById(R.id.btn_titlebar_right);
		rightBtn.setText("注册");
		rightBtn.setOnClickListener(this);
		((Button)titleBar.findViewById(R.id.btn_titlebar_back)).setVisibility(View.GONE);
		
		login_bt = (Button)findViewById(R.id.login_bt);
		login_bt.setOnClickListener(this);
		tel_et = (EditText)findViewById(R.id.tel_et);
		pswd_et = (EditText)findViewById(R.id.pswd_et);
		remeber_pswd = (CheckBox)findViewById(R.id.remeber_pswd);
		boolean isRemeber = pref.getBoolean("remeberPswd", false);
		if (isRemeber) {
			userName = pref.getString("userName", "");
			password = pref.getString("password", "");
			tel_et.setText(userName);
			pswd_et.setText(password);
			remeber_pswd.setChecked(true);
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.login_bt:
			String telephone = tel_et.getText().toString();
			String password = pswd_et.getText().toString();
			if (telephone.equals("17000000000") && password.equals("javaapk")) {
				editor = pref.edit();
				if (remeber_pswd.isChecked()) {
					editor.putBoolean("remeberPswd", true);
					editor.putString("userName", telephone);
					editor.putString("password", password);
				} else {
					editor.clear();
				}
				editor.commit();
				Intent intent = new Intent(LoginActivity.this, MapActivity.class);
				startActivity(intent);
				LoginActivity.this.finish();
			} else {
				MyToast.showToast(LoginActivity.this, "请输入正确的手机号或密码");
			}
			break;
		case R.id.btn_titlebar_right:
			Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
			startActivity(intent);
			LoginActivity.this.finish();
			break;
		default:
			break;
		}
	}
}
