package com.leroymerlin.pandroid.app.delegate;

import android.os.Bundle;
import android.view.View;

import com.leroymerlin.pandroid.app.ResumeState;
import com.leroymerlin.pandroid.future.CancellableActionDelegate;

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

/**
 * Created by florian on 26/11/15.
 */
public class PandroidDelegate extends SimpleLifecycleDelegate<Object> implements CancellableActionDelegate.ActionDelegateRegister {

    public static final String TAG = "PandroidLifecycleDelegate";
    private List<CancellableActionDelegate> delegates = new ArrayList<>();
    private List<LifecycleDelegate<Object>> lifecycleDelegates = new ArrayList<>();

    public ResumeState getResumeState() {
        return resumeState;
    }

    public void addLifecycleDelegate(LifecycleDelegate<Object> lifecycleDelegate) {
        this.lifecycleDelegates.add(lifecycleDelegate);
    }

    public boolean removeLifecycleDelegate(LifecycleDelegate lifecycleDelegate) {
        return this.lifecycleDelegates.remove(lifecycleDelegate);
    }

    @Override
    public void onInit(Object target) {
        super.onInit(target);
        for (LifecycleDelegate<Object> lifecycleDelegate : lifecycleDelegates) {
            lifecycleDelegate.onInit(target);
        }
    }

    @Override
    public void onCreateView(Object target, View view, Bundle savedInstanceState) {
        super.onCreateView(target, view, savedInstanceState);
        for (LifecycleDelegate<Object> lifecycleDelegate : lifecycleDelegates) {
            lifecycleDelegate.onCreateView(target, view, savedInstanceState);
        }
    }

    @Override
    public void onResume(Object target) {
        super.onResume(target);
        for (LifecycleDelegate<Object> lifecycleDelegate : lifecycleDelegates) {
            lifecycleDelegate.onResume(target);
        }
    }

    @Override
    public void onPause(Object target) {
        super.onPause(target);
        for (LifecycleDelegate<Object> lifecycleDelegate : lifecycleDelegates) {
            lifecycleDelegate.onPause(target);
        }
    }

    @Override
    public void onSaveView(Object target, Bundle outState) {
        super.onSaveView(target, outState);
        for (LifecycleDelegate<Object> lifecycleDelegate : lifecycleDelegates) {
            lifecycleDelegate.onSaveView(target, outState);
        }
    }

    @Override
    public void onDestroyView(Object target) {
        super.onDestroyView(target);
        for (LifecycleDelegate<Object> lifecycleDelegate : lifecycleDelegates) {
            lifecycleDelegate.onDestroyView(target);
        }
        for (CancellableActionDelegate delegate : delegates) {
            delegate.cancel();
        }
    }


    @Override
    public void registerDelegate(CancellableActionDelegate delegate) {
        this.delegates.add(delegate);
        if (!viewExist) {
            delegate.cancel();
        }
    }

    @Override
    public boolean unregisterDelegate(CancellableActionDelegate delegate) {
        return this.delegates.remove(delegate);
    }

}

@@ -67,16 +67,59 @@
 Bundle;%0A
+import android.support.annotation.NonNull;%0A
 import a
@@ -136,16 +136,16 @@
 w.View;%0A
-
 %0Aimport 
@@ -813,32 +813,41 @@
 fecycleDelegate(
+@NonNull 
 LifecycleDelegat
@@ -1014,32 +1014,32 @@
 ycleDelegate) %7B%0A
-
         return t
@@ -1087,32 +1087,454 @@
 legate);%0A    %7D%0A%0A
+    public LifecycleDelegate getLifecycleDelegate(Class%3C? extends LifecycleDelegate%3E delegateType) %7B%0A        for (LifecycleDelegate delegate : lifecycleDelegates) %7B%0A            if (delegate.getClass().isAssignableFrom(delegateType)) %7B%0A                return delegate;%0A            %7D%0A        %7D%0A        return null;%0A    %7D%0A%0A    public List%3CCancellableActionDelegate%3E getLifecycleDelegates() %7B%0A        return delegates;%0A    %7D%0A%0A
     @Override%0A  

refactor (PandroidDelegate) : add method to manipulate delegates