/**
 *  Copyright 2005-2015 Red Hat, Inc.
 *
 *  Red Hat 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 io.fabric8.cdi.bean;

import io.fabric8.cdi.Types;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Any;
import javax.enterprise.inject.Default;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.InjectionPoint;
import javax.enterprise.inject.spi.PassivationCapable;
import javax.enterprise.util.AnnotationLiteral;
import java.lang.annotation.Annotation;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public abstract class BaseBean<T> implements Bean<T>, PassivationCapable {

    private static final Annotation[] DEFAULT_QUALIFIERS = {
            new AnnotationLiteral<Default>() {
            },
            new AnnotationLiteral<Any>() {
            }
    };

    private Type beanType;
    private final String name;
    private final Set<Type> types;
    private final Set<Annotation> qualifiers;

    public BaseBean(String name, Annotation... annotations) {
        this.name = name;
        ParameterizedType genericSuperclass = (ParameterizedType) getClass().getGenericSuperclass();
        this.beanType = (Class<T>) genericSuperclass.getActualTypeArguments()[0];
        List<Type> allTypes = new ArrayList<>();
        allTypes.add(beanType);

        for (Type t = Types.superClassOf(beanType); !allTypes.contains(Object.class) && t != null; t =   Types.superClassOf(beanType)) {
            allTypes.add(t);
        }

        types = Collections.unmodifiableSet(new HashSet<>(allTypes));
        qualifiers = Collections.unmodifiableSet(new HashSet<>(Arrays.asList(annotations)));
    }

    public BaseBean(String name) {
        this.name = name;
        ParameterizedType genericSuperclass = (ParameterizedType) getClass().getGenericSuperclass();
        this.beanType = (Class<T>) genericSuperclass.getActualTypeArguments()[0];
        List<Type> allTypes = new ArrayList<>();
        allTypes.add(beanType);

        for (Type t = Types.superClassOf(beanType); !allTypes.contains(Object.class) && t != null; t =   Types.superClassOf(beanType)) {
            allTypes.add(t);
        }

        types = Collections.unmodifiableSet(new HashSet<>(allTypes));
        qualifiers = Collections.unmodifiableSet(new HashSet<>(Arrays.asList(DEFAULT_QUALIFIERS)));
    }

    public BaseBean(String name, Type beanType, Annotation ... annotations) {
        this.name = name;
        this.beanType = beanType;
        List<Type> allTypes = new ArrayList<>();
        allTypes.add(beanType);

        for (Type t = Types.superClassOf(beanType); !allTypes.contains(Object.class) && t != null; t =   Types.superClassOf(beanType)) {
            allTypes.add(t);
        }

        types = Collections.unmodifiableSet(new HashSet<>(allTypes));
        qualifiers = Collections.unmodifiableSet(new HashSet<>(Arrays.asList(annotations)));
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public Class<?> getBeanClass() {
        return Types.asClass(beanType);
    }

    @Override
    public String getId() {
        return getName();
    }

    @Override
    public Set<InjectionPoint> getInjectionPoints() {
        return Collections.emptySet();
    }

    @Override
    public boolean isNullable() {
        return false;
    }

    @Override
    public Set<Type> getTypes() {
        return types;
    }

    @Override
    public Set<Annotation> getQualifiers() {
        return qualifiers;
    }

    @Override
    public Class<? extends Annotation> getScope() {
        return ApplicationScoped.class;
    }

    @Override
    public Set<Class<? extends Annotation>> getStereotypes() {
        return Collections.emptySet();
    }

    @Override
    public boolean isAlternative() {
        return false;
    }
}

@@ -1133,36 +1133,8 @@
 pe;%0A
-import java.util.ArrayList;%0A
 impo
@@ -1229,17 +1229,26 @@
 .util.Li
-s
+nkedHashSe
 t;%0Aimpor
@@ -1940,35 +1940,34 @@
 s()%5B0%5D;%0A        
-Lis
+Se
 t%3CType%3E allTypes
@@ -1969,32 +1969,36 @@
 Types = new 
-ArrayLis
+LinkedHashSe
 t%3C%3E();%0A     
@@ -2145,32 +2145,25 @@
 uperClassOf(
-beanType
+t
 )) %7B%0A       
@@ -2235,38 +2235,24 @@
 difiableSet(
-new HashSet%3C%3E(
 allTypes));%0A
@@ -2240,33 +2240,32 @@
 bleSet(allTypes)
-)
 ;%0A        qualif
@@ -2594,35 +2594,34 @@
 s()%5B0%5D;%0A        
-Lis
+Se
 t%3CType%3E allTypes
@@ -2623,32 +2623,36 @@
 Types = new 
-ArrayLis
+LinkedHashSe
 t%3C%3E();%0A     
@@ -2799,32 +2799,25 @@
 uperClassOf(
-beanType
+t
 )) %7B%0A       
@@ -2889,38 +2889,24 @@
 difiableSet(
-new HashSet%3C%3E(
 allTypes));%0A
@@ -2894,33 +2894,32 @@
 bleSet(allTypes)
-)
 ;%0A        qualif
@@ -3157,19 +3157,18 @@
         
-Lis
+Se
 t%3CType%3E 
@@ -3186,16 +3186,20 @@
 new 
-ArrayLis
+LinkedHashSe
 t%3C%3E(
@@ -3358,24 +3358,17 @@
 ClassOf(
-beanType
+t
 )) %7B%0A   
@@ -3448,30 +3448,16 @@
 ableSet(
-new HashSet%3C%3E(
 allTypes
@@ -3457,17 +3457,16 @@
 llTypes)
-)
 ;%0A      

Fix possible infinite loop in fabric8-cdi.