package com.datarecovery.master.utils.bindingadapters;

import android.content.res.ColorStateList;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.view.TouchDelegate;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.DecelerateInterpolator;

import androidx.annotation.ColorInt;
import androidx.annotation.DrawableRes;
import androidx.databinding.BindingAdapter;

import com.atmob.common.logging.AtmobLog;
import com.atmob.common.ui.SizeUtil;

import java.util.concurrent.TimeUnit;

import atmob.reactivex.rxjava3.disposables.Disposable;
import atmob.rxjava.utils.RxJavaUtil;


public class ViewBindingAdapter {
    private static final long ClickThrottleWindowDuration = 600; // In milliseconds

    @BindingAdapter(value = {"android:onClickListener"})
    public static void setClickListener(View view, View.OnClickListener clickListener) {
        view.setClickable(true);
        Disposable ignore = RxJavaUtil.click(view)
                .throttleFirst(ClickThrottleWindowDuration, TimeUnit.MILLISECONDS)
                .subscribe(unused -> {
                    if (clickListener != null) {
                        clickListener.onClick(view);
                    }
                }, Throwable::printStackTrace);
    }

    @BindingAdapter(value = {"android:onClick"})
    public static void setOnClick(View view, View.OnClickListener clickListener) {
        view.setClickable(true);
        Disposable ignore = RxJavaUtil.click(view)
                .throttleFirst(ClickThrottleWindowDuration, TimeUnit.MILLISECONDS)
                .subscribe(unused -> {
                    if (clickListener != null) {
                        clickListener.onClick(view);
                    }
                }, Throwable::printStackTrace);
    }

    @BindingAdapter(value = {"android:onLongClickListener", "android:longClickable"}, requireAll = false)
    public static void setOnLongClickListener(View view, View.OnLongClickListener clickListener,
                                              boolean clickable) {
        view.setLongClickable(clickable);
        Disposable ignore = RxJavaUtil.longClick(view)
                .throttleFirst(ClickThrottleWindowDuration, TimeUnit.MILLISECONDS)
                .subscribe(unused -> {
                    if (clickListener != null) {
                        clickListener.onLongClick(view);
                    }
                }, Throwable::printStackTrace);
    }

    @BindingAdapter(value = {"android:onLongClick", "android:longClickable"}, requireAll = false)
    public static void setOnLongClick(View view, View.OnLongClickListener clickListener,
                                      boolean clickable) {
        view.setLongClickable(clickable);
        Disposable ignore = RxJavaUtil.longClick(view)
                .throttleFirst(ClickThrottleWindowDuration, TimeUnit.MILLISECONDS)
                .subscribe(unused -> {
                    if (clickListener != null) {
                        clickListener.onLongClick(view);
                    }
                }, Throwable::printStackTrace);
    }

    @BindingAdapter({"isInvisible"})
    public static void setIsInvisible(View view, boolean isInvisible) {
        view.setVisibility(isInvisible ? View.INVISIBLE : View.VISIBLE);
    }

    @BindingAdapter({"isGone"})
    public static void setIsGone(View view, boolean isGone) {
        view.setVisibility(isGone ? View.GONE : View.VISIBLE);
    }

    @BindingAdapter({"backgroundRes"})
    public static void setBackgroundRes(View view, @DrawableRes int backgroundRes) {
        view.setBackgroundResource(backgroundRes);
    }

    @BindingAdapter({"backgroundDraw"})
    public static void setBackgroundDrawable(View view, Drawable drawable) {
        view.setBackground(drawable);
    }

    /**
     * {@link View#getBackground()}不为空时才生效
     */
    @BindingAdapter({"backgroundTint"})
    public static void setBackgroundTint(View view, @ColorInt int backgroundTint) {
        view.setBackgroundTintList(ColorStateList.valueOf(backgroundTint));
    }

    @BindingAdapter("android:layout_width")
    public static void setLayoutWidth(View view, float width) {
        ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
        layoutParams.width = (int) width;
        view.setLayoutParams(layoutParams);
    }

    @BindingAdapter("android:layout_height")
    public static void setLayoutHeight(View view, float height) {
        ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
        layoutParams.height = (int) height;
        view.setLayoutParams(layoutParams);
    }

    @BindingAdapter("expandTouchSize")
    public static void touchDelegate(View view, int expandTouchSize) {
        ViewGroup parent = view.getParent() instanceof ViewGroup ? ((ViewGroup) view.getParent()) : null;
        if (parent == null) {
            return;
        }
        TouchDelegate touchDelegate = parent.getTouchDelegate();
        if (touchDelegate != null) {
            AtmobLog.w("View", "touchDelegate is not null");
        }
        parent.post(() -> {
            float px = SizeUtil.dp2px(expandTouchSize);
            Rect rect = new Rect();
            view.getHitRect(rect);
            rect.top -= px;
            rect.bottom += px;
            rect.left -= px;
            rect.right += px;
            parent.setTouchDelegate(new TouchDelegate(rect, view));
        });
    }

    @BindingAdapter(value = {"scaleX", "scaleY", "pivotXPercent", "pivotYPercent"})
    public static void setScale(View view, final float scaleX, final float scaleY, final float pivotXPercent, final float pivotYPercent) {
        view.post(() -> {
            view.setPivotX(Math.max(pivotXPercent, 0) * view.getWidth());
            view.setPivotY(Math.max(pivotYPercent, 0) * view.getHeight());
            view.animate()
                    .scaleX(scaleX < 0f ? 1f : scaleX)
                    .scaleY(scaleY < 0f ? 1f : scaleY)
                    .setDuration(150)
                    .setInterpolator(new DecelerateInterpolator())
                    .start();
        });
    }

}