package com.collabnet.ctf.events;

import com.collabnet.ce.soap60.types.SoapFieldValues;
import com.vasoftware.sf.events.EventContext;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;

public class HookUtil {

    /**
     * Get hook script name
     *
     * @return
     */
    public static File getHookScript(String hookScriptLocation, String eventType, String operation) {

        String hookScriptPath = hookScriptLocation + eventType + "_" + operation;

        return new File(hookScriptPath);
    }
    /**
     * Set context infomration into environment
     *
     * @param process
     */
    public static void setEventContextInformationInEnvironment(EventContext eventContext, ProcessBuilder process) {
        Map<String, String> environment = process.environment();
        environment.put("tf_type", eventContext.getType());
        environment.put("tf_operation", eventContext.getOperation());
        environment.put("tf_projectId", eventContext.getProjectId() == null ? "null" :
                eventContext.getProjectId());
        environment.put("tf_userName", eventContext.getUsername());
        environment.put("tf_comment", eventContext.getComment() == null ? "null" : eventContext.getComment());
    }

    /**
     * Set result values into environment
     *
     * @param process
     * @param data
     * @param dataDifferentiator
     * @throws IllegalAccessException
     * @throws InvocationTargetException
     */
    public static void setResultValuesInEnvironment(ProcessBuilder process, Object data, String dataDifferentiator)
            throws IllegalAccessException, InvocationTargetException {
        Map<String, String> environment = process.environment();

        Class dataClass = data.getClass();
        Method[] methods = dataClass.getMethods();

        environment.put("tf_" + dataDifferentiator + "DataClassName", dataClass.toString());
        environment.put("tf_" + dataDifferentiator + "DataClassName", dataClass.toString());

        for (Method m : methods) {
            // we are only interested in getter methods AND that do not take any parameters
            if (m.getName().startsWith("get") && m.getParameterTypes().length == 0) {
                Object result = m.invoke(data);
                if (result != null && result instanceof SoapFieldValues) {
                    SoapFieldValues fieldValues = (SoapFieldValues) result;
                    for (int i = 0; i < fieldValues.getNames().length; ++i) {
                        environment.put("tf_" + dataDifferentiator + "_flex_field_name_" + i,
                                fieldValues.getNames()[i] == null ? "null" : fieldValues.getNames()[i]);
                        environment.put("tf_" + dataDifferentiator + "_flex_field_type_" + i,
                                fieldValues.getTypes()[i] == null ? "null" : fieldValues.getTypes()[i]);
                        environment.put("tf_" + dataDifferentiator + "_flex_field_value_" + i,
                                fieldValues.getValues()[i] == null ? "null" : fieldValues.getValues()[i].toString());
                    }
                } else {
                    environment.put("tf_" + dataDifferentiator + m.getName().substring(3),
                            result == null ? "null" : result.toString());
                }
            }
        }
    }
}
