package com.dspread.pos.common.adapters;

import android.graphics.Bitmap;
import android.graphics.Color;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.ColorInt;
import androidx.annotation.DrawableRes;
import androidx.databinding.BindingAdapter;
import androidx.databinding.ObservableField;

import com.dspread.pos.utils.ImageUtils;
import com.dspread.pos.utils.PosParameters;
import com.dspread.pos_android_app.R;
import com.google.android.material.navigation.NavigationView;

public class BindingAdapters {

    @BindingAdapter("logoImage")
    public static void setLogoImage(ImageView view, @DrawableRes int resId) {
        if (resId != 0) {
            view.setImageResource(resId);
        }
    }

    @BindingAdapter("logoImage")
    public static void setLogoImage(ImageView view, Object image) {
        if (image instanceof Bitmap) {
            view.setImageBitmap((Bitmap) image);
        } else if (image instanceof Integer) {
            view.setImageResource((Integer) image);
        } else {
            view.setImageResource(R.drawable.am_mulberry_logo_wide_color);
        }
    }

    @BindingAdapter("android:visibility")
    public static void setVisibility(View view, Boolean visible) {
        view.setVisibility(visible ? View.VISIBLE : View.GONE);
    }

    @BindingAdapter("android:textColor")
    public static void setTextColor(TextView view, String colorHex) {
        try {
            view.setTextColor(Color.parseColor(colorHex));
        } catch (IllegalArgumentException e) {
            // Use default color if parsing fails
            view.setTextColor(Color.BLACK);
        }
    }

    @BindingAdapter("android:background")
    public static void setBackgroundColor(View view, String colorHex) {
        try {
            view.setBackgroundColor(Color.parseColor(colorHex));
        } catch (IllegalArgumentException e) {
            // Use default color if parsing fails
            view.setBackgroundColor(Color.TRANSPARENT);
        }
    }


    // Main image binding adapter for Mulberry items
    @BindingAdapter("android:src")
    public static void setImageSource(ImageView imageView, Object icon) {
        if (icon == null) {
            imageView.setImageDrawable(null);
            return;
        }

        try {
            if (icon instanceof Integer) {
                // Handle resource ID
                imageView.setImageResource((Integer) icon);
            } else if (icon instanceof Bitmap) {
                // Handle Bitmap directly
                imageView.setImageBitmap((Bitmap) icon);
            } else if (icon instanceof String) {
                // Handle string paths
                String path = (String) icon;
                if (path.startsWith("/")) {
                    // File path
                    Bitmap bitmap = ImageUtils.loadBitmapFromFile(path);
                    if (bitmap != null) {
                        imageView.setImageBitmap(bitmap);
                    }
                } else {
                    // Try to parse as resource name
                    try {
                        @DrawableRes int resId = imageView.getResources()
                                .getIdentifier(path, "drawable", imageView.getContext().getPackageName());
                        if (resId != 0) {
                            imageView.setImageResource(resId);
                        }
                    } catch (Exception e) {
                        // Fall through to null
                    }
                }
            }
        } catch (Exception e) {
            imageView.setImageDrawable(null);
        }
    }

    // Additional adapter for ObservableField if needed
    @BindingAdapter("android:src")
    public static void setImageSource(ImageView imageView, ObservableField<Object> iconField) {
        if (iconField != null) {
            setImageSource(imageView, iconField.get());
        } else {
            setImageSource(imageView, null);
        }
    }


    @BindingAdapter("imageBitmap")
    public static void setImageBitmap(ImageView view, Bitmap bitmap) {
        if (bitmap != null) {
            view.setImageBitmap(bitmap);
        }
    }


}