package com.davidstemmer.screenplay.scene;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;

import flow.Flow;
import mortar.Blueprint;
import mortar.Mortar;
import mortar.MortarScope;

/**
 * Created by weefbellington on 10/20/14.
 */
public abstract class ScopedScene extends StandardScene implements Blueprint {

    private MortarScope scope;

    @Override
    public View setUp(Context context, ViewGroup parent, Flow.Direction direction) {
        return super.setUp(createScope(context), parent, direction);
    }

    @Override
    public View tearDown(Context context, ViewGroup parent, Flow.Direction direction) {
        View destroyed = super.tearDown(context, parent, direction);
        destroyScope(context);
        return destroyed;
    }


    private Context createScope(Context context) {
        MortarScope parentScope = Mortar.getScope(context);
        scope = parentScope.requireChild(this);
        Context childContext = scope.createContext(context);

        Mortar.inject(childContext, this);
        onCreateScope(scope);
        return childContext;
    }

    private void destroyScope(Context context) {
        MortarScope parentScope = Mortar.getScope(context);
        parentScope.destroyChild(scope);
        scope = null;
        onDestroyScope();
    }

    public void onCreateScope(MortarScope scope) {}

    public void onDestroyScope() {}
}

@@ -127,16 +127,65 @@
 Group;%0A%0A
+import java.util.Arrays;%0Aimport java.util.List;%0A%0A
 import f
@@ -267,16 +267,38 @@
 arScope;
+%0Aimport mortar.Scoped;
 %0A%0A/**%0A *
@@ -407,16 +407,24 @@
 plements
+ Scoped,
  Bluepri
@@ -441,16 +441,22 @@
 private 
+final 
 MortarSc
@@ -465,16 +465,308 @@
 e scope;
+%0A    private final Context scopedContext;%0A    private final List modules;%0A%0A    protected ScopedScene(Context context, Object...modules) %7B%0A        this.modules = Arrays.asList(modules);%0A        this.scope = createScope(context);%0A        this.scopedContext = createMortarContext(context);%0A    %7D
 %0A%0A    @O
@@ -885,36 +885,29 @@
 r.setUp(
-createS
+s
 cope
-(c
+dC
 ontext
-)
 , parent
@@ -1094,24 +1094,79 @@
 direction);%0A
+        if (direction != Flow.Direction.FORWARD) %7B%0A    
         dest
@@ -1176,32 +1176,42 @@
 Scope(context);%0A
+        %7D%0A
         return d
@@ -1227,17 +1227,16 @@
 %0A    %7D%0A%0A
-%0A
     priv
@@ -1239,23 +1239,27 @@
 private 
-Context
+MortarScope
  createS
@@ -1350,23 +1350,22 @@
         
-scope =
+return
  parentS
@@ -1385,24 +1385,90 @@
 hild(this);%0A
+    %7D%0A%0A    private Context createMortarContext(Context context) %7B%0A
         Cont
@@ -1516,17 +1516,16 @@
 ntext);%0A
-%0A
         
@@ -1571,27 +1571,27 @@
     
-onCreateScope(scope
+scope.register(this
 );%0A 
@@ -1783,58 +1783,73 @@
     
-    scope = null;%0A        onDestroy
+%7D%0A%0A%0A    @Override%0A    public void onExit
 Scope()
-;
+ %7B%7D%0A
 %0A    
-%7D%0A
+@Override
 %0A   
@@ -1863,22 +1863,21 @@
  void on
-Crea
+En
 te
+r
 Scope(Mo
@@ -1905,38 +1905,183 @@
     
-public void onDestroyScope() %7B
+@Override%0A    public String getMortarScopeName() %7B%0A        return getClass().getName();%0A    %7D%0A%0A    @Override%0A    public Object getDaggerModule() %7B%0A        return modules;%0A    
 %7D%0A%7D%0A

Modify the ScopedScene so that it creates the scope in the constructor and destroys it in tearDown, if the direction is BACK. This allows you to inject dependencies in the scene without relying on a proxy object. Also have it implement the `Scoped` interface.