package com.pixable.pixalytics.core.trace;

import android.app.Service;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.pixable.pixalytics.core.R;
import com.pixable.pixalytics.core.platform.Platform;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

public class ToastTraceProxy implements TraceProxy {

    final Map<Level, Integer> durationMap = new HashMap<Level, Integer>() {{
        put(Level.DEBUG, Toast.LENGTH_SHORT);
        put(Level.INFO, Toast.LENGTH_LONG);
    }};
    /**
     * Read always through {@link #getBackgroundColorMap}
     */
    final Map<Level, Integer> backgroundColorMap = new HashMap<>();

    private Map<Level, Integer> getBackgroundColorMap() {
        if (backgroundColorMap.isEmpty()) {
            backgroundColorMap.put(Level.DEBUG, R.color.toast_debug);
            backgroundColorMap.put(Level.INFO, R.color.toast_info);
        }

        return backgroundColorMap;
    }

    @Override
    public void traceMessage(Context context, Level level, String messageTitle, Map<String, Object> properties, Collection<Platform> platforms) {
        final LayoutInflater inflater =
                (LayoutInflater) context.getSystemService(Service.LAYOUT_INFLATER_SERVICE);
        final View layout = inflater.inflate(R.layout.pixalytics_tracking_debug_toast, null);

        final Toast toast = new Toast(context);
        //noinspection ResourceType
        toast.setDuration(durationMap.get(level));
        toast.setGravity(Gravity.START | Gravity.BOTTOM,
                context.getResources().getInteger(R.integer.tracking_toast_margin),
                context.getResources().getInteger(R.integer.tracking_toast_margin));
        layout.findViewById(R.id.pixalytics_toast_container).setBackgroundColor(
                context.getResources().getColor(getBackgroundColorMap().get(level)));

        ((TextView) layout.findViewById(R.id.pixalytics_toast_title)).setText(messageTitle);
        ((TextView) layout.findViewById(R.id.pixalytics_toast_parameters)).setText(
                mapToLinedString(properties));

        LinearLayout platformsList = (LinearLayout) layout.findViewById(R.id.pixalytics_platforms_list);
        for (Platform platform : platforms) {
            ImageView platformIcon = new ImageView(context);
            platformIcon.setPadding(1, 1, 1, 1);
            platformIcon.setImageResource(platform.getIconId());
            platformsList.addView(platformIcon);
            // ButterKnife.findById(layout, platform.getIconId()).setVisibility(View.VISIBLE);
        }

        toast.setView(layout);

        toast.show();
    }

    private static String mapToLinedString(Map<String, Object> map) {
        final StringBuilder builder = new StringBuilder();
        final Map<String, Object> sortedMap = new TreeMap<>(map);
        for (Map.Entry<String, Object> entry : sortedMap.entrySet()) {
            builder.append("- ");
            builder.append(entry.getKey());
            builder.append(": ");
            builder.append(entry.getValue().toString());
            builder.append('\n');
        }
        return (builder.length() > 0) ? builder.substring(0, builder.length() - 1) : "";
    }
}

@@ -37,16 +37,45 @@
 trace;%0A%0A
+import android.app.Activity;%0A
 import a
@@ -126,16 +126,67 @@
 ontext;%0A
+import android.os.Looper;%0Aimport android.util.Log;%0A
 import a
@@ -648,16 +648,109 @@
 eProxy %7B
+%0A    private static final String TAG = %22Pixalytics-%22 + ToastTraceProxy.class.getSimpleName();
 %0A%0A    fi
@@ -1397,16 +1397,22 @@
 Message(
+final 
 Context 
@@ -1432,16 +1432,22 @@
 l level,
+ final
  String 
@@ -1491,16 +1491,1236 @@
 perties,
+ final Collection%3CPlatform%3E platforms) %7B%0A        final int color = context.getResources().getColor(getBackgroundColorMap().get(level));%0A        final Integer duration = durationMap.get(level);%0A        final String messageBody = mapToLinedString(properties);%0A%0A        if (Looper.getMainLooper().getThread() == Thread.currentThread()) %7B%0A            // We're in the UI thread, all fine%0A            displayToast(context, color, duration, messageTitle, messageBody, platforms);%0A        %7D else if (context instanceof Activity) %7B%0A            // Well, even if we were not called in the UI thread, let's try to fix it%0A            ((Activity) context).runOnUiThread(new Runnable() %7B%0A                @Override%0A                public void run() %7B%0A                    displayToast(context, color, duration, messageTitle, messageBody, platforms);%0A                %7D%0A            %7D);%0A        %7D else %7B%0A            // Nop, no way we can show a toast%0A            Log.w(TAG, %22Can't create the toast for the message %5C%22%22 + messageTitle + %22%5C%22: we're not in the UI thread%22);%0A        %7D%0A    %7D%0A%0A    /** Must be run in the UI thread. */%0A    private void displayToast(Context context, int color, int duration, String messageTitle, String messageBody,
  Collect
@@ -3091,30 +3091,16 @@
 duration
-Map.get(level)
 );%0A     
@@ -3423,73 +3423,11 @@
   co
-ntext.getResources().getColor(getBackgroundColorMap().get(level))
+lor
 );%0A%0A
@@ -3606,53 +3606,19 @@
 ext(
-%0A                mapToLinedString(properties)
+messageBody
 );%0A%0A
@@ -3996,103 +3996,8 @@
 n);%0A
-            // ButterKnife.findById(layout, platform.getIconId()).setVisibility(View.VISIBLE);%0A
     

Allow to toast-trace from background threads