/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.apache.deltaspike.test.jsf.impl.config.view.controller.uc005;

import org.apache.deltaspike.core.api.config.view.metadata.CallbackDescriptor;
import org.apache.deltaspike.core.api.config.view.metadata.DefaultCallback;
import org.apache.deltaspike.core.api.config.view.metadata.ExecutableCallbackDescriptor;
import org.apache.deltaspike.core.api.config.view.metadata.ViewMetaData;
import org.apache.deltaspike.core.spi.config.view.ConfigPreProcessor;
import org.apache.deltaspike.core.spi.config.view.ViewConfigNode;

import java.lang.annotation.Annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import java.util.List;
import java.util.Set;

import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

@Target( TYPE )
@Retention(RUNTIME)
@Documented

@ViewMetaData(preProcessor = TestSecured.AnnotationPreProcessor.class)
public @interface TestSecured
{
    Class<? extends TestAccessDecisionVoter>[] value();

    class AnnotationPreProcessor implements ConfigPreProcessor<TestSecured>
    {
        //for a real implementation see e.g.:
        //org.apache.deltaspike.security.api.authorization.Secured
        //org.apache.deltaspike.core.api.config.view.controller.ViewControllerRef
        @Override
        public TestSecured beforeAddToConfig(TestSecured metaData, ViewConfigNode viewConfigNode)
        {
            List<CallbackDescriptor> descriptors = viewConfigNode.getCallbackDescriptors(TestSecured.class);

            if (descriptors.isEmpty()) //just for testing different constellations - usually not needed!
            {
                descriptors.add(new Descriptor(metaData.value(), DefaultCallback.class));
            }
            return metaData; //no change needed
        }
    }

    //can be used from outside to get a typed result
    static class Descriptor extends ExecutableCallbackDescriptor<Set<String>>
    {
        public Descriptor(Class[] beanClasses, Class<? extends Annotation> callbackMarker)
        {
            super(beanClasses, callbackMarker);
        }

        public List<Set<String>> execute(String param1, String param2)
        {
            return super.execute(param1, param2);
        }
    }
}
