package io.core9.commerce.payment;

import java.util.HashMap;
import java.util.Map;

import io.core9.commerce.CommerceDataHandlerHelper;
import io.core9.commerce.checkout.Order;
import io.core9.plugin.server.request.Request;
import io.core9.plugin.widgets.datahandler.DataHandler;
import io.core9.plugin.widgets.datahandler.DataHandlerDefaultConfig;
import io.core9.plugin.widgets.datahandler.DataHandlerFactoryConfig;
import net.xeoh.plugins.base.annotations.PluginImplementation;
import net.xeoh.plugins.base.annotations.injections.InjectPlugin;

@PluginImplementation
public class InvoiceVerifierDataHandlerImpl<T extends DataHandlerDefaultConfig> implements InvoiceVerifierDataHandler<T> {
	
	@InjectPlugin
	private CommerceDataHandlerHelper helper;

	@Override
	public String getName() {
		return "Payment-Invoice-Verifier";
	}

	@Override
	public Class<? extends DataHandlerFactoryConfig> getConfigClass() {
		return DataHandlerDefaultConfig.class;
	}

	@Override
	public DataHandler<T> createDataHandler(DataHandlerFactoryConfig options) { 

		return new DataHandler<T>() {

			@Override
			public Map<String, Object> handle(Request req) {
				Order order = helper.getRawOrder(req);
				order.setStatus("pending");
				helper.saveOrder(req, order);
				return new HashMap<String,Object>();
			}

			@SuppressWarnings("unchecked")
			@Override
			public T getOptions() {
				return (T) options;
			}
			
		};
	}

}

@@ -1253,22 +1253,44 @@
 r);%0A%09%09%09%09
-return
+Map%3CString, Object%3E result =
  new Has
@@ -1301,16 +1301,17 @@
 %3CString,
+ 
 Object%3E(
@@ -1313,16 +1313,70 @@
 ect%3E();%0A
+%09%09%09%09result.put(%22type%22, %22invoice%22);%0A%09%09%09%09return result;%0A
 %09%09%09%7D%0A%0A%09%09

set verifier type