package com.bonait.bnframework.modules.home.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.ContextWrapper;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.bonait.bnframework.R;
import com.bonait.bnframework.common.constant.DataBus;
import com.bonait.bnframework.common.db.mode.BPA_GOODS;
import com.bonait.bnframework.common.helper.I.MyClickListener;
import com.bonait.bnframework.common.image.MyBitmapUtils;
import com.bonait.bnframework.common.utils.AlertDialogUtils;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;
import com.qmuiteam.qmui.widget.textview.QMUILinkTextView;

import java.util.List;

public class sp_adapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private final LayoutInflater mLayoutInflater;

    private Context context;

    List<BPA_GOODS> goods = DataBus.getInstance().Goods;

    /**
     * 点击事件
     */
    public MyClickListener mListener=null;

    public sp_adapter(Context context) {
        this.context = context;
        mLayoutInflater = LayoutInflater.from(context);
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = mLayoutInflater.inflate(R.layout.item_sp, parent, false);
        return new MyLCViewHolder1(inflate);

    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        try {
            if (holder instanceof MyLCViewHolder1) {
                MyLCViewHolder1 myViewHolder = (MyLCViewHolder1) holder;
                if(goods.get(position).url!=null)
                {
                    new MyBitmapUtils().disPlay(myViewHolder.img,goods.get(position).url);
                }
                //myViewHolder.img.setBackground(context.getResources().getDrawable(R.mipmap.hgr));
                myViewHolder.name.setText(goods.get(position).name);
                myViewHolder.img.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        //按钮点击
                        String title = "选择菜品操作提示！";
                        String message = "请问客官确定要选择["+goods.get(position).name+"],开始制作吗?";
                        AlertDialogUtils.showDialog(context, title, message, new QMUIDialogAction.ActionListener() {
                            @Override
                            public void onClick(QMUIDialog dialog, int index) {
                                if(mListener!=null)
                                {
                                    mListener.clickListener(view,goods.get(position));
                                }
                                dialog.dismiss();
                            }
                        });
                    }
                });

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private Activity findActivity(@NonNull Context context) {
        if (context instanceof Activity) {
            return (Activity) context;
        } else if (context instanceof ContextWrapper) {
            return findActivity(((ContextWrapper) context).getBaseContext());
        } else {
            return null;
        }
    }

    /**
     * 刷新
     *
     * @param
     */
    public void refresh() {
        Activity activity = findActivity(context);
        if (activity != null) {
            activity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    try {
                        notifyDataSetChanged();
                    } catch (Exception e) {

                    }
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return goods.size();
    }

    public static class MyLCViewHolder1 extends RecyclerView.ViewHolder {
        ImageView img;//图片按钮
        QMUILinkTextView name;//名称

        public MyLCViewHolder1(View view) {
            super(view);
            img=view.findViewById(R.id.img);
            name=view.findViewById(R.id.name);
        }
    }
}