package com.leroymerlin.pandroid.mvp;

import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.CallSuper;
import android.support.annotation.CheckResult;
import android.support.annotation.Nullable;
import android.view.View;

import com.leroymerlin.pandroid.annotations.RxWrapper;
import com.leroymerlin.pandroid.app.delegate.PandroidDelegate;
import com.leroymerlin.pandroid.app.delegate.impl.AutoBinderLifecycleDelegate;
import com.leroymerlin.pandroid.app.delegate.rx.RxLifecycleDelegate;

import java.lang.ref.WeakReference;

/**
 * Created by Mehdi on 07/11/2016.
 */

@RxWrapper
public class Presenter<T> extends PandroidDelegate<T> {

    protected WeakReference<T> targetView;

    public Presenter() {
    }

    protected void initNestedLifecycleDelegate() {
        addLifecycleDelegate(new RxLifecycleDelegate());
        addLifecycleDelegate(new AutoBinderLifecycleDelegate());
    }

    @CallSuper
    @Override
    public void onInit(T target) {
        super.onInit((T) this);
        initNestedLifecycleDelegate();
        targetView = new WeakReference<T>(target);
    }

    @Nullable
    protected Context getContext() {
        T view = getView();
        if (view instanceof Context) {
            return (Context) view;
        } else if (view instanceof Fragment) {
            return ((Fragment) view).getActivity();
        } else {
            return null;
        }
    }

    @Override
    public void onCreateView(T target, View view, Bundle savedInstanceState) {
        super.onCreateView((T) this, view, savedInstanceState);
    }

    @Override
    public void onResume(T target) {
        super.onResume((T) this);
    }

    @Override
    public void onPause(T object) {
        super.onPause((T) this);
    }

    @Override
    public void onSaveView(T target, Bundle outState) {
        super.onSaveView((T) this, outState);
    }

    @Override
    public void onDestroyView(T target) {
        super.onDestroyView((T) this);
    }

    @Override
    public void onRemove(T target) {
        super.onRemove((T) this);
        targetView = null;
    }

    @Nullable
    public T getView() {
        if(targetView != null){
            return targetView.get();
        }
        return null;
    }

}

@@ -630,16 +630,47 @@
 6.%0A */%0A%0A
+@SuppressWarnings(%22unchecked%22)%0A
 @RxWrapp
@@ -672,16 +672,16 @@
 Wrapper%0A
-
 public c
@@ -1021,24 +1021,30 @@
 %0A    public 
+final 
 void onInit(
@@ -1130,59 +1130,8 @@
 ();%0A
-        targetView = new WeakReference%3CT%3E(target);%0A
     
@@ -1529,32 +1529,82 @@
 nstanceState) %7B%0A
+        targetView = new WeakReference%3C%3E(target);%0A
         super.on
@@ -2011,32 +2011,60 @@
 iew(T target) %7B%0A
+        targetView.clear();%0A
         super.on
@@ -2136,32 +2136,59 @@
 ove(T target) %7B%0A
+        targetView = null;%0A
         super.on
@@ -2209,35 +2209,8 @@
 s);%0A
-        targetView = null;%0A
     

remove target view when view destroyed