package page.misc;

import org.wikiwebserver.handler.http.HTTPException;
import org.wikiwebserver.handler.http.HTTPHandler;
import org.wikiwebserver.handler.http.interfaces.HTTPResponder;
import org.wikiwebserver.util.NochexHelper;

import page.config.SiteTemplatedPage;
import page.tools.entity.Order;
import page.tools.entity.Payment;
import page.tools.entity.User;
import static org.wikiwebserver.html.HTMLHelper.*;

public class PaymentProcessPage extends SiteTemplatedPage implements HTTPResponder {
    
    private NochexHelper nochex = new NochexHelper();   
    
    private Order order = null;
    private Payment payment = null;
    
    public void init(HTTPHandler conn) throws HTTPException {
        super.init(conn);
        
        if (getFormData() != null && getFormData().size() > 0) {
            String paybutton = getFormData().getFirst("paybutton");
            
            if (isSet(paybutton)){
                // Go get payment via Nochex
                nochex.redirectToNochexProcessor(createOrder());
            }
            
            /* Check payment now
             
            String orderID = getFormData().get("order_id");
                         
            if (orderID == null) {
                String msg = "This payment response did not include the order_id.";
                throw new HTTPException(500, msg);
            }            

            this.order = Order.getOrderById(orderID);
            if (order == null) {
                String msg = "This payment refers to an order that does not exist.";
                throw new HTTPException(500, msg);
            }

            try {
                nochex.confirmPurchase(order);
                this.payment = order.getPayment();
                
            } catch (Exception ex) {
                String msg = "Failed to confirm payment for purchase.";
                HTTPException httpEx = new HTTPException(500, msg);
                httpEx.initCause(ex);
                throw httpEx;
            }            
            
            */
        }
    }
    
    public Order createOrder() {
        int amountInPennies = 100;
        String description = "My order";
        return createOrder(amountInPennies, description);
    }
    
    public Order createOrder(int amountInPennies, String description) {
        
        String callback = getServiceAddress() + getUrl();
        User user = getUser();
        Order order = nochex.createOrder(amountInPennies, description, callback, user);    
        
        return order;
    }
	
    public void generate() throws HTTPException {
        
        setTitle("Payment - WikiWebServer.org");
        
        if (getPayment() == null) {
            append(form(submitbutton("paybutton", "Purchase Item")));
        } else {
            Payment payment = getPayment();
            append(codeBox(payment.toString()));       
        }
    }
    
    
    public Order getOrder() {
        return this.order;
    }     
    
    public Payment getPayment() {
        return this.payment;
    }   
}
