package com.zomato.photofilters.imageprocessors;


import android.graphics.Bitmap;

import com.zomato.photofilters.imageprocessors.subfilters.BrightnessSubFilter;
import com.zomato.photofilters.imageprocessors.subfilters.ColorOverlaySubFilter;
import com.zomato.photofilters.imageprocessors.subfilters.ContrastSubFilter;
import com.zomato.photofilters.imageprocessors.subfilters.SaturationSubFilter;
import com.zomato.photofilters.imageprocessors.subfilters.ToneCurveSubFilter;
import com.zomato.photofilters.imageprocessors.subfilters.VignetteSubFilter;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * This Class represents a ImageFilter and includes many subfilters within, we add different subfilters to this class's
 * object and they are then processed in that particular order
 */
public class Filter {
    private List<SubFilter> subFilters = new ArrayList<>();

    public Filter(Filter filter) {
        this.subFilters = filter.subFilters;
    }

    public Filter() {
    }

    /**
     * Adds a Subfilter to the Main Filter
     *
     * @param subFilter Subfilter like contrast, brightness, tone Curve etc. subfilter
     * @see BrightnessSubFilter
     * @see ColorOverlaySubFilter
     * @see ContrastSubFilter
     * @see ToneCurveSubFilter
     * @see VignetteSubFilter
     * @see SaturationSubFilter
     */
    public void addSubFilter(SubFilter subFilter) {
        subFilters.add(subFilter);
    }

    /**
     * Clears all the subfilters from the Parent Filter
     */
    public void clearSubFilters() {
        subFilters.clear();
    }

    /**
     * Removes the subfilter containing Tag from the Parent Filter
     */
    public void removeSubFilterWithTag(String tag) {
        Iterator<SubFilter> iterator = subFilters.iterator();
        while (iterator.hasNext()) {
            SubFilter subFilter = iterator.next();
            if (subFilter.getTag().equals(tag)) {
                iterator.remove();
            }
        }
    }

    /**
     * Returns The filter containing Tag
     */
    public SubFilter getSubFilterByTag(String tag) {
        for (SubFilter subFilter : subFilters) {
            if (subFilter.getTag().equals(tag)) {
                return subFilter;
            }
        }
        return null;
    }

    /**
     * Give the output Bitmap by applying the defined filter
     *
     * @param inputImage Input Bitmap on which filter is to be applied
     * @return filtered Bitmap
     */
    public Bitmap processFilter(Bitmap inputImage) {
        Bitmap outputImage = inputImage;
        if (outputImage != null) {
            for (SubFilter subFilter : subFilters) {
                try {
                    outputImage = subFilter.process(outputImage);
                } catch (OutOfMemoryError oe) {
                    System.gc();
                    try {
                        outputImage = subFilter.process(outputImage);
                    } catch (OutOfMemoryError ignored) {
                    }
                }
            }
        }

        return outputImage;
    }

}

@@ -75,16 +75,59 @@
 .Bitmap;
+%0Aimport android.support.annotation.NonNull;
 %0A%0Aimport
@@ -1489,32 +1489,819 @@
 Filter);%0A    %7D%0A%0A
+    /**%0A     * Adds all %7B@link SubFilter%7Ds from the List to the Main Filter.%0A     * @param subFilterList a list of %7B@link SubFilter%7Ds; must not be null%0A     */%0A    public void addSubFilters(@NonNull List%3CSubFilter%3E subFilterList) %7B%0A        subFilters.addAll(subFilterList);%0A    %7D%0A%0A    /**%0A     * Get a new list of currently applied subfilters.%0A     *%0A     * @return a %7B@link List%7D of %7B@link SubFilter%7D.%0A     *         Empty if no filters are added to %7B@link #subFilters%7D;%0A     *         never null%0A     *%0A     * @see #addSubFilter(SubFilter)%0A     * @see #addSubFilters(List)%0A     */%0A    @NonNull%0A    public List%3CSubFilter%3E getSubFilters() %7B%0A        if (subFilters == null %7C%7C subFilters.isEmpty())%0A            return new ArrayList%3C%3E(0);%0A        return new ArrayList%3C%3E(subFilters);%0A    %7D%0A%0A
     /**%0A     * C

Add APIs to add a list of subfilter and get a copy of applied subfilters