package mcc.sample.mobile;

import java.util.Timer;
import java.util.TimerTask;

import oracle.adfmf.amx.event.ActionEvent;
import oracle.adfmf.framework.FeatureContext;
import oracle.adfmf.framework.api.AdfmfContainerUtilities;

public class PopupHelper {

    boolean isPopupOpen = false;

    public PopupHelper() {
        
    }

    public void onPopupShowHideAction(ActionEvent actionEvent) {

        if (!isPopupOpen) {
            //the error object is filled with the reason for a JS exception. If the variable is empty, no exception has 
            //occured. Note that usually you use the return object to pass information from the JS function back to MAF.
            //In this case, the function doesn't return a value and so its only used here to allow debugging of exceptions 
            Object errorMsg = AdfmfContainerUtilities.invokeContainerJavaScriptFunction(FeatureContext.getCurrentFeatureId(),
                                                                      "popupUtilsShowPopup", new Object[] {
                                                                      "_popShowId" });
            
            
            isPopupOpen = true;

            //wait a few seconds and then close the popup
            Timer timer = new Timer();
            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                  closePopup();
                  this.cancel();
                }

            },5000);

        }
            }


    private void closePopup() {

        if (isPopupOpen) {

            Object errorMsg = AdfmfContainerUtilities.invokeContainerJavaScriptFunction(FeatureContext.getCurrentFeatureId(),
                                                                      "popupUtilsHidePopup", new Object[] {
                                                                      "_popCloseId" });
            isPopupOpen = false;

        }

    }
}
