/*
 * Copyright 2017 mk
 *
 * Licensed 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 mk.gdx.firebase.deserialization;

import java.util.Map;

import mk.gdx.firebase.annotations.MapConversion;
import mk.gdx.firebase.annotations.NestedGenericType;
import mk.gdx.firebase.listeners.DataChangeListener;
import mk.gdx.firebase.reflection.AnnotationFinder;

/**
 * Wraps data listener results and do conversion from map to objects, if necessary.
 * <p>
 * We call {@code DataChangeListener<T>} because of nested generic type. For ex. it can be List<User> and we can't do such conversion directly to into the database.
 * {@code coveredListener} data will be cast to {@code <E>} after conversion.
 *
 * @param <T> Result type of listener
 */
public class DataChangeListenerMitmConverter<T, E extends T> extends MapMitmConverter {

    private DataChangeListener<E> coveredListener;
    private Class<T> dataType;

    public DataChangeListenerMitmConverter(Class<T> dataType, DataChangeListener<E> coveredListener, FirebaseMapConverter mapConverter) {
        super(mapConverter);
        this.dataType = dataType;
        this.coveredListener = coveredListener;
    }

    /**
     * Detect if client want POJO class or not and return appropriate callback.
     * <p>
     * If wanted type is POJO we need to retrieve Map and then convert it.
     * Otherwise we need to retrieve type given by user (as T generic type).
     *
     * @return DataChangeListener which retrieves data from database, not null.
     */
    public DataChangeListener<?> get() {
        return isPojo(dataType) ? getPojoListener() : getGenericListener();
    }

    /**
     * Get callback appropriate for pojo retrieving.
     *
     * @return New Pojo data callback instance, not null.
     */
    public DataChangeListener<Map> getPojoListener() {
        return new PojoDataChangeListener();
    }

    /**
     * Get callback appropriate for pojo retrieving.
     *
     * @return New Pojo data callback instance, not null.
     */
    public DataChangeListener<T> getGenericListener() {
        return new GenericDataChangeListener();
    }

    /**
     * Do MITM Map conversion and pass through to {@code coveredListener}.
     *
     * @param data Received data from Firebase - data was cast specified generic type {@code <T>}, may be null.
     * @see #doMitmConversion(Class, Object)
     */
    @SuppressWarnings("unchecked")
    public void onChange(Object data) {
        MapConversion mapConversionAnnotation = AnnotationFinder.getMethodAnnotation(MapConversion.class, coveredListener);
        // If MapConversions was not indicated - do nothing.
        if (mapConversionAnnotation != null) {
            data = (E) doMitmConversion(mapConversionAnnotation.value(), data);
        } else {
            // Depracated - will be remove in next releases.
            NestedGenericType nestedGenericTypeAnnotation = AnnotationFinder.getMethodAnnotation(NestedGenericType.class, coveredListener);
            if (nestedGenericTypeAnnotation != null) {
                data = (E) doMitmConversion(nestedGenericTypeAnnotation.value(), data);
            }
        }
        coveredListener.onChange((E) data);
    }

    /**
     * Pass through to {@code coveredListener}.
     *
     * @param e Exception with description what was wrong.
     */
    public void onCanceled(Exception e) {
        coveredListener.onCanceled(e);
    }

    /**
     * Retrieve map from database and pass to {@link #onChange(Object)}
     */
    private class PojoDataChangeListener implements DataChangeListener<Map> {

        @Override
        public void onChange(Map data) {
            DataChangeListenerMitmConverter.this.onChange(data);
        }

        @Override
        public void onCanceled(Exception e) {
            DataChangeListenerMitmConverter.this.onCanceled(e);
        }
    }

    /**
     * Retrieve {@code T} object from database and pass to  {@link #onChange(Object)}
     */
    private class GenericDataChangeListener implements DataChangeListener<T> {

        @Override
        public void onChange(T data) {
            DataChangeListenerMitmConverter.this.onChange(data);
        }

        @Override
        public void onCanceled(Exception e) {
            DataChangeListenerMitmConverter.this.onCanceled(e);
        }
    }
}

@@ -622,16 +622,125 @@
 ation;%0A%0A
+import com.badlogic.gdx.utils.reflect.ClassReflection;%0A%0Aimport java.util.Collections;%0Aimport java.util.List;%0A
 import j
@@ -808,62 +808,8 @@
 on;%0A
-import mk.gdx.firebase.annotations.NestedGenericType;%0A
 impo
@@ -3135,69 +3135,8 @@
 r);%0A
-        // If MapConversions was not indicated - do nothing.%0A
     
@@ -3270,251 +3270,106 @@
     
-%7D else %7B%0A            // Depracated - will be remove in next releases.%0A            NestedGenericType nestedGenericTypeAnnotation = AnnotationFinder.getMethodAnnotation(NestedGenericType.class, coveredListener);%0A            if (nestedGenericType
+    if (ClassReflection.isAssignableFrom(List.class, dataType) && data.getClass() == mapConversion
 Anno
@@ -3370,32 +3370,32 @@
 onAnnotation
- != null
+.value()
 ) %7B%0A        
@@ -3413,66 +3413,34 @@
 a = 
-(E) doMitmConversion(nestedGenericTypeAnnotation.value(), 
+Collections.singletonList(
 data

drop depracated annotation and do single value wrap into the list for DataChangeListener mitm converter