package com.xtremelabs.robolectric.shadows;

import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.view.View;
import com.xtremelabs.robolectric.internal.Implementation;
import com.xtremelabs.robolectric.internal.Implements;
import com.xtremelabs.robolectric.internal.RealObject;

@Implements(Fragment.class)
public class ShadowFragment {
    @RealObject
    protected Fragment realFragment;

    protected View view;
    protected FragmentActivity activity;
    private String tag;
    private Bundle savedInstanceState;
    private int containerViewId;
    private boolean shouldReplace;
    private Bundle arguments;
    private boolean attached;
    private boolean hidden;
    private boolean added;

    public void setView(View view) {
        this.view = view;
    }

    public void setActivity(FragmentActivity activity) {
        this.activity = activity;
    }

    @Implementation
    public View getView() {
        return view;
    }

    @Implementation
    public FragmentActivity getActivity() {
        return activity;
    }

    @Implementation
    public void startActivity(Intent intent) {
        new FragmentActivity().startActivity(intent);
    }

    @Implementation
    public void startActivityForResult(Intent intent, int requestCode) {
        activity.startActivityForResult(intent, requestCode);
    }

    @Implementation
    final public FragmentManager getFragmentManager() {
        return activity.getSupportFragmentManager();
    }

    @Implementation
    public String getTag() {
        return tag;
    }

    @Implementation
    public Resources getResources() {
        if (activity == null) {
            throw new IllegalStateException("Fragment " + this + " not attached to Activity");
        }
        return activity.getResources();
    }

    public void setTag(String tag) {
        this.tag = tag;
    }

    public void setSavedInstanceState(Bundle savedInstanceState) {
        this.savedInstanceState = savedInstanceState;
    }

    public Bundle getSavedInstanceState() {
        return savedInstanceState;
    }

    public void setContainerViewId(int containerViewId) {
        this.containerViewId = containerViewId;
    }

    public int getContainerViewId() {
        return containerViewId;
    }

    public void setShouldReplace(boolean shouldReplace) {
        this.shouldReplace = shouldReplace;
    }

    public boolean getShouldReplace() {
        return shouldReplace;
    }

    @Implementation
    public Bundle getArguments() {
        return arguments;
    }

    @Implementation
    public void setArguments(Bundle arguments) {
        this.arguments = arguments;
    }

    @Implementation
    public final String getString(int resId) {
        if (activity == null) {
          throw new IllegalStateException("Fragment " + this + " not attached to Activity");
        }
        return activity.getString(resId);
    }

    @Implementation
    public final String getString(int resId, Object... formatArgs) {
        if (activity == null) {
          throw new IllegalStateException("Fragment " + this + " not attached to Activity");
        }
        return activity.getString(resId, formatArgs);
    }

    public void setAttached(boolean isAttached) {
        attached = isAttached;
    }

    public boolean isAttached() {
        return attached;
    }

    public void setHidden(boolean isHidden) {
        hidden = isHidden;
    }

    public void setAdded(boolean isAdded) {
        added = isAdded;
    }

    @Implementation
    public boolean isAdded() {
        return getActivity() != null && added;
    }

    @Implementation
    public boolean isHidden() {
        return hidden;
    }

    @Implementation
    public boolean isVisible() {
        return isAdded() && !isHidden() && getView() != null
                && getView().getVisibility() == View.VISIBLE;
    }
}

@@ -1543,22 +1543,16 @@
 ion%0A    
-final 
 public F
@@ -1641,32 +1641,256 @@
 nager();%0A    %7D%0A%0A
+    @Implementation%0A    public FragmentManager getChildFragmentManager() %7B%0A        // Doesn't follow support library behavior, but is correct enough%0A        // for robolectric v1.%0A        return getFragmentManager();%0A    %7D%0A%0A
     @Implementat

Add getChildFragmentManager to robov1.