package com.datarecovery.master.utils.bindingadapters;

import android.content.Context;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.widget.TextView;

import androidx.annotation.ColorRes;
import androidx.core.content.res.ResourcesCompat;
import androidx.databinding.BindingAdapter;


public class TextViewBindingAdapter {


    @BindingAdapter({"assetFontFile"})
    public static void setAssetFontFile(TextView textView, String assetFontFile) {
        Context context = textView.getContext();
        AssetManager assets = context.getAssets();
        Typeface typeface = Typeface.createFromAsset(assets, assetFontFile);
        textView.setTypeface(typeface);
    }

    @BindingAdapter({"textColorRes"})
    public static void setTextColor(TextView textView, @ColorRes int colorRes) {
        Resources resources = textView.getResources();
        try {
            int color = ResourcesCompat.getColor(resources, colorRes, null);
            textView.setTextColor(color);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @BindingAdapter({"textColorResource"})
    public static void setTextColorResource(TextView textView, int colorRes) {
        try {
            textView.setTextColor(colorRes);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @BindingAdapter(value = {"drawableEnd", "drawableStart", "drawableTop", "drawableBoattrs.xmlttom"}, requireAll = false)
    public static void setDrawableEnd(
            TextView textView,
            Drawable drawableEnd,
            Drawable drawableStart,
            Drawable drawableTop,
            Drawable drawableBottom
    ) {
        textView.setCompoundDrawablesWithIntrinsicBounds(drawableStart, drawableTop, drawableEnd, drawableBottom);
    }

    @BindingAdapter(value = {"isBold"})
    public static void setBold(TextView textView, boolean isBold) {
        if (isBold) {
            textView.setTypeface(null, Typeface.BOLD);
        } else {
            textView.setTypeface(null, Typeface.NORMAL);
        }
    }

    @BindingAdapter(value = {"deleteLine"})
    public static void setDeleteLine(TextView textView, boolean isDeleteLine) {
        if (isDeleteLine) {
            textView.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);
        } else {
            textView.setPaintFlags(textView.getPaintFlags() & (~Paint.STRIKE_THRU_TEXT_FLAG));
        }
    }
}