package com.example.demoactivity;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;

public class PinActivity extends Activity
{
	private Button pinButton1;
	private Button modifyButton2;
	private Button newButton3;
	private ProgressDialog dialog;
	private EditText pinEditText2;
	private EditText editText1;
	private EditText modifyEditText3;
	private EditText newEditText4;
	private Handler handler;
	long verify;
	long change;
	long reset;
	String tempverify;
	String tempchange;
	String tempreset;
	int flag;
	public void onCreate(Bundle savedInstanceState) 
	{
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.pin_main);
		pinEditText2=(EditText)findViewById(R.id.pinEditText2);
		editText1=(EditText)findViewById(R.id.editText1);
		modifyEditText3=(EditText)findViewById(R.id.modifyEditText3);
		newEditText4=(EditText)findViewById(R.id.newEditText4);
		
		
		pinButton1=(Button)findViewById(R.id.pinbutton1);
		modifyButton2=(Button)findViewById(R.id.modifyButton2);
		newButton3=(Button)findViewById(R.id.newButton3);
		
		pinButton1.setOnClickListener(listener);
		modifyButton2.setOnClickListener(listener);
		newButton3.setOnClickListener(listener);
		
		handler=new Handler()
		{
			public void handleMessage(Message msg)
			{ 
				if(msg.arg1==0)
				{
					
					if(msg.what==0) 
					{
						dialog.dismiss();
		        		AlertDialog.Builder builder1=new AlertDialog.Builder(PinActivity.this);
						builder1.setMessage(R.string.Verify_success);
						/*builder1.setIcon(R.drawable.warning);
						builder1.setTitle(R.string.in_main_Warning);*/
						builder1.setPositiveButton(R.string.determine, new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int which) {
								dialog.dismiss();
							}
						});
		        		
						AlertDialog dialog=builder1.create();
						dialog.show();
					}
					else
					{
						dialog.dismiss();
		        		AlertDialog.Builder builder1=new AlertDialog.Builder(PinActivity.this);
						builder1.setMessage(R.string.Verify_Failure);
						builder1.setPositiveButton(R.string.determine, new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int which) {
								dialog.dismiss();
							}
						});
		        		
						AlertDialog dialog=builder1.create();
						dialog.show();
					}
				}
				else if(msg.arg1==1)
				{
					if(msg.what==0) 
					{
						dialog.dismiss();
		        		AlertDialog.Builder builder1=new AlertDialog.Builder(PinActivity.this);
						builder1.setMessage(R.string.Modified_success);
						/*builder1.setIcon(R.drawable.warning);
						builder1.setTitle(R.string.title);*/
						builder1.setPositiveButton(R.string.determine, new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int which) {
								dialog.dismiss();
							}
						});
		        		
						AlertDialog dialog=builder1.create();
						dialog.show();
					}
					else
					{
						dialog.dismiss();
		        		AlertDialog.Builder builder1=new AlertDialog.Builder(PinActivity.this);
						builder1.setMessage(R.string.Modified_Failure);
						//builder1.setIcon(R.drawable.warning);
						//builder1.setTitle(R.string.title);
						builder1.setPositiveButton(R.string.determine, new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int which) {
								dialog.dismiss();
							}
						});
		        		
						AlertDialog dialog=builder1.create();
						dialog.show();
					}
				}
				else if(msg.arg1==2)
				{
					if(msg.what==0) 
					{
						dialog.dismiss();
		        		AlertDialog.Builder builder1=new AlertDialog.Builder(PinActivity.this);
						builder1.setMessage(R.string.Reset_success);
						//builder1.setIcon(R.drawable.warning);
						//builder1.setTitle(R.string.title);
						builder1.setPositiveButton(R.string.determine, new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int which) {
								dialog.dismiss();
							}
						});
		        		
						AlertDialog dialog=builder1.create();
						dialog.show();
					}
					else
					{
						dialog.dismiss();
		        		AlertDialog.Builder builder1=new AlertDialog.Builder(PinActivity.this);
						builder1.setMessage(R.string.Reset_Failure);
						//builder1.setIcon(R.drawable.warning);
						//builder1.setTitle(R.string.title);
						builder1.setPositiveButton(R.string.determine, new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int which) {
								dialog.dismiss();
							}
						});
		        		
						AlertDialog dialog=builder1.create();
						dialog.show();
					}
				}
				
				super.handleMessage(msg);
			}
		};
	}
	private OnClickListener listener=new OnClickListener()
	{

		public void onClick(View v) {
			if(v.getId()==R.id.pinbutton1)
			{
				if("".equals(pinEditText2.getText().toString()))
				{
					AlertDialog.Builder builder1=new AlertDialog.Builder(PinActivity.this);
					builder1.setMessage(R.string.Please_enter_the_pin_code);
					//builder1.setIcon(R.drawable.warning);
					//builder1.setTitle(R.string.title);
					builder1.setPositiveButton(R.string.determine, new DialogInterface.OnClickListener() {
						
						public void onClick(DialogInterface dialog, int which) {
							// TODO Auto-generated method stub
							//dialog.dismiss();  
							dialog.dismiss();
							
						}
					});
	        		
					AlertDialog dialog=builder1.create();
					dialog.show();
				}
				else {
					dialog = new ProgressDialog(PinActivity.this);
					dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
					dialog.setTitle(R.string.tip);
		            dialog.setMessage(v.getContext().getString(R.string.Are_test_pin));
	                dialog.setIndeterminate(false);
	                dialog.setCancelable(true);
	                dialog.show();
	               
	                new Thread()
	                {
	                	public void run()
	                	{
	                		try {
	                			String Old=pinEditText2.getText().toString();
	                			byte[] OldPwd=Old.getBytes();
	                			int OldPwdLen=OldPwd.length;
	                			verify=VerifyPwd(OldPwd,OldPwdLen);
	                			//tempverify=String.valueOf(verify);
	                			flag=0;
	                			sendMsg(verify,flag);
	                			
							} catch (Exception e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
	                		
	                	}
	                }.start();
				}
				
			}
			if(v.getId()==R.id.modifyButton2)
			{
				if("".equals(editText1.getText().toString())||"".equals(modifyEditText3.getText().toString()))
				{
					AlertDialog.Builder builder1=new AlertDialog.Builder(PinActivity.this);
					builder1.setMessage(R.string.Please_enter_the_pin_code);
					//builder1.setIcon(R.drawable.warning);
					//builder1.setTitle(R.string.title);
					builder1.setPositiveButton(R.string.determine, new DialogInterface.OnClickListener() {
						
						public void onClick(DialogInterface dialog, int which) {
							// TODO Auto-generated method stub
							//dialog.dismiss();  
							dialog.dismiss();
							
						}
					});
	        		
					AlertDialog dialog=builder1.create();
					dialog.show();
				}
				else
				{
					dialog = new ProgressDialog(PinActivity.this);
					dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
					dialog.setTitle(R.string.tip);
		            dialog.setMessage(v.getContext().getString(R.string.Are_modified));
	                dialog.setIndeterminate(false);
	                dialog.setCancelable(true);
	                dialog.show();
	               
	                new Thread()
	                {
	                	public void run()
	                	{
	                		try {
	                			String Old=editText1.getText().toString();
	                			String New=modifyEditText3.getText().toString();
	                			byte[] OldPassword=Old.getBytes();
	                			byte[] NewPassword=New.getBytes();
	                			int OldPasswordLen=OldPassword.length;
	                			int NewPasswordLen=NewPassword.length;
	                			change=ChangePwd(OldPassword,OldPasswordLen,NewPassword,NewPasswordLen);
	                			//tempchange=String.valueOf(change);
	                			flag=1;
	                			sendMsg(change,flag);
							
										
							} catch (Exception e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
	                		
	                	}
	                }.start();
				}
				
			}
			if(v.getId()==R.id.newButton3)
			{
				if("".equals(newEditText4.getText().toString()))
				{
					AlertDialog.Builder builder1=new AlertDialog.Builder(PinActivity.this);
					builder1.setMessage(R.string.Please_enter_the_pin_code);
					//builder1.setIcon(R.drawable.warning);
					//builder1.setTitle(R.string.title);
					builder1.setPositiveButton(R.string.determine, new DialogInterface.OnClickListener() {
						
						public void onClick(DialogInterface dialog, int which) {
							// TODO Auto-generated method stub
							//dialog.dismiss();  
							dialog.dismiss();
							
						}
					});
	        		
					AlertDialog dialog=builder1.create();
					dialog.show();
				}
				else
				{
					dialog = new ProgressDialog(PinActivity.this);
					dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
					dialog.setTitle(R.string.tip);
		            dialog.setMessage(v.getContext().getString(R.string.Are_reset));
	                dialog.setIndeterminate(false);
	                dialog.setCancelable(true);
	                dialog.show();
	               
	                new Thread()
	                {
	                	public void run()
	                	{
	                		try 
	                		{
	                			String New=newEditText4.getText().toString();
	                			byte[] NewPwd=New.getBytes();
	                			int NewPwdLen=NewPwd.length;
	                			reset=ResetPwd(NewPwd,NewPwdLen);
	                			flag=2;
	                			sendMsg(reset,flag);
								
							} catch (Exception e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
	                	}
	                }.start();
				}
				
				
			}
			
		}
		
	};
	private void sendMsg(long result,int i)
	{
		Message msg=new Message();
		msg.what=(int) result;
		msg.arg1=i;
		handler.sendMessage(msg);
	}
	
	public static native long VerifyPwd(byte[] OldPwd,int OldPwdLen);
	public static native long ChangePwd(byte[] OldPassword,int OldPasswordLen,byte[] NewPassword,int NewPasswordLen);
	public static native long ResetPwd(byte[] NewPwd,int NewPwdLen);
	
}
