package ru.ltst.u2020mvp.base.mvp;

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.IdRes;

import java.util.Arrays;
import java.util.HashMap;

import butterknife.ButterKnife;
import ru.ltst.u2020mvp.ui.ActivityHierarchyServer;

import static ru.ltst.u2020mvp.util.Strings.COLON;
import static ru.ltst.u2020mvp.util.Strings.DOT;

public class Registry {
    private static final HashMap<String, Entry> registers = new HashMap<>();
    public static final ActivityHierarchyServer.Empty SERVER = new ActivityHierarchyServer.Empty() {
        @Override
        public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
            final Entry entry = registers.get(getKey(activity));
            if (entry != null && savedInstanceState != null) {
                entry.presenter.onRestore(savedInstanceState);
            }
        }

        @Override
        @SuppressWarnings("unchecked")
        public void onActivityStarted(Activity activity) {
            final Entry entry = registers.get(getKey(activity));
            if (entry != null) {
                final BaseView view = ButterKnife.findById(activity, entry.viewId);
                entry.presenter.takeView(view);
            }
        }

        @Override
        @SuppressWarnings("unchecked")
        public void onActivityStopped(Activity activity) {
            final Entry entry = registers.get(getKey(activity));
            if (entry != null) {
                final BaseView view = ButterKnife.findById(activity, entry.viewId);
                entry.presenter.dropView(view);
            }
        }

        @Override
        public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
            final Entry entry = registers.get(getKey(activity));
            if (entry != null) {
                entry.presenter.onSave(outState);
            }
        }

        @Override
        public void onActivityDestroyed(Activity activity) {
            registers.remove(getKey(activity));
        }
    };

    public static <V extends BaseView> void add(Activity activity, @IdRes int viewId, BasePresenter<V> presenter) {
        registers.put(getKey(activity), new Entry<>(viewId, presenter));
    }

    private static String getKey(Activity activity) {
        StringBuilder builder = new StringBuilder();
        builder.append(activity.getClass().getName());
        final String action = activity.getIntent().getAction();
        if (action != null) {
            builder.append(DOT).append(action);
        }
        final Uri data = activity.getIntent().getData();
        if (data != null) {
            builder.append(DOT).append(data.toString());
        }
        final Bundle extras = activity.getIntent().getExtras();
        if (extras != null) {
            for (String key : extras.keySet()) {
                Object value = extras.get(key);

                String valueString;
                if (value.getClass().isArray()) {
                    valueString = Arrays.toString((Object[]) value);
                } else {
                    valueString = value.toString();
                }

                builder.append(DOT);
                builder.append(key).append(COLON);
                builder.append(valueString);
            }
        }
        return builder.toString();
    }

    private static class Entry<V extends BaseView> {
        @IdRes
        public final int viewId;
        public final BasePresenter<V> presenter;

        public Entry(int viewId, BasePresenter<V> presenter) {
            this.viewId = viewId;
            this.presenter = presenter;
        }
    }
}

@@ -285,16 +285,42 @@
 yServer;
+%0Aimport timber.log.Timber;
 %0A%0Aimport
@@ -742,52 +742,139 @@
 nal 
-Entry entry = registers.get(getKey(activity)
+String key = getKey(activity);%0A            final Entry entry = registers.get(key);%0A            Timber.d(%22%25s onActivityCreated%22, key
 );%0A 
@@ -1161,52 +1161,139 @@
 nal 
-Entry entry = registers.get(getKey(activity)
+String key = getKey(activity);%0A            final Entry entry = registers.get(key);%0A            Timber.d(%22%25s onActivityStarted%22, key
 );%0A 
@@ -1619,52 +1619,139 @@
 nal 
-Entry entry = registers.get(getKey(activity)
+String key = getKey(activity);%0A            final Entry entry = registers.get(key);%0A            Timber.d(%22%25s onActivityStopped%22, key
 );%0A 
@@ -2065,52 +2065,149 @@
 nal 
-Entry entry = registers.get(getKey(activity)
+String key = getKey(activity);%0A            final Entry entry = registers.get(key);%0A            Timber.d(%22%25s onActivitySaveInstanceState%22, key
 );%0A 
@@ -2408,41 +2408,130 @@
     
-registers.remove(getKey(activity)
+final String key = getKey(activity);%0A            Timber.d(%22%25s onActivityDestroyed%22, key);%0A            registers.remove(key
 );%0A 

Add Registry logging