package com.casic.dcms.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

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

import com.casic.dcms.R;
import com.casic.dcms.bean.MaintainRecordBean;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;

import java.util.List;

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

    private Context context;
    private List<MaintainRecordBean.DataBean.RowsBean> dataRows;
    private LayoutInflater layoutInflater;
    private QMUITipDialog.Builder dialogBuilder;

    public MaintainRecordAdapter(Context context, List<MaintainRecordBean.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_maintain_recycleview, parent, false);
        return new ItemViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ItemViewHolder holder, int position) {
        holder.bindView(dataRows.get(position));
    }

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

    class ItemViewHolder extends RecyclerView.ViewHolder {

        private TextView bridgeNameView;
        private ImageView descriptionImageView;
        private TextView maintainContentView;
        private TextView maintainTimeView;

        ItemViewHolder(@NonNull View itemView) {
            super(itemView);
            bridgeNameView = itemView.findViewById(R.id.bridgeNameView);
            descriptionImageView = itemView.findViewById(R.id.descriptionImageView);
            maintainContentView = itemView.findViewById(R.id.maintainContentView);
            maintainTimeView = itemView.findViewById(R.id.maintainTimeView);
        }

        void bindView(MaintainRecordBean.DataBean.RowsBean rowsBean) {
            bridgeNameView.setText(rowsBean.getBridgeName());
            String description = rowsBean.getDescription();
            if (TextUtils.isEmpty(description)) {
                descriptionImageView.setVisibility(View.GONE);
            } else {
                descriptionImageView.setVisibility(View.VISIBLE);
                descriptionImageView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        QMUITipDialog tipDialog = new QMUITipDialog.Builder(context)
                                .setIconType(QMUITipDialog.Builder.ICON_TYPE_NOTHING)
                                .setTipWord(description)
                                .create();
                        tipDialog.show();
                        descriptionImageView.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                tipDialog.dismiss();
                            }
                        }, 2000);
                    }
                });
            }
            maintainContentView.setText(rowsBean.getContent());
            maintainTimeView.setText(rowsBean.getMaintainTimeStr());
        }
    }
}
