package terminal;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;

import backend.BackendRentalTerminal;

/*
 * This is the main class of Rental Terminal
 * contains all the code that will interact with the user
 * This class is related to RentalTerminal_classes that contains the functions executed in this class
 * 
 */
public class RentalTerminal {
	public RentalTerminal(){
		
	}
	
	public static void main(String[] args) throws SQLException {
		//Call the classes that contains all the script needed
		BackendRentalTerminal rt = new BackendRentalTerminal();
		
		System.out.println("Welcome to the Cars-Rental");
		System.out.println("1. Register (for new customer)");
		System.out.println("2. Top Up");
		System.out.println("3. Refund");
		System.out.print("Choose a menu number: ");
		Scanner scan = new Scanner(System.in);
	    String menu = scan.nextLine();
		if(menu.equals("1")){
			System.out.println("Input your name: ");
			Scanner scName = new Scanner(System.in);
		    String custName = scName.nextLine();
		    
		    rt.RegisterNewCustomer(custName);
		    System.out.println("Congratulation you have been registered");
		    System.out.println("Name : " + custName);
		    
		}else if(menu.equals("2")){
			System.out.println("Menu Top Up");
			/*ALGO: 1. get cardCert from S
			 * 		2. do mutual authentication between Rental Terminal - Smartcard 
			 * 		3. get card data from database
			 * 		4. update kilometers and certificate
			 * 		5. update database + update card certificate and kilometers
			 */
			
		    //Do Mutual Authentication --> check the card data
		    if(rt.MutualAuthenticationRT_S()){
		    	//read certificate from the card --> after Mutual Authentication of RT and S  
				byte[] cardCert = new byte[163];
				//Get the card data from the database
				Card cardDB = rt.getCardDB(cardCert);
				if(cardDB!=null){
				    System.out.println(cardDB.getKilometers());
					
					System.out.print("Input the kilometers that you want to add: ");
				    Scanner scKm = new Scanner(System.in);
				    short kilometers = (short) scKm.nextInt();
				    
				    Card card_new = rt.topUpCard(cardCert, cardDB, kilometers);
					System.out.println(card_new.getKilometers());
				}
		    }else{
		    	System.out.println("Authentication failed. Card is not recognized.");
		    }
		}else if(menu.equals("3")){
			System.out.println("Menu Refund");
			
			if(rt.MutualAuthenticationRT_S()){
				//HardCoded CardPublicKey and km  --> supposed to be read from the card --> after Mutual Authentication of RT and S  
				byte[] cardPK = new byte[126]; 
				
				//Get Card data from database
				Card card = rt.getCardDB(cardPK);
				if(card != null){
					System.out.println(card.getKilometers());
					
				}
			}else{
				System.out.println("Authentication failed. Card is not recognized.");
			}
			
		}

	}
}

@@ -49,96 +49,8 @@
 n;%0D%0A
-import java.text.SimpleDateFormat;%0D%0Aimport java.util.Calendar;%0D%0Aimport java.util.Date;%0D%0A
 impo
@@ -2422,149 +2422,256 @@
 %0A%09%09%09
-System.out.println(%22Menu Refund%22);%0D%0A%09%09%09%0D%0A%09%09%09if(rt.MutualAuthenticationRT_S())%7B%0D%0A%09%09%09%09//HardCoded CardPublicKey and km  --%3E supposed to be read
+//Refund is topup with 0%0D%0A%09%09%09//QUESTION: do we need to revoke the key at refund?%0D%0A%09%09%09System.out.println(%22Menu Refund%22);%0D%0A%09%09%09%0D%0A%09%09%09//Do Mutual Authentication --%3E check the card data%0D%0A%09%09    if(rt.MutualAuthenticationRT_S())%7B%0D%0A%09%09    %09//read certificate
  fro
@@ -2743,18 +2743,20 @@
 e%5B%5D card
-PK
+Cert
  = new b
@@ -2764,13 +2764,12 @@
 te%5B1
-2
 6
+3
 %5D;
- 
 %0D%0A%09%09
@@ -2782,17 +2782,21 @@
 %09%09//Get 
-C
+the c
 ard data
@@ -2793,32 +2793,36 @@
  card data from 
+the 
 database%0D%0A%09%09%09%09Ca
@@ -2828,16 +2828,18 @@
 ard card
+DB
  = rt.ge
@@ -2854,10 +2854,12 @@
 card
-PK
+Cert
 );%0D%0A
@@ -2873,12 +2873,12 @@
 card
- 
+DB
 !=
- 
 null
@@ -2881,25 +2881,28 @@
 null)%7B%0D%0A%09%09%09%09
-%09
+    
 System.out.p
@@ -2912,16 +2912,18 @@
 tln(card
+DB
 .getKilo
@@ -2934,25 +2934,66 @@
 rs());%0D%0A%09%09%09%09
-%09
+    rt.refundKilometers(cardCert, cardDB);
 %0D%0A%09%09%09%09%7D%0D%0A%09%09%09
@@ -2991,17 +2991,20 @@
 %09%09%09%7D%0D%0A%09%09
-%09
+    
 %7Delse%7B%0D%0A
@@ -3005,17 +3005,20 @@
 lse%7B%0D%0A%09%09
-%09
+    
 %09System.
@@ -3087,15 +3087,13 @@
 %0D%0A%09%09
-%09%7D%0D%0A%09%09%09
+    %7D
 %0D%0A%09%09

add refund menu