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 com.bonait.bnframework.R;
import com.bonait.bnframework.common.constant.DataBus;
import com.bonait.bnframework.common.db.res.lcMode;
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 lc_adapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private final LayoutInflater mLayoutInflater;

    private Context context;

    List<lcMode> lcModes = DataBus.getInstance().lcModes;

    public lc_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_liaochang, parent, false);
        return new MyLCViewHolder(inflate);

    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        try {
            if (holder instanceof MyLCViewHolder) {
                MyLCViewHolder myViewHolder = (MyLCViewHolder) holder;
                myViewHolder.name.setText(lcModes.get(position).materialName);
                int bfb=0;
                int yl=lcModes.get(position).silosmargin;
                int zl=lcModes.get(position).siloszl;
                if(zl==0)
                {
                    bfb=0;
                }else
                {
                    bfb=((yl*100)/zl)>=100?100:((yl*100)/zl);
                }
                myViewHolder.bfb.setText(bfb+"%");
                myViewHolder.type.setText((lcModes.get(position).materiallx==0?"液料":"酱料"));
                myViewHolder.add.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        //按钮点击
                        String title = "料仓操作提示！";
                        String message = "请问客官确定要将["+lcModes.get(position).materialName+"]加满吗？";
                        AlertDialogUtils.showDialog(context, title, message, new QMUIDialogAction.ActionListener() {
                            @Override
                            public void onClick(QMUIDialog dialog, int index) {
                                DataBus.getInstance().TopUp(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 lcModes.size();
    }

    public static class MyLCViewHolder extends RecyclerView.ViewHolder {
        ImageView add;//加满按钮
        QMUILinkTextView type;//物料类型
        QMUILinkTextView name;//物料名称
        QMUILinkTextView bfb;//料仓余量百分比
        public MyLCViewHolder(View view) {
            super(view);
            add=view.findViewById(R.id.add);
            type=view.findViewById(R.id.type);
            name=view.findViewById(R.id.name);
            bfb=view.findViewById(R.id.bfb);
        }
    }
}
