/*
 * Copyright (C) 2010 Teleal GmbH, Switzerland
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.teleal.cling.binding.annotations;

import org.teleal.cling.binding.LocalServiceBindingException;
import org.teleal.cling.model.Constants;
import org.teleal.cling.model.ModelUtil;
import org.teleal.cling.model.action.ActionExecutor;
import org.teleal.cling.model.action.MethodActionExecutor;
import org.teleal.cling.model.meta.Action;
import org.teleal.cling.model.meta.ActionArgument;
import org.teleal.cling.model.meta.LocalService;
import org.teleal.cling.model.meta.StateVariable;
import org.teleal.cling.model.state.GetterStateVariableAccessor;
import org.teleal.cling.model.state.StateVariableAccessor;
import org.teleal.cling.model.types.Datatype;
import org.teleal.common.util.Reflections;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Logger;

/**
 * @author Christian Bauer
 */
public class AnnotationActionBinder {

    private static Logger log = Logger.getLogger(AnnotationLocalServiceBinder.class.getName());

    protected UpnpAction annotation;
    protected Method method;
    protected Map<StateVariable, StateVariableAccessor> stateVariables;
    protected Set<Class> stringConvertibleTypes;

    public AnnotationActionBinder(Method method, Map<StateVariable, StateVariableAccessor> stateVariables, Set<Class> stringConvertibleTypes) {
        this.annotation = method.getAnnotation(UpnpAction.class);
        this.stateVariables = stateVariables;
        this.method = method;
        this.stringConvertibleTypes = stringConvertibleTypes;
    }

    public UpnpAction getAnnotation() {
        return annotation;
    }

    public Map<StateVariable, StateVariableAccessor> getStateVariables() {
        return stateVariables;
    }

    public Method getMethod() {
        return method;
    }

    public Set<Class> getStringConvertibleTypes() {
        return stringConvertibleTypes;
    }

    public void appendAction(Map<Action, ActionExecutor> actions) throws LocalServiceBindingException {

        String name;
        if (getAnnotation().name().length() != 0) {
            name = getAnnotation().name();
        } else {
            name = AnnotationLocalServiceBinder.toUpnpActionName(getMethod().getName());
        }

        log.fine("Creating action and executor: " + name);

        List<ActionArgument> inputArguments = createInputArguments();
        Map<ActionArgument<LocalService>, StateVariableAccessor> outputArguments = createOutputArguments();

        inputArguments.addAll(outputArguments.keySet());
        ActionArgument<LocalService>[] actionArguments =
                inputArguments.toArray(new ActionArgument[inputArguments.size()]);

        Action action = new Action(name, actionArguments);
        ActionExecutor executor = createExecutor(outputArguments);

        actions.put(action, executor);
    }

    protected ActionExecutor createExecutor(Map<ActionArgument<LocalService>, StateVariableAccessor> outputArguments) {
        // TODO: Invent an annotation for this configuration
        return new MethodActionExecutor(outputArguments, getMethod());
    }

    protected List<ActionArgument> createInputArguments() throws LocalServiceBindingException {

        List<ActionArgument> list = new ArrayList();

        // Input arguments are always method parameters
        int annotatedParams = 0;
        Annotation[][] params = getMethod().getParameterAnnotations();
        for (int i = 0; i < params.length; i++) {
            Annotation[] param = params[i];
            for (Annotation paramAnnotation : param) {
                if (paramAnnotation instanceof UpnpInputArgument) {
                    UpnpInputArgument inputArgumentAnnotation = (UpnpInputArgument) paramAnnotation;
                    annotatedParams++;

                    String argumentName =
                            inputArgumentAnnotation.name();

                    StateVariable stateVariable =
                            findRelatedStateVariable(
                                    inputArgumentAnnotation.stateVariable(),
                                    argumentName,
                                    getMethod().getName()
                            );

                    if (stateVariable == null) {
                        // Attempt to use the method name, this makes definition of single input/output arg methods easier

                    }

                    if (stateVariable == null) {
                        throw new LocalServiceBindingException(
                                "Could not detected related state variable of argument: " + argumentName
                        );
                    }

                    validateType(stateVariable, getMethod().getParameterTypes()[i]);

                    ActionArgument inputArgument = new ActionArgument(
                            argumentName,
                            stateVariable.getName(),
                            ActionArgument.Direction.IN
                    );

                    list.add(inputArgument);
                }
            }
        }
        // A method can't have any parameters that are not annotated with @UpnpInputArgument - we wouldn't know what
        // value to pass when we invoke it later on...
        if (annotatedParams < getMethod().getParameterTypes().length) {
            throw new LocalServiceBindingException("Method has parameters that are not input arguments: " + getMethod().getName());
        }

        return list;
    }

    protected Map<ActionArgument<LocalService>, StateVariableAccessor> createOutputArguments() throws LocalServiceBindingException {

        Map<ActionArgument<LocalService>, StateVariableAccessor> map = new LinkedHashMap(); // !!! Insertion order!

        UpnpAction actionAnnotation = getMethod().getAnnotation(UpnpAction.class);
        if (actionAnnotation.out().length == 0) return map;

        boolean hasMultipleOutputArguments = actionAnnotation.out().length > 1;

        for (UpnpOutputArgument outputArgumentAnnotation : actionAnnotation.out()) {

            String argumentName = outputArgumentAnnotation.name();

            StateVariable stateVariable = findRelatedStateVariable(
                    outputArgumentAnnotation.stateVariable(),
                    argumentName,
                    getMethod().getName()
            );

            // Might-just-work attempt, try the name of the getter
            if (stateVariable == null && outputArgumentAnnotation.getterName().length() > 0) {
                stateVariable = findRelatedStateVariable(null, null, outputArgumentAnnotation.getterName());
            }

            if (stateVariable == null) {
                throw new LocalServiceBindingException(
                        "Related state variable not found for output argument: " + argumentName
                );
            }

            StateVariableAccessor accessor = findOutputArgumentAccessor(
                    stateVariable,
                    outputArgumentAnnotation.getterName(),
                    hasMultipleOutputArguments
            );

            log.finer("Found related state variable for output argument '" + argumentName + "': " + stateVariable);

            ActionArgument outputArgument = new ActionArgument(
                    argumentName,
                    stateVariable.getName(),
                    ActionArgument.Direction.OUT,
                    !hasMultipleOutputArguments
            );

            map.put(outputArgument, accessor);
        }

        return map;
    }

    protected StateVariableAccessor findOutputArgumentAccessor(StateVariable stateVariable, String getterName, boolean multipleArguments)
            throws LocalServiceBindingException {

        boolean isVoid = getMethod().getReturnType().equals(Void.TYPE);

        if (isVoid) {

            if (getterName != null && getterName.length() > 0) {
                log.finer("Action method is void, will use getter method named: " + getterName);

                // Use the same class as the action method
                Method getter = Reflections.getMethod(getMethod().getDeclaringClass(), getterName);
                if (getter == null)
                    throw new LocalServiceBindingException(
                            "Declared getter method '" + getterName + "' not found on: " + getMethod().getDeclaringClass()
                    );

                validateType(stateVariable, getter.getReturnType());

                return new GetterStateVariableAccessor(getter);

            } else {
                log.finer("Action method is void, trying to find existing accessor of related: " + stateVariable);
                return getStateVariables().get(stateVariable);
            }


        } else if (getterName != null && getterName.length() > 0) {
            log.finer("Action method is not void, will use getter method on returned instance: " + getterName);

            // Use the returned class
            Method getter = Reflections.getMethod(getMethod().getReturnType(), getterName);
            if (getter == null)
                throw new LocalServiceBindingException(
                        "Declared getter method '" + getterName + "' not found on return type: " + getMethod().getReturnType()
                );

            validateType(stateVariable, getter.getReturnType());

            return new GetterStateVariableAccessor(getter);

        } else if (!multipleArguments) {
            log.finer("Action method is not void, will use the returned instance: " + getMethod().getReturnType());
            validateType(stateVariable, getMethod().getReturnType());
        }

        return null;
    }

    protected StateVariable findRelatedStateVariable(String declaredName, String argumentName, String methodName)
            throws LocalServiceBindingException {

        StateVariable relatedStateVariable = null;

        if (declaredName != null && declaredName.length() > 0) {
            String actualName = AnnotationLocalServiceBinder.toUpnpStateVariableName(declaredName);
            log.finer("Finding related state variable with declared name: " + actualName);
            relatedStateVariable = getStateVariable(actualName);
        }

        if (relatedStateVariable == null && argumentName != null && argumentName.length() > 0) {
            // Try with A_ARG_TYPE prefix
            String actualName = Constants.ARG_TYPE_PREFIX + argumentName;
            log.finer("Finding related state variable with prefixed argument name: " + actualName);
            relatedStateVariable = getStateVariable(actualName);
        }

        if (relatedStateVariable == null && methodName != null && methodName.length() > 0) {
            // TODO: Well, this is often a nice shortcut but sometimes might have false positives
            String methodPropertyName = Reflections.getMethodPropertyName(methodName);
            if (methodPropertyName != null) {
                log.finer("Finding related state varible with method property name: " + methodPropertyName);
                relatedStateVariable =
                        getStateVariable(
                                AnnotationLocalServiceBinder.toUpnpStateVariableName(methodPropertyName)
                        );
            }
        }

        return relatedStateVariable;
    }

    protected void validateType(StateVariable stateVariable, Class type) throws LocalServiceBindingException {

        // Validate datatype as good as we can
        // (for enums and other convertible types, the state variable type should be STRING)

        Datatype.Default expectedDatatype =
                ModelUtil.isStringConvertibleType(getStringConvertibleTypes(), type)
                        ? Datatype.Default.STRING
                        : Datatype.Default.getByJavaType(type);

        log.finer("Expecting '" + stateVariable + "' to be of type: " + expectedDatatype);

        if (expectedDatatype != null &&
                !expectedDatatype.getBuiltinType().equals(stateVariable.getTypeDetails().getDatatype().getBuiltin())) {

            throw new LocalServiceBindingException(
                    "State variable has wrong type: " + stateVariable
            );

        } else if (expectedDatatype == null && stateVariable.getTypeDetails().getDatatype().getBuiltin() != null) {
            throw new LocalServiceBindingException(
                    "State variable has wrong type (expected custom datatype '" + type.getSimpleName() + "'): " + stateVariable
            );
        }

        log.finer("State variable matches required argument datatype (or can't be validated because it is custom)");
    }

    protected StateVariable getStateVariable(String name) {
        for (StateVariable stateVariable : getStateVariables().keySet()) {
            if (stateVariable.getName().equals(name)) {
                return stateVariable;
            }
        }
        return null;
    }

}
