package getlastorderupdate;

import com.fxcore2.IO2GResponseListener;
import com.fxcore2.O2GLastOrderUpdateResponseReader;
import com.fxcore2.O2GOrderResponseReader;
import com.fxcore2.O2GOrderRow;
import com.fxcore2.O2GResponse;
import com.fxcore2.O2GResponseReaderFactory;
import com.fxcore2.O2GResponseType;
import com.fxcore2.O2GSession;
import com.fxcore2.O2GTableUpdateType;

public class ResponseListener implements IO2GResponseListener {

    private boolean mError = false;
    private boolean mRequestComplete = false;
    private String mRequestID = "";
    private String mOrderID = "";
    private O2GSession mSession;
    
    public ResponseListener(O2GSession session) {
        mSession = session;
    }
    
    // Shows if there was an error during the request processing
    public final boolean hasError() {
        return mError;
    }
    
    public void setRequest(String requestID)
    {
        mRequestID = requestID;
        mError = false;
        mRequestComplete = false;
        
    }
    
    // Implementation of IO2GResponseListener interface public method onRequestCompleted
    public void onRequestCompleted(String requestID, O2GResponse response) {
        if (mRequestID.equals(requestID)) {
            System.out.println("Request completed.\nrequestID= " + requestID);
            O2GResponseReaderFactory readerFactory = mSession.getResponseReaderFactory();
            if (response.getType() == O2GResponseType.CREATE_ORDER_RESPONSE) {    
                O2GOrderResponseReader orderResponseReader = readerFactory.createOrderResponseReader(response);
                mOrderID = orderResponseReader.getOrderID();
            }
            else if (response.getType() == O2GResponseType.GET_LAST_ORDER_UPDATE) {
                O2GLastOrderUpdateResponseReader lastOrderUpdateResponseReader = readerFactory.createLastOrderUpdateResponseReader(response);
                O2GOrderRow orderRow = lastOrderUpdateResponseReader.getOrder();
                O2GTableUpdateType updateType = lastOrderUpdateResponseReader.getUpdateType();
                String updateTypeString = updateType.toString();
                System.out.println("UpdateType: " + updateTypeString + "; OrderID: " + orderRow.getOrderID() + "; Status: " + orderRow.getStatus());
            }
            mError = false;
            mRequestComplete = true;
        }
    }

    // Implementation of IO2GResponseListener interface public method onRequestFailed
    public void onRequestFailed(String requestID, String error) {
        if (mRequestID.equals(requestID)) {
            System.out.println("Request failed.\nrequestID= " + requestID + "; error= " + error);
            mError = true;
            mRequestComplete = false;
        }
    }

    // Implementation of IO2GResponseListener interface public method onTablesUpdates
    public void onTablesUpdates(O2GResponse response) {
        
    }

    public boolean isRequestComplete() {
        return mRequestComplete;
    }
    
    public String getOrderID() {
        return mOrderID;
    }
}
