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

import android.app.Activity;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.DialogInterface;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bonait.bnframework.R;
import com.bonait.bnframework.common.constant.MessageName;
import com.bonait.bnframework.common.db.QueryDB;
import com.bonait.bnframework.common.db.mode.BPA_GOODS;
import com.bonait.bnframework.common.db.mode.BPA_GOODSTYPE;
import com.bonait.bnframework.common.db.mode.BPA_MATERIAL;
import com.bonait.bnframework.common.db.mode.BPA_SILOSANDMATERIAL;
import com.bonait.bnframework.common.db.res.lcMode;
import com.bonait.bnframework.common.glide.GlideUtil;
import com.bonait.bnframework.common.image.MyBitmapUtils;
import com.bonait.bnframework.common.message.MessageManager;
import com.bonait.bnframework.common.model.mode.GoodsAPositionBean;
import com.bonait.bnframework.common.utils.AlertDialogUtils;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;


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

    private final LayoutInflater mLayoutInflater;

    private Context context;

    //图标
    private MyBitmapUtils myBitmapUtils = new MyBitmapUtils();
    private int currentPos;//分类位置

    public List<Object> Data = new ArrayList<>();

    public newgood_adapter(int position,Context context, List<Object> data) {
        this.context = context;
        mLayoutInflater = LayoutInflater.from(context);
        Data = data;
        this.currentPos = position;
    }

    @Override
    public void onViewRecycled(@NonNull RecyclerView.ViewHolder holder) {
        super.onViewRecycled(holder);
        // 回收不可见的图片资源
        ((MyLCViewHolder1)holder).ImageUrl.setImageDrawable(null);
    }

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

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        try {
            if (holder instanceof MyLCViewHolder1) {

                BPA_GOODS goods = (BPA_GOODS) Data.get(position);
                MyLCViewHolder1 myViewHolder = (MyLCViewHolder1) holder;

                myViewHolder.Tag_text.setText(goods.name);
                //设置图片
                if (!goods.url.equals("未知") && !goods.url.isEmpty() &&  !goods.url.equals("")) {
                    myBitmapUtils.disPlay(myViewHolder.ImageUrl, goods.url);
//                    GlideUtil.showImageWithRadius(myViewHolder.ImageUrl.getContext(),goods.url,myViewHolder.ImageUrl,R.mipmap.loading3,10);
                }

                /**
                 * 图片点击后打开
                 */
                myViewHolder.Tag_text.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        MessageManager.getInstance().sendMessage(MessageName.ClickGoodNew,new GoodsAPositionBean(currentPos,goods));
                    }
                });
                myViewHolder.ImageUrl.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        MessageManager.getInstance().sendMessage(MessageName.ClickGoodNew,new GoodsAPositionBean(currentPos,goods));
                    }
                });

                /**
                 * 修改按钮
                 */
                myViewHolder.Sc_text.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        MessageManager.getInstance().sendMessage(MessageName.UpdateGood,new GoodsAPositionBean(currentPos,goods));
                    }
                });

                /**
                 * 删除按钮
                 */
                myViewHolder.delete_text.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        MessageManager.getInstance().sendMessage(MessageName.DeleteGood1,new GoodsAPositionBean(currentPos,goods));

                    }
                });

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

    /**
     * 删除数据
     *
     * @param
     */
    public void Detele(BPA_GOODS goods) {
        QueryDB.DeleteGoods(goods);

        Iterator<Object> it= Data.iterator();
        while (it.hasNext())
        {
            BPA_GOODS s=(BPA_GOODS)it;
            if(s.id.equals(goods.id))
            {
                it.remove();
                notifyDataSetChanged();
            }
        }

    }

    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 Data.size();
    }

    public static class MyLCViewHolder1 extends RecyclerView.ViewHolder {
        ImageView ImageUrl;
        TextView Tag_text;
        TextView Sc_text;
        TextView delete_text;

        public MyLCViewHolder1(View view) {
            super(view);
            ImageUrl = view.findViewById(R.id.ImageUrl);

            Tag_text = view.findViewById(R.id.Tag_text);

            Sc_text = view.findViewById(R.id.Sc_text);
            delete_text = view.findViewById(R.id.delete_text);

        }
    }
}