package com.example.demoactivity;

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

public class BookActivity extends Activity
{
	private Button inButton;
	private Button inButton3;
	private Button nameButton4;
	private Button nameButton;
	ProgressDialog dialog;
	private TableLayout table1;
	private TableLayout table2;
	int flag;
	Handler handler;
	int temp=0;
	long KeyPair=0;
	long GenCert=0;
	long SignCert=0;
	long Cert=0;
	long Import=0; 
	int f;
	int num=0;
	long certInfo;
	int[] count;
	String issuedBy;
	String issuedTo;
	String expirationDate;
	byte[] sendCert;
	long[] sendCertLen;
	 @Override
	    public void onCreate(Bundle savedInstanceState) {
		 
		 requestWindowFeature(Window.FEATURE_NO_TITLE);
		
	        
		 
		 dialog = new ProgressDialog(BookActivity.this);
	     dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
	     dialog.setTitle(R.string.tip);
		 dialog.setMessage(this.getString(R.string.seekcertificat));
		 dialog.show();
		 new Thread()
		 {
			 public void run()
			 {
				 try {
					 	count=new int[1];
				        long[] certLen=new long[2];
				        
				        byte[] cert1=new byte[2000];
				        byte[] cert1IssuedBy=new byte[100];
				        byte[] cert1IssuedTo=new byte[100];
				        byte[] cert1ExpirationDate=new byte[100];
				        
				        byte[] cert2=new byte[2000];
				        byte[] cert2IssuedBy=new byte[100];
				        byte[] cert2IssuedTo=new byte[100];
				        byte[] cert2ExpirationDate=new byte[100];
				        
				        int[] IssuedByLen=new int[100];
				        int[] IssuedToLen=new int[100];
				        int[] ExpirationDateLen=new int[100];
					 certInfo=getCertInfor(count, certLen, 
				   			 cert1, cert1IssuedBy, cert1IssuedTo, cert1ExpirationDate,
							 cert2, cert2IssuedBy, cert2IssuedTo,cert2ExpirationDate,
							 IssuedByLen, IssuedToLen,ExpirationDateLen);
					 
					 issuedBy=new String(cert1IssuedBy,0,IssuedByLen[0]);
					 issuedTo=new String(cert1IssuedTo,0,IssuedToLen[0]);
					 expirationDate=new String(cert1ExpirationDate,0,ExpirationDateLen[0]);
					 sendCert=cert1;
					 sendCertLen=certLen;
					 
					dialog.dismiss();
					sendMsg(count[0]);

				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			 }
			 
		 }.start();
		 
		 super.onCreate(savedInstanceState);
	        setContentView(R.layout.book_main);
	        
	        table1=(TableLayout)findViewById(R.id.tableLayout1);
	        table2=(TableLayout)findViewById(R.id.tableLayout2);
	        table1.setVisibility(View.GONE);
        	table2.setVisibility(View.GONE);
        	
	        inButton3=(Button)findViewById(R.id.inbutton3);
	        nameButton4=(Button)findViewById(R.id.nameButton4);
	        
	        inButton=(Button)findViewById(R.id.inbutton2);
	        nameButton=(Button)findViewById(R.id.nameButton1);
	        
	        inButton.setOnClickListener(listener);
	        nameButton.setOnClickListener(listener);
	        inButton3.setOnClickListener(listener);
	        nameButton4.setOnClickListener(listener);
	        
	        handler=new Handler()
			{
				public void handleMessage(Message msg)
				{ 
			        if(count[0]==1)
			        {
			        	table1.setVisibility(View.VISIBLE);
			        	//table2.setVisibility(View.GONE);
			        }
					
					int status = msg.what;
					
					if(msg.arg1==0)
					{
						dialog.setMessage(getBaseContext().getString(R.string.Is_add_certificate));
						num++;
		            	
					}
					else if(msg.arg1==1)
					{
						if(status==0)
						{
							num++;
							dialog.setMessage(getBaseContext().getString(R.string.Is_add_certificate)+"\n"+
									getBaseContext().getString(R.string.Was_generating_key));
							
						}
						else {
							dialog.dismiss();
							AlertDialog.Builder builder1=new AlertDialog.Builder(BookActivity.this);
							builder1.setMessage(R.string.Generate_key_failure);
							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 if(msg.arg1==2)
					{
						if(status==0)
						{
							num++;
							dialog.setMessage(getBaseContext().getString(R.string.Is_add_certificate)+"\n"+
									getBaseContext().getString(R.string.Was_generating_key)+"\n"+
									getBaseContext().getString(R.string.certificate_struct)
			        	     		 );
						}
						else {
							dialog.dismiss();
							AlertDialog.Builder builder1=new AlertDialog.Builder(BookActivity.this);
							builder1.setMessage(R.string.Generation_certificate_structure_body_failure);
							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 if(msg.arg1==3)
					{
						if(status==0)
						{
							num++;
							dialog.setMessage(getBaseContext().getString(R.string.Is_add_certificate)+"\n"+
									getBaseContext().getString(R.string.Was_generating_key)+"\n" +
									getBaseContext().getString(R.string.certificate_struct)+"\n" +
									getBaseContext().getString(R.string.Are_issued_certificate_struct));
						}
						else {
							dialog.dismiss();
							AlertDialog.Builder builder1=new AlertDialog.Builder(BookActivity.this);
							builder1.setMessage(R.string.Generation_certificate_structure_body_failure);
							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 if(msg.arg1==4)
					{
						if(status==0)
						{
							num++;
							dialog.setMessage(getBaseContext().getString(R.string.Is_add_certificate)+"\n"+
									getBaseContext().getString(R.string.Was_generating_key)+"\n" +
									getBaseContext().getString(R.string.certificate_struct)+"\n" +
									getBaseContext().getString(R.string.Are_issued_certificate_struct)+"\n" +
									getBaseContext().getString(R.string.Are_packing_certificate));
						}
						else {
							dialog.dismiss();
							AlertDialog.Builder builder1=new AlertDialog.Builder(BookActivity.this);
							builder1.setMessage(R.string.Packing_certificate_failed);
							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 if(msg.arg1==5)
					{
						if(status==0)
						{
							num++;
							dialog.setMessage(getBaseContext().getString(R.string.Is_add_certificate)+"\n"+
									getBaseContext().getString(R.string.Was_generating_key)+"\n" +
									getBaseContext().getString(R.string.certificate_struct)+"\n" +
									getBaseContext().getString(R.string.Are_issued_certificate_struct)+"\n" +
									getBaseContext().getString(R.string.Are_packing_certificate)+"\n"+
									getBaseContext().getString(R.string.Are_import_certificate));
							dialog.dismiss();
							
						}
						else {
							dialog.dismiss();
							AlertDialog.Builder builder1=new AlertDialog.Builder(BookActivity.this);
							builder1.setMessage(R.string.Import_certificate_failed);
							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();
						}
					}
					if(num==6)
					{
						table1.setVisibility(View.VISIBLE);
					}
//
//					
//		            if(temp==2&&num==6)
//		            {
//		            	
//		            	table2.setVisibility(View.VISIBLE);
//		            	addButton.setVisibility(View.GONE);
//		            }
//		            if(flag==2)
//		            {
//		            	addButton.setVisibility(View.GONE);
//		            }
				}
			};
	 }
	 private OnClickListener listener=new OnClickListener()
	 {
		public void onClick(View v) {
			// TODO Auto-generated method stub
			if(v.getId()==R.id.inbutton2)
			{
				//Bundle bundle=new Bundle();
//				String info="aaaa";
//				String people="bbbb";
//				String time="yy-mm-dd";
				//bundle.putString("name", info);
				Intent intent=new Intent(BookActivity.this,BookInActivity.class);
				intent.putExtra("name", issuedBy);
				intent.putExtra("people", issuedTo);
				intent.putExtra("time", expirationDate);
				startActivity(intent);
			}
			if(v.getId()==R.id.nameButton1)
			{
				Intent intent=new Intent(BookActivity.this,NameActivity.class);
				intent.putExtra("cert", sendCert);
				intent.putExtra("certLen", sendCertLen);
				startActivity(intent);
			}
			if(v.getId()==R.id.inbutton3)
			{
				//Bundle bundle=new Bundle();
				String info="aaaa";
				String people="bbbb";
				String time="yy-mm-dd";
				
				//bundle.putString("name", info);
				Intent intent=new Intent(BookActivity.this,BookInActivity.class);
				intent.putExtra("name", info);
				intent.putExtra("people", people);
				intent.putExtra("time", time);
				startActivity(intent);
			}
			if(v.getId()==R.id.nameButton4)
			{
				Intent intent=new Intent(BookActivity.this,NameActivity.class);
				startActivity(intent);
			}
		}
		 
	 };
	
	 private void sendMsg(int result)
		{
			Message msg=new Message();
			msg.what=result;
			handler.sendMessage(msg);
		}

	 
	 public static native long getCertInfor(int[] count, long[] certLen, 
			 byte[] cert1, byte[] cert1IssuedBy, byte[] cert1IssuedTo, byte[] cert1ExpirationDate,
			 byte[] cert2, byte[] cert2IssuedBy, byte[] cert2IssuedTo, byte[] cert2ExpirationDate,
			 int[] IssuedByLen, int[] IssuedToLen, int[] ExpirationDateLen);
	 
	 public static native long GenerateKeyPair(int keyLen);
	 public static native long GenCertBody();
	 public static native long SignCertBody();
	 public static native long GenCert();
	 public static native long ImportCert();
	 
	 static {
	        int version = 0;   
	        try {   
	           version = Integer.valueOf(android.os.Build.VERSION.SDK);   
	        } catch (NumberFormatException e) {   
	        }
	        if(version < 9)
	           System.loadLibrary("Demo8");
	        else
	           System.loadLibrary("Demo"); 
	       }
}
