package org.commcare.views;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;

import org.commcare.dalvik.BuildConfig;
import org.commcare.suite.model.DisplayData;
import org.commcare.utils.MediaUtil;
import org.javarosa.core.services.locale.Localizer;

import java.util.LinkedList;

/**
 * Utilities for converting CommCare UI diplsay details into Android objects
 *
 * @author ctsims
 */
public final class ViewUtil {

    // This is silly and isn't really what we want here, but it's a start.
    // (We'd like to be able to add a displayunit to a menu in a super
    // easy/straightforward way.
    public static void addDisplayToMenu(Context context, Menu menu,
                                        int menuId, int menuGroupId, DisplayData display) {
        MenuItem item = menu.add(menuGroupId, menuId, menuId,
                Localizer.clearArguments(display.getName()).trim());
        if (display.getImageURI() != null) {
            Bitmap b = MediaUtil.inflateDisplayImage(context, display.getImageURI());
            if (b != null) {
                item.setIcon(new BitmapDrawable(context.getResources(), b));
            }
        }
    }

    public static void hideVirtualKeyboard(Activity activity) {
        InputMethodManager inputManager = (InputMethodManager)activity.getSystemService(Context.INPUT_METHOD_SERVICE);

        View focus = activity.getCurrentFocus();
        if (focus != null) {
            inputManager.hideSoftInputFromWindow(focus.getWindowToken(),
                    InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }

    /**
     * Sets the background on a view to the provided drawable while retaining the padding
     * of the original view (regardless of whether the provided drawable has its own padding)
     *
     * @param v          The view whose background will be updated
     * @param background A background drawable (can be null to clear the background)
     */
    public static void setBackgroundRetainPadding(View v, Drawable background) {
        //Need to transplant the padding due to background affecting it
        int[] padding = {v.getPaddingLeft(), v.getPaddingTop(), v.getPaddingRight(), v.getPaddingBottom()};

        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN) {
            v.setBackground(background);
        } else {
            v.setBackgroundDrawable(background);
        }
        v.setPadding(padding[0], padding[1], padding[2], padding[3]);
    }

    /**
     * Debug method to toast a view's ID whenever it is clicked.
     */
    public static void setClickListenersForEverything(Activity act) {
        if (BuildConfig.DEBUG) {
            final ViewGroup layout = (ViewGroup)act.findViewById(android.R.id.content);
            final LinkedList<View> views = new LinkedList<>();
            views.add(layout);
            for (int i = 0; !views.isEmpty(); i++) {
                final View child = views.getFirst();
                views.removeFirst();
                Log.i("GetID", "Adding onClickListener to view " + child);
                child.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(final View v) {
                        String vid;
                        try {
                            vid = "View id is: " + v.getResources().getResourceName(v.getId()) + " ( " + v.getId() + " )";
                        } catch (final Resources.NotFoundException excp) {
                            vid = "View id is: " + v.getId();
                        }
                        Log.i("CLK", vid);
                    }
                });
                if (child instanceof ViewGroup) {
                    final ViewGroup vg = (ViewGroup)child;
                    for (int j = 0; j < vg.getChildCount(); j++) {
                        final View gchild = vg.getChildAt(j);
                        if (!views.contains(gchild)) views.add(gchild);
                    }
                }
            }
        }
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public static int getColorDrawableColor(ColorDrawable drawable) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
            Bitmap bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_4444);
            Canvas canvas = new Canvas(bitmap);
            drawable.draw(canvas);
            int pix = bitmap.getPixel(0, 0);
            bitmap.recycle();
            return pix;
        } else {
            return drawable.getColor();
        }
    }
}

@@ -124,46 +124,8 @@
 xt;%0A
-import android.content.res.Resources;%0A
 impo
@@ -152,16 +152,16 @@
 Bitmap;%0A
+
 import a
@@ -353,33 +353,8 @@
 ld;%0A
-import android.util.Log;%0A
 impo
@@ -435,39 +435,8 @@
 ew;%0A
-import android.view.ViewGroup;%0A
 impo
@@ -488,48 +488,8 @@
 r;%0A%0A
-import org.commcare.dalvik.BuildConfig;%0A
 impo
@@ -623,38 +623,8 @@
 r;%0A%0A
-import java.util.LinkedList;%0A%0A
 /**%0A
@@ -2703,32 +2703,32 @@
 und);%0A        %7D%0A
+
         v.setPad
@@ -2792,1566 +2792,8 @@
  %7D%0A%0A
-    /**%0A     * Debug method to toast a view's ID whenever it is clicked.%0A     */%0A    public static void setClickListenersForEverything(Activity act) %7B%0A        if (BuildConfig.DEBUG) %7B%0A            final ViewGroup layout = (ViewGroup)act.findViewById(android.R.id.content);%0A            final LinkedList%3CView%3E views = new LinkedList%3C%3E();%0A            views.add(layout);%0A            for (int i = 0; !views.isEmpty(); i++) %7B%0A                final View child = views.getFirst();%0A                views.removeFirst();%0A                Log.i(%22GetID%22, %22Adding onClickListener to view %22 + child);%0A                child.setOnClickListener(new View.OnClickListener() %7B%0A                    @Override%0A                    public void onClick(final View v) %7B%0A                        String vid;%0A                        try %7B%0A                            vid = %22View id is: %22 + v.getResources().getResourceName(v.getId()) + %22 ( %22 + v.getId() + %22 )%22;%0A                        %7D catch (final Resources.NotFoundException excp) %7B%0A                            vid = %22View id is: %22 + v.getId();%0A                        %7D%0A                        Log.i(%22CLK%22, vid);%0A                    %7D%0A                %7D);%0A                if (child instanceof ViewGroup) %7B%0A                    final ViewGroup vg = (ViewGroup)child;%0A                    for (int j = 0; j %3C vg.getChildCount(); j++) %7B%0A                        final View gchild = vg.getChildAt(j);%0A                        if (!views.contains(gchild)) views.add(gchild);%0A                    %7D%0A                %7D%0A            %7D%0A        %7D%0A    %7D%0A%0A
     

Remove old debug method