package com.szu;

import java.util.Random;

import com.szu.accountsoft.dao.PwdDAO;
import com.szu.accountsoft.model.Tb_pwd;

import android.R.integer;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class Calculate_Activity extends Activity {
	private TextView num1;
	private TextView num2;
	private TextView oper;
	private EditText ans;
	private Button yes;
	private Button calculator;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.calculate);
		
		findView();
		
		final PwdDAO pwdDAO = new PwdDAO(Calculate_Activity.this);// 创建PwdDAO对象
		
		
		
		setTitle("请输入验证码");
		ans.setText("0");
		Random random1 = new Random();
		Random random2 = new Random();
		Random operator = new Random();
		
		final int i = operator.nextInt(4);
		final int val1 = random1.nextInt(100);
		final int val2 = random2.nextInt(100);

		
		switch (i) {
		case 0:
			oper.setText("+");
			break;
		case 1:
			oper.setText("-");
			break;
		case 2:
			oper.setText("×");
			break;
		case 3:
			oper.setText("÷");
			break;
		default:
			break;
		}
		
		ans.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				ans.setText("");
			}
		});
				
		num1.setText(val1+"");
		num2.setText(val2+"");
		
		calculator.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(Calculate_Activity.this, Calculator.class);
				startActivity(intent);
			}
		});
		
		
		yes.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				boolean flag = false;

				
				if(ans.getText().toString().trim().equals(""))
				{
					Toast.makeText(Calculate_Activity.this, "答案不能为空", Toast.LENGTH_SHORT).show();
				}
				else
				{
					switch (i) {
					case 0:
						flag = add(val1, val2);
						break;
					case 1:
						flag = sub(val1, val2);
						break;
					case 2:
						flag = mul(val1, val2);
						break;
					case 3:
						flag = div(val1, val2);
						break;
					default:
						break;
					}
					if(flag)
					{
						Intent intent = getIntent();
						String password = intent.getStringExtra("password").toString();
						Tb_pwd tb_pwd = new Tb_pwd(password);// 根据输入的密码创建Tb_pwd对象
						pwdDAO.add(tb_pwd);// 添加用户密码
						Toast.makeText(Calculate_Activity.this, "答案正确！恭喜您，注册成功！", Toast.LENGTH_SHORT).show();
						
						intent.setClass(Calculate_Activity.this, MainActivity.class);
						startActivity(intent);
						finish();
					
					}
					else
					{
						Toast.makeText(Calculate_Activity.this, "答案错误！对不起！注册失败！", Toast.LENGTH_SHORT).show();
						finish();
						Intent intent = new Intent(Calculate_Activity.this, Calculate_Activity.class);
						startActivity(intent);
						
					}
				}
			}
		});
	}

	private boolean div(int val1, int val2) {
		// TODO Auto-generated method stub
		
		double ans_dou = (double)val1/val2;
		String ans_String = ans.getText().toString().trim();
		Double double_ans = Double.parseDouble(ans_String);
		if(Math.abs(double_ans -  ans_dou)<1e-2)
		{
			return true;
		}
		else
			return false;
		
		
	}
	
		private boolean mul(int val1, int val2) {
		// TODO Auto-generated method stub
		
		int ans_int = val1*val2;
		String ans_String = ans.getText().toString().trim();
		Integer integer_ans = Integer.parseInt(ans_String);
		if(ans_int == integer_ans)
		{
			return true;
		}
		else
			return false;
		
		
	}
	

	private boolean add(int val1, int val2) {
		// TODO Auto-generated method stub
		int ans_int = val1+val2;
		System.out.println("val1+val2："+ans_int);
		String ans_String = ans.getText().toString().trim();
		
		
		Integer integer_ans = Integer.parseInt(ans_String);
		System.out.println("我的答案："+integer_ans);
		
		if(ans_int == integer_ans)
		{
			return true;
		}
		else
			return false;
		
	}
	
		private boolean sub(int val1, int val2) {
		// TODO Auto-generated method stub
		int ans_int = val1-val2;
		String ans_String = ans.getText().toString().trim();
		Integer integer_ans = Integer.parseInt(ans_String);
		if(ans_int == integer_ans)
		{
			return true;
		}
		else
			return false;
		
	}

	private void findView() {
		// TODO Auto-generated method stub
		num1 = (TextView) findViewById(R.id.num1);
		num2 = (TextView) findViewById(R.id.num2);
		oper = (TextView) findViewById(R.id.oper);
		ans = (EditText) findViewById(R.id.ans);
		yes = (Button) findViewById(R.id.yes);
		calculator = (Button) findViewById(R.id.calculator);
		
	}

}
