package gov.va.isaac.gui.conceptview.menu;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javafx.concurrent.Task;
import javafx.scene.control.MenuItem;
import javafx.scene.image.Image;
import javafx.stage.Window;
import gov.va.isaac.gui.util.Images;
import gov.va.isaac.interfaces.gui.ApplicationMenus;
import gov.va.isaac.interfaces.gui.MenuItemI;
import gov.va.isaac.interfaces.gui.views.IsaacViewWithMenusI;
import gov.va.isaac.interfaces.gui.views.PopupViewI;
import gov.va.isaac.util.Utility;
import gov.vha.isaac.ochre.api.Get;
import gov.vha.isaac.ochre.api.commit.CommitRecord;


public class GlobalCommit implements IsaacViewWithMenusI {

	private static Logger log = LoggerFactory.getLogger(GlobalCommit.class);

	@Override
	public List<MenuItemI> getMenuBarMenus() {

		ArrayList<MenuItemI> menus = new ArrayList<>();

		// Commit
		menus.add(new MenuItemI()
		{
			@Override
			public void handleMenuSelection(Window parent, MenuItem menuItem) 
			{
				doGlobalCommit();
			}

			@Override
			public int getSortOrder()
			{
				return 40;
			}

			@Override
			public String getParentMenuId()
			{
				return ApplicationMenus.ACTIONS.getMenuId();
			}

			@Override
			public String getMenuName()
			{
				return "Global Commit";
			}

			@Override
			public String getMenuId()
			{
				return "doGlobalCommit";
			}

			@Override
			public boolean enableMnemonicParsing()
			{
				return false;
			}

			/**
			 * @see gov.va.isaac.interfaces.gui.MenuItemI#getImage()
			 */
			@Override
			public Image getImage()
			{
				return null; //Images.CLASSIFIER.getImage();
			}
		});
		
		
		// Cancel
		menus.add(new MenuItemI()
		{
			@Override
			public void handleMenuSelection(Window parent, MenuItem menuItem) 
			{
				doGlobalCancel();
			}

			@Override
			public int getSortOrder()
			{
				return 41;
			}

			@Override
			public String getParentMenuId()
			{
				return ApplicationMenus.ACTIONS.getMenuId();
			}

			@Override
			public String getMenuName()
			{
				return "Global Cancel";
			}

			@Override
			public String getMenuId()
			{
				return "doGlobalCancel";
			}

			@Override
			public boolean enableMnemonicParsing()
			{
				return false;
			}

			@Override
			public Image getImage()
			{
				return null; //Images.CLASSIFIER.getImage();
			}
		});
		
		return menus;
	}

	
	private void doGlobalCommit() {
		Task<Optional<CommitRecord>> cr = Get.commitService().commit("Global commit requested from application menu");
		Utility.execute(cr);
	}
	
	private void doGlobalCancel() {
		Task<Void> cr = Get.commitService().cancel(); 
		Utility.execute(cr);
	}


}

@@ -317,16 +317,49 @@
 indow;%0D%0A
+import gov.va.isaac.AppContext;%0D%0A
 import g
@@ -602,24 +602,80 @@
 opupViewI;%0D%0A
+import gov.va.isaac.interfaces.utility.DialogResponse;%0D%0A
 import gov.v
@@ -2657,24 +2657,217 @@
 Commit() %7B%0D%0A
+%09%09DialogResponse response = AppContext.getCommonDialogs().showYesNoDialog(%22Please Confirm%22, %22Are you sure you want COMMIT all outstanding changes?%22);%0D%0A%09%09if (response == DialogResponse.YES) %7B%0D%0A%09
 %09%09Task%3COptio
@@ -2966,24 +2966,25 @@
  menu%22);%0D%0A%09%09
+%09
 Utility.exec
@@ -2989,24 +2989,29 @@
 ecute(cr);%0D%0A
+%09%09%7D%0D%0A
 %09%7D%0D%0A%09%0D%0A%09priv
@@ -3039,16 +3039,209 @@
 el() %7B%0D%0A
+%09%09DialogResponse response = AppContext.getCommonDialogs().showYesNoDialog(%22Please Confirm%22, %22Are you sure you want CANCEL all outstanding changes?%22);%0D%0A%09%09if (response == DialogResponse.YES) %7B%0D%0A%09
 %09%09Task%3CV
@@ -3284,16 +3284,17 @@
 (); %0D%0A%09%09
+%09
 Utility.
@@ -3307,16 +3307,21 @@
 e(cr);%0D%0A
+%09%09%7D%0D%0A
 %09%7D%0D%0A%0D%0A%0D%0A

add confirmations