package edu.upenn.polaris.card.policyappP_t;


import edu.upenn.polaris.card.lib.*;

import javacard.framework.*;

public class PolicyAppP_t extends Applet implements ApprovalInterface {
    
    //Doch's AID 
    static private byte[] TRANSACTIONAPP_AID = {
        (byte) 0x73, (byte) 0x69, (byte) 0x6D, (byte) 0x70, (byte) 0x6C,
        (byte) 0x65, (byte) 0x69, (byte) 0x6e, (byte) 0x73, (byte) 0x74
    };
    private final static short ERR_BASE = (short) 0x11000;
    private final static short ERR_UNKNOWN_MODE = ERR_BASE + 10;
   
    private static byte pls_mode_var;

 
    private byte SECRET = (byte) 0x00;
    short SW_UNAUTHORIZED_CLIENT = (short) 0x6669;

	private static byte[] t;

	
	

    public Vote getVote(byte inByte0,byte inByte1,byte inByte2,byte inByte3) {
        AID client_aid = JCSystem.getPreviousContextAID();

        if (client_aid.equals(TRANSACTIONAPP_AID, (short) 0,
                    (byte) TRANSACTIONAPP_AID.length) == false) {
            ISOException.throwIt(SW_UNAUTHORIZED_CLIENT);
        }


    t[0]=inByte0;t[1]=inByte1;t[2]=inByte2;t[3]=inByte3;

	switch (pls_mode_var) {
	    case 0:
if ((t[3]==(byte)1)) { return vote0;
}
if ((t[3]==(byte)2)) { return vote1;
}
if (true) { return vote2;
}
break;

		   default: }
	
	return NullVote.constructNullVote();
    }

    public void update(byte inByte0,byte inByte1,byte inByte2,byte inByte3, boolean yes) {
    
    t[0]=inByte0;t[1]=inByte1;t[2]=inByte2;t[3]=inByte3;
    
	switch (pls_mode_var) {
	    case 0:
break;

	    default:
            ISOException.throwIt(ERR_UNKNOWN_MODE); 
	}
    }

    //private static final Vote NULL_VOTE = Vote.NULL_VOTE;

    
//[[]->~yes]
 private static VoteImpl vote0;
//[[]~>~yes]
 private static VoteImpl vote1;
//[[]->tof]
 private static VoteImpl vote2;

    public Shareable getShareableInterfaceObject(AID client_aid, 
                                                 byte parameter){
 
        if ((client_aid.equals(TRANSACTIONAPP_AID,
                               (short)0,
                               (byte)TRANSACTIONAPP_AID.length)) 
            == false){ 
            // Check the AID of the applet that invokes this method
            return null;
        }
        if (parameter != SECRET){return null;}
        return this; // Return the access point of this SIO
    }
    
    
    public static void install(byte[] bArray, short bOffset, byte bLength){
	//what does this mean?
        (new PolicyAppP_t()).register(bArray, 
						(short)(bOffset + 1), 
						bArray[bOffset]);
						
						t = new byte[4];

						
	vote0 = new VoteImpl((byte)-128,Vote.STRICT, new byte[] { });
vote1 = new VoteImpl((byte)-128,Vote.DEFEATER, new byte[] { });
vote2 = new VoteImpl((byte)75,Vote.STRICT, new byte[] { });

    }
    
    public void process(APDU apdu){
        byte[] buf = apdu.getBuffer();
        switch(buf[ISO7816.OFFSET_INS]){
        case (byte)0x01: ; // Just for testing
            return;
        default:
            ;
        }
    }
    
    // Register to TransactionApp when this applet has been selected
    public boolean select(){ 
        AID Transaction_aid1 
        = JCSystem.lookupAID(TRANSACTIONAPP_AID, 
                             (short)0, 
                             (byte)TRANSACTIONAPP_AID.length);
        
        if(Transaction_aid1 == null) {
            // Cannot find the TransactionApp AID
            ISOException.throwIt((short)0x0010);
        }
        
        TransactionInterface sio1 
        = (TransactionInterface) 
        (JCSystem.getAppletShareableInterfaceObject(Transaction_aid1, 
                                                    (byte)0x00));

        if(sio1 == null){
            ISOException.throwIt((short)0x0020);
        }

        // Register the policy to TransactionApp
        sio1.addPolicyApp(); 
        return true;
    }

    
}
