package eu.dnetlib.enabling.ui.server;

import java.util.*;

import org.springframework.beans.factory.annotation.Required;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

import eu.dnetlib.enabling.manager.msro.rmi.MSROService;
import eu.dnetlib.enabling.manager.msro.rmi.ProcessDescription;
import eu.dnetlib.enabling.tools.ServiceLocator;
import eu.dnetlib.enabling.ui.common.beans.ManagerRuleInfo;
import eu.dnetlib.enabling.ui.common.beans.ProcessHtmlInfo;
import eu.dnetlib.enabling.ui.common.beans.ProcessInfo;
import eu.dnetlib.enabling.ui.common.services.ManagerService;
import eu.dnetlib.enabling.ui.common.services.MyGwtException;

public class ManagerServlet extends RemoteServiceServlet implements ManagerService {

	/**
	 * 
	 */
	private static final long serialVersionUID = 874365563782740856L;
		
	private ServiceLocator<MSROService> msroLocator;

	
	/**
	 * The time waited to refresh a process image on UI.
	 */
	private int refreshFrequencyMillis;
	/**
	 * The delay used to simulate framebuffer during the redraw of a process image.
	 */
	private int frameDelayMillis;
	
	
	public ServiceLocator<MSROService> getMsroLocator() {
		return msroLocator;
	}

	@Required
	public void setMsroLocator(ServiceLocator<MSROService> msroLocator) {
		this.msroLocator = msroLocator;
	}

	
	public List<ProcessInfo> listOrchestrationProcess(String rsId, int maxProcesses) throws MyGwtException {
		List<ProcessInfo> list = new ArrayList<ProcessInfo>();
		for (ProcessDescription p : msroLocator.getService().listOrchestrationProcesses(rsId, maxProcesses)) {
			ProcessInfo info = new ProcessInfo();
			info.setProcessId(p.getProcessId());
			info.setLastActivity(p.getLastActivity());
			info.setStatus(p.getStatus().toString());
			info.setWorkflowName(p.getWorkflowName());
			list.add(info);
		}
		return list;
	}

	
	
	public ProcessHtmlInfo obtainHtmlProcess(String pid) throws MyGwtException {
		ProcessHtmlInfo info = new ProcessHtmlInfo();
		info.setRefreshFrequencyMillis(refreshFrequencyMillis);
		info.setFrameDelayMillis(frameDelayMillis);
		info.setHtml(msroLocator.getService().processHtmlMap(pid));
		return info;
	}

	public String obtainHtmlProcessNode(String pid, String nodeId) throws MyGwtException {
		// TODO Auto-generated method stub
		return "NOT IMPLEMENTED";
	}
	
	public String executeRule(String ruleId, String actionId) throws MyGwtException {
		// TODO Auto-generated method stub
		return "NOT IMPLEMENTED";
	}

	public Map<String, List<ManagerRuleInfo>> listRules() throws MyGwtException {
		Map<String, List<ManagerRuleInfo>> rules = new HashMap<String, List<ManagerRuleInfo>>();
	
		List<String> actions = new ArrayList<String>();
		actions.add("action 1");
		actions.add("action 2");
		actions.add("action 3");
		
		List<ManagerRuleInfo> list1 = new ArrayList<ManagerRuleInfo>();
		list1.add(new ManagerRuleInfo("123","aaa", "category 1", "aaaaaaaaaa", "daily", "CLASS", "MAILTO", true, actions));
		list1.add(new ManagerRuleInfo("123","aaa", "category 1", "aaaaaaaaaa", "daily", "CLASS", "MAILTO", true, actions));
		list1.add(new ManagerRuleInfo("123","aaa", "category 1", "aaaaaaaaaa", "weekly", "CLASS", "MAILTO", true, actions));
		list1.add(new ManagerRuleInfo("123","aaa", "category 1", "aaaaaaaaaa", "never", "CLASS", "MAILTO", true, actions));
		
		List<ManagerRuleInfo> list2 = new ArrayList<ManagerRuleInfo>();
		list2.add(new ManagerRuleInfo("123","aaa", "category 1", "aaaaaaaaaa", "weekly", "CLASS", "MAILTO", true, actions));
		list2.add(new ManagerRuleInfo("123","aaa", "category 1", "aaaaaaaaaa", "weekly", "CLASS", "MAILTO", true, actions));
		list2.add(new ManagerRuleInfo("123","aaa", "category 1", "aaaaaaaaaa", "weekly", "CLASS", "MAILTO", true, actions));

		List<ManagerRuleInfo> list3 = new ArrayList<ManagerRuleInfo>();
		list3.add(new ManagerRuleInfo("123","aaa", "category 1", "aaaaaaaaaa", "never", "CLASS", "MAILTO", true, actions));
		list3.add(new ManagerRuleInfo("123","aaa", "category 1", "aaaaaaaaaa", "never", "CLASS", "MAILTO", true, actions));
		list3.add(new ManagerRuleInfo("123","aaa", "category 1", "aaaaaaaaaa", "hourly", "CLASS", "MAILTO",true, actions));

		rules.put("category 1", list1);
		rules.put("category 2", list2);
		rules.put("category 3", list3);
		
		return rules;
	}

	public ManagerRuleInfo getRuleInfo(String ruleId) throws MyGwtException {
		List<String> actions = new ArrayList<String>();
		actions.add("action 1");
		actions.add("action 2");
		actions.add("action 3");
		return new ManagerRuleInfo("123","aaa", "category 1", "aaaaaaaaaa", "12/*", "CLASS", "MAILTO", true, actions);
	}

	public int getRefreshFrequencyMillis() {
		return refreshFrequencyMillis;
	}

	@Required
	public void setRefreshFrequencyMillis(int refreshFrequencyMillis) {
		this.refreshFrequencyMillis = refreshFrequencyMillis;
	}

	public int getFrameDelayMillis() {
		return frameDelayMillis;
	}
	
	@Required
	public void setFrameDelayMillis(int frameDelayMillis) {
		this.frameDelayMillis = frameDelayMillis;
	}

	


}
