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.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
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_GOODPROPERTY;
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.helper.I.MyClickListener;
import com.bonait.bnframework.common.message.MessageManager;
import com.bonait.bnframework.common.utils.ToastUtils;
import com.bonait.bnframework.common.view.MyLayoutManager;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;


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

    private final LayoutInflater mLayoutInflater;

    private Context context;

    List<Object> liaos = new ArrayList<>();

    public liaochang_adapter(Context context,List<Object> data) {
        this.context = context;
        mLayoutInflater = LayoutInflater.from(context);
        liaos=data;
    }

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

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

                lcMode lcMode = (lcMode)liaos.get(position);
                MyLCViewHolder1 myViewHolder = (MyLCViewHolder1) holder;

                myViewHolder.xuhao.setText(lcMode.exp+"");
                myViewHolder.name.setText(lcMode.materialName.isEmpty()?"未设置":(lcMode.materialName+""));

                if(lcMode.materialName.isEmpty() || lcMode.materialName.equals("未设置"))
                {
                    myViewHolder.baifenbi.setText("");
//                    myViewHolder.gaojing.setVisibility(View.GONE);
                    myViewHolder.wuliaoimage.setImageResource(R.mipmap.newsilos);
                }else
                {
                    //总量等于0  雨量小于等于
                    if(lcMode.warningValue>=lcMode.silosmargin || lcMode.siloszl==0)
                    {
//                        myViewHolder.gaojing.setVisibility(View.VISIBLE);
                        myViewHolder.wuliaoimage.setImageResource(R.mipmap.newsilos_yc);
                    }else
                    {
//                        myViewHolder.gaojing.setVisibility(View.GONE);
                        myViewHolder.wuliaoimage.setImageResource(R.mipmap.newsilos);
                    }
                    myViewHolder.baifenbi.setText(get(lcMode.silosmargin,lcMode.siloszl));
                }

                myViewHolder.wuliaoimage.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        MessageManager.getInstance().sendMessage(MessageName.ClickSilos,lcMode);
                    }
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static String get(int up, int down) {
        if (down == 0) {
            return "0%";
        }
        double value = new BigDecimal((float) up / down).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        return new DecimalFormat("#%").format(value);
    }

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

    public static class MyLCViewHolder1 extends RecyclerView.ViewHolder {
        TextView xuhao;
        TextView name;
        ImageView wuliaoimage;

        TextView baifenbi;

//        LinearLayout gaojing;
        public MyLCViewHolder1(View view) {
            super(view);
            xuhao = view.findViewById(R.id.xuhao);//序号
            name = view.findViewById(R.id.wuliaoname);
            wuliaoimage=view.findViewById(R.id.wuliaoimage);
            baifenbi= view.findViewById(R.id.baifenbi);//序号
//            gaojing= view.findViewById(R.id.gaojing);//序号
        }
    }
}


