package com.example.client;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity extends Activity implements OnClickListener {
	
	EditText use,password=null;
	Button login,register=null;
	
	SqlHelper db=null;//数据库帮助类，相当于定义了各种各样的类，全局变量
	SQLiteDatabase sDatabase = null;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        
        Button back = (Button)findViewById(R.id.login_reback_btn);
        back.setOnClickListener(this);
        
        Button loginButton = (Button)findViewById(R.id.login_login_btn);
        loginButton.setOnClickListener(this);
        
        use = (EditText) findViewById(R.id.login_user_edit);
        password = (EditText) findViewById(R.id.login_passwd_edit);
        
        SqlHelper db = new SqlHelper(getApplicationContext(), "store.db", null, 1);
     	sDatabase = db.getWritableDatabase();
	}
	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		int id = v.getId();
		
		if(id == R.id.login_reback_btn){ 
			 Intent intent = new Intent(LoginActivity.this, MainActivity.class);			 
			 startActivity(intent);
		}
		
		//获得用户名
		String i = use.getText().toString();	
		
		//登陆
		if (id == R.id.login_login_btn) {	
			String userName = "";
			String userPw = "";
			String realname = "";
			String age = "";
			String sex = "";
	
			//编写数据库语句
			String select_sql = "select username,password,realname,age,sex from user_info where username = '"
					+ i + "'";
			//执行语句
			Cursor cursor = sDatabase.rawQuery(select_sql, null);
			
			//返回一个布尔值，判断一个游标是否为空
			cursor.moveToFirst();
			
			// 将从数据中取出的用户名和密码赋值给两个字符串变量
			try {
				userName = cursor.getString(0);
				userPw = cursor.getString(1);
				realname = cursor.getString(2);
				age = cursor.getString(3);
				sex = cursor.getString(4);
			} catch (Exception e) {
				// TODO: handle exception
				userName = "";
				userPw = "";
			}
			
			// 判断用户名是否为空,if是很自然的
			if (i.equals("")) {
				Toast.makeText(this, "用户名不能为空！", Toast.LENGTH_SHORT).show();
			}
			// 判断密码是否为空
			else if (password.getText().toString().equals("")) {
				Toast.makeText(this, "密码不能为空！", Toast.LENGTH_SHORT).show();
			} 	
			// 判断用户名和密码是否正确
			else if (!(use.getText().toString().equals(userName) && password
					.getText().toString().equals(userPw))) {
				Toast.makeText(this, "用户名或密码错误，请重新输入！", Toast.LENGTH_SHORT).show();
			}
			// 全部正确跳转到操作界面
			else {
			    // 已登录
			    UserInfo.getInstance().setIsLogin(true);
			    
			    // 保存用户信息
			    UserInfo.getInstance().setUsername(userName);
			    UserInfo.getInstance().setRealname(realname);
			    UserInfo.getInstance().setAge(age);
			    UserInfo.getInstance().setSex(sex);			    
				
			    cursor.close();
				Intent intent = new Intent(LoginActivity.this, EvaluateActivity.class);			
				startActivity(intent);	
				
				Toast.makeText(LoginActivity.this, "登陆成功!", Toast.LENGTH_LONG).show();
			}
		}
	}
}
