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

import android.app.Activity;
import android.content.Context;
import android.content.ContextWrapper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
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.db.QueryDB;
import com.bonait.bnframework.common.db.mode.BPA_GOODPROPERTY;
import com.bonait.bnframework.common.helper.I.MyClickListener;
import com.bonait.bnframework.common.image.MyBitmapUtils;
import com.bonait.bnframework.common.utils.AlertDialogUtils;
import com.bonait.bnframework.common.utils.KeyboardUtil;
import com.bonait.bnframework.common.utils.ToastUtils;
import com.bonait.bnframework.modules.home.fragment.mode.image_sp;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;

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

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

    private final LayoutInflater mLayoutInflater;

    private Context context;

    List<BPA_GOODPROPERTY> goodproperties = new ArrayList<>();

    /**
     * 点击事件
     */
    public MyClickListener mListener=null;
    private int currentPos=0;

    public zsx_adapter(int currentPos,Context context,List<BPA_GOODPROPERTY> data, MyClickListener listener) {
        this.context = context;
        mListener = listener;
        mLayoutInflater = LayoutInflater.from(context);
        goodproperties=data;
        this.currentPos = currentPos;
    }

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

    }

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

                BPA_GOODPROPERTY goodproperty= goodproperties.get(position);
                MyLCViewHolder1 myViewHolder = (MyLCViewHolder1) holder;
                myViewHolder.root.setOnClickListener(KeyboardUtil::hideKeyboard);
                myViewHolder.edittext.setText(goodproperty.name+"");
                myViewHolder.edittext_wj.setText(goodproperty.foreignKeyRe==null?"":goodproperty.foreignKeyRe);
                myViewHolder.edittext_px.setText(goodproperty.sort+"");
                myViewHolder.save_text.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        String xh=myViewHolder.edittext_px.getText().toString();
                        if(xh==null || xh.isEmpty())
                        {
                            ToastUtils.info("排序不能为空");
                            return;
                        }
                        goodproperty.name=myViewHolder.edittext.getText().toString();
                        goodproperty.foreignKeyRe=myViewHolder.edittext_wj.getText().toString();
                        goodproperty.sort=Integer.parseInt(xh);
                        if (goodproperty.name.isEmpty()) {
                            ToastUtils.info("属性名称不能为空");
                            return;
                        }
                        if(mListener!=null) mListener.clickListenerNew(view,currentPos,goodproperty);
                    }
                });
                myViewHolder.delete_text.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if(mListener!=null) mListener.clickListenerNew(view,currentPos,goodproperty);
                    }
                });
            }
        } 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 goodproperties.size();
    }

    public static class MyLCViewHolder1 extends RecyclerView.ViewHolder {
        EditText edittext;//名称
        EditText edittext_wj;//外键
        EditText edittext_px;//排序
        TextView save_text;
        TextView delete_text;
        RelativeLayout root;
        public MyLCViewHolder1(View view) {
            super(view);
            edittext=view.findViewById(R.id.edittext);
            edittext_wj=view.findViewById(R.id.edittext_wj);
            save_text=view.findViewById(R.id.save_text);
            delete_text=view.findViewById(R.id.delete_text);
            edittext_px=view.findViewById(R.id.edittext_px);
            root=view.findViewById(R.id.root);
        }
    }
}
