package page.tools.entity;

/**
 * A Payment object stores the information about a payment
 * 
 * @author Michael Gardiner
 *
 */
public class Payment extends ProtectedStorable {
    
    public String getType() {
        return "Payment";
    } 
    
    public Payment() {
        super();
    }
    
    Payment(String id) {
        super(id);
    }   
    
    public void setOrder(Order order) {
        put("orderID", order.getId());
    }
    
    public Order getOrder() {
        String orderID = (String) get("orderID");
        if (orderID == null) return null;
        return Order.getOrderById(orderID);
    }    
    
    public void setPaymentTo(String email) {
        put("paymentTo", email);
    }
    
    public String getPaymentTo() {
        return (String) get("paymentTo");
    }   
    
    public void setPaymentFrom(String email) {
        put("paymentFrom", email);
    }
    
    public String getPaymentFrom() {
        return (String) get("paymentFrom");
    }    
    
    public void setAmountInPennies(int pennies) {
        put("amountInPennies", new Long(pennies));
    }
    
    public int getAmountInPennies() {
        Long pennies = (Long) get("amountInPennies");
        if (pennies == null) pennies = new Long(0);
        return pennies.intValue();
    }
    
    public String getAmount() {
        return Payment.penniesToPounds(getAmountInPennies());
    }
    
    public void setLive(boolean live) {
        put("live", new Boolean(live));
    }
    
    public boolean isLive() {
        Boolean live = (Boolean) get("live");
        if (live == null) live = new Boolean(false);
        return live.booleanValue();
    }       
    
    
            
    
    public void setAPCResponse(String apcResponse) {
        put("apcResponse", apcResponse);
    }
    
    public String getAPCResponse() {
        return (String) get("apcResponse");
    }    
    
    public void setAPCConfirmation(String apcConfirmation) {
        put("apcConfirmation", apcConfirmation);
    }
    
    public String getAPCConfirmation() {
        return (String) get("apcConfirmation");
    }  
    
    
    public void setConfirmed(boolean confirmed) {
        put("confirmed", new Boolean(confirmed));
    }
    
    public boolean isConfirmed() {
        Boolean confirmed = (Boolean) get("confirmed");
        if (confirmed == null) confirmed = new Boolean(false);
        return confirmed.booleanValue();
    }     
    
    public static Payment getPaymentById(String id) {
        Payment p = new Payment(id);
        if (p.isValid()) return p;
        // Not created
        return null; 
    }    
    
    public String toString() {
        String LF = "\r\n";
        
        StringBuilder info = new StringBuilder();
        info.append("PaymentID: " + getId() + LF);
        info.append("PaymentTo: " + getPaymentTo() + LF);
        info.append("PaymentFrom: " + getPaymentFrom() + LF);
        info.append("Amount: " + getAmount() + LF);
        info.append("Live: " + isLive() + LF);
        String orderID = getOrder() == null ? "none" : getOrder().getId();
        info.append("OrderID:" + orderID + LF);
        info.append("APCResponse:" + getAPCResponse() + LF);
        info.append("APCConfirmation:" + getAPCConfirmation() + LF);
        info.append("Confirmed: " + isConfirmed() + LF);
        
        return info.toString();
    }    
    
    
    
    public static String penniesToPounds(long numPennies) {
        String s = String.valueOf(numPennies);
        if (numPennies == 0) return "0.00";
        if (s.length() <= 2) return "0." + numPennies;
        
        String pounds = s.substring(0, s.length()-2);
        String pennies = s.substring(s.length()-2);
        
        return pounds + "." + pennies;
    }      
    
    protected void checkCanClear() {
        if (this.isLive()) {
            throw new SecurityException("Live payments can not be deleted");
        }
    }    
}

