package com.casic.dcms.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.casic.dcms.R;
import com.casic.dcms.bean.WellListBean;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;
import com.qmuiteam.qmui.widget.roundwidget.QMUIRoundButton;

import java.util.List;

public class WellListAdapter extends RecyclerView.Adapter<WellListAdapter.ItemViewHolder> {

    private Context context;
    private List<WellListBean.DataBean.RowsBean> dataRows;
    private LayoutInflater layoutInflater;

    public WellListAdapter(Context context, List<WellListBean.DataBean.RowsBean> dataRows) {
        this.context = context;
        this.dataRows = dataRows;
        layoutInflater = LayoutInflater.from(context);
    }

    @NonNull
    @Override
    public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = layoutInflater.inflate(R.layout.item_well_recycleview, parent, false);
        return new ItemViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ItemViewHolder holder, int position) {
        holder.bindView(dataRows.get(position));
        if (buttonOptionListener != null) {
            holder.wellOptionButton.setChangeAlphaWhenPress(true);
            holder.wellOptionButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    buttonOptionListener.onClick(position);
                }
            });
        }
        if (clickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    clickListener.onClick(position);
                }
            });
        }
    }

    private OnOptionClickListener buttonOptionListener;

    public interface OnOptionClickListener {
        void onClick(int position);
    }

    public void setOnOptionClickListener(OnOptionClickListener optionClickListener) {
        this.buttonOptionListener = optionClickListener;
    }

    private OnItemClickListener clickListener;

    public interface OnItemClickListener {
        void onClick(int position);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.clickListener = onItemClickListener;
    }

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

    class ItemViewHolder extends RecyclerView.ViewHolder {

        private TextView wellTypeView;
        private TextView wellNameView;
        private TextView wellCodeView;
        private TextView wellPositionView;
        private TextView wellStatusView;
        private QMUIRoundButton wellOptionButton;

        ItemViewHolder(@NonNull View itemView) {
            super(itemView);
            wellTypeView = itemView.findViewById(R.id.wellTypeView);
            wellNameView = itemView.findViewById(R.id.wellNameView);
            wellCodeView = itemView.findViewById(R.id.wellCodeView);
            wellPositionView = itemView.findViewById(R.id.wellPositionView);
            wellStatusView = itemView.findViewById(R.id.wellStatusView);
            wellOptionButton = itemView.findViewById(R.id.wellOptionButton);
        }

        @SuppressLint("SetTextI18n")
        void bindView(WellListBean.DataBean.RowsBean rowsBean) {
            wellTypeView.setText(rowsBean.getWellTypeName());
            wellNameView.setText(rowsBean.getWellName());
            wellNameView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    QMUITipDialog tipDialog = new QMUITipDialog.Builder(context)
                            .setIconType(QMUITipDialog.Builder.ICON_TYPE_NOTHING)
                            .setTipWord("井深" + rowsBean.getDeep() + "m")
                            .create();
                    tipDialog.show();
                    wellNameView.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            tipDialog.dismiss();
                        }
                    }, 2000);
                }
            });
            wellCodeView.setText(rowsBean.getWellCode());
            wellPositionView.setText(rowsBean.getPosition());
            wellStatusView.setText("已" + rowsBean.getBfztName());
            String bfzt = rowsBean.getBfzt();
            if (bfzt.equals("0")) {
                wellOptionButton.setText("布防");
                wellOptionButton.setBackgroundColor(ContextCompat.getColor(context, R.color.onLineColor));
            } else {
                wellOptionButton.setText("撤防");
                wellOptionButton.setBackgroundColor(ContextCompat.getColor(context, R.color.qmui_config_color_red));
            }
        }
    }
}
