package de.beyondsoft.ownchat.utils.ui;

import android.app.Activity;
import android.content.Context;
import android.graphics.PorterDuff;
import android.graphics.Rect;
import android.os.Build;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.ProgressBar;

import de.beyondsoft.ownchat.R;

public abstract class ViewUtils {

    private ViewUtils() {

    }

    static void setProgressBarColorToOrange(ProgressBar progressBar) {
        setProgressBarColor(progressBar, R.color.colorDefault);
    }

    public static void setProgressBarColor(ProgressBar progressBar, int color) {
        if (progressBar == null) {
            return;
        }

        Context context = progressBar.getContext();

        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            progressBar.getIndeterminateDrawable().mutate().setColorFilter(ContextCompat.getColor(context, color), PorterDuff.Mode.SRC_IN);

            return;
        }

        progressBar.getIndeterminateDrawable().setColorFilter(ContextCompat.getColor(context, color), PorterDuff.Mode.SRC_IN);
    }

    public static void hideKeyboard(Activity activity) {
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
        View view = activity.getCurrentFocus();

        if (view == null) {
            view = new View(activity);
        }

        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }

    public static Rect getRectFromView(View view) {
        Rect outRect = new Rect();
        int[] location = new int[2];

        view.getDrawingRect(outRect);
        view.getLocationOnScreen(location);

        outRect.offset(location[0], location[1]);

        return outRect;
    }

    public static Rect getHitRectFromView(View view) {
        Rect outRect = new Rect();
        int[] location = new int[2];

        view.getHitRect(outRect);
        view.getLocationOnScreen(location);

        outRect.offset(location[0], location[1]);

        return outRect;
    }
}
