package com.mobilesolutionworks.android.app.controller;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import com.mobilesolutionworks.android.app.WorkControllerHost;

import java.lang.ref.WeakReference;

/**
 * Controller with host storing feature.
 * <p>
 * In actual development, developer will most likely use this class rather than WorksController.
 * As the host is updated  after device rotation, the controller can consider that the host is
 * always available when making update in {@link #runWhenUiIsReady(Runnable)} scope.
 * <p>
 * Created by yunarta on 15/3/17.
 */

public abstract class HostWorksController<H extends WorkControllerHost> extends WorksController {

    private WeakReference<H> mHost;

    void updateHost(H host) {
        mHost = new WeakReference<>(host);
    }

    @NonNull
    public H getHost() {
        return mHost.get();
    }

    @Override
    public void onCreate(Bundle arguments) {
        super.onCreate(arguments);

        if (mHost == null) {
            throw new IllegalStateException("HostWorksController can only be created using HostWorksController.create function");
        }
    }

    @SuppressWarnings("squid:S1604")
    public static <C extends HostWorksController<H>, H extends WorkControllerHost> C create(@NonNull final H host, int id, @Nullable Bundle args, @NonNull final WorksControllerManager.ControllerCallbacks<C> callback) {
        // SuppressWarnings("squid:S1604")
        C controller = host.getControllerManager().initController(id, args, new WorksControllerManager.ControllerCallbacks<C>() {
            @Override
            public C onCreateController(int id, Bundle args) {
                C c = callback.onCreateController(id, args);
                c.updateHost(host);
                return c;
            }
        });

        controller.updateHost(host);
        return controller;
    }
}

@@ -747,40 +747,1137 @@
     
-private WeakReference%3CH%3E mHost;%0A
+/**%0A     * Hold weak reference to host.%0A     */%0A    private WeakReference%3CH%3E mHost;%0A%0A    @SuppressWarnings(%22squid:S1604%22)%0A    public static %3CC extends HostWorksController%3CH%3E, H extends WorkControllerHost%3E C create(@NonNull%0A                                                                                            final H host, int id, @Nullable Bundle args, @NonNull%0A                                                                                            final WorksControllerManager.ControllerCallbacks%3CC%3E callback) %7B%0A        // SuppressWarnings(%22squid:S1604%22)%0A        C controller = host.getControllerManager().initController(id, args, new WorksControllerManager.ControllerCallbacks%3CC%3E() %7B%0A            @Override%0A            public C onCreateController(int id, Bundle args) %7B%0A                C c = callback.onCreateController(id, args);%0A                c.updateHost(host);%0A                return c;%0A            %7D%0A        %7D);%0A%0A        controller.updateHost(host);%0A        return controller;%0A    %7D%0A%0A    /**%0A     * Update host, called by controller create callback.%0A     *%0A     * @param host host for this controller.%0A     */
 %0A   
@@ -1959,22 +1959,23 @@
 %0A%0A    @N
-onN
 ull
+able
 %0A    pub
@@ -2300,742 +2300,7 @@
    %7D
-%0A%0A    @SuppressWarnings(%22squid:S1604%22)%0A    public static %3CC extends HostWorksController%3CH%3E, H extends WorkControllerHost%3E C create(@NonNull final H host, int id, @Nullable Bundle args, @NonNull final WorksControllerManager.ControllerCallbacks%3CC%3E callback) %7B%0A        // SuppressWarnings(%22squid:S1604%22)%0A        C controller = host.getControllerManager().initController(id, args, new WorksControllerManager.ControllerCallbacks%3CC%3E() %7B%0A            @Override%0A            public C onCreateController(int id, Bundle args) %7B%0A                C c = callback.onCreateController(id, args);%0A                c.updateHost(host);%0A                return c;%0A            %7D%0A        %7D);%0A%0A        controller.updateHost(host);%0A        return controller;%0A    %7D
 %0A%7D%0A

Update javadoc