package com.tofly.ghrq_route.common.ui.adapter;

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

import androidx.recyclerview.widget.RecyclerView;

import com.lcp.arecyclerview.widget.ARecyclerBaseAdapter;
import com.tofly.ghrq_route.R;
import com.tofly.ghrq_route.module.buildManage.domain.BuildHandleBean;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Amu on 2017/5/22.
 */

public class BuildHandleAdapter extends ARecyclerBaseAdapter<BuildHandleAdapter.MyViewHolder, BuildHandleBean> {
    private Context mContext;

    private List<BuildHandleBean> mData;

    public BuildHandleAdapter(Context context, List<BuildHandleBean> data) {
        super(context, data);
        mContext = context;
        mData = data;
    }

    public void setData(List<BuildHandleBean> data) {
        mData = data;
        notifyDataSetChanged();
    }

    @Override
    public BuildHandleBean getItem(int position) {
        BuildHandleBean handleBean = null;
        if (position < mData.size()){
            handleBean = mData.get(position);
        }
        return handleBean;
    }

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

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View rootView = LayoutInflater.from(mContext).inflate(R.layout.listview_build_handle_item, parent, false);
        MyViewHolder viewHolder = new MyViewHolder(rootView);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, int position) {
        String[] checkStates = mContext.getResources().getStringArray(R.array.handle_check_state);
        BuildHandleBean buildHandle = getItem(position);
        if (buildHandle != null) {
            holder.textHandleApplier.setText(buildHandle.getHandle_username());
            holder.textHandleTime.setText(buildHandle.getHandle_time());
            int state = buildHandle.getCheck_state();
            if (state < checkStates.length){
                holder.textCheckState.setText(checkStates[state]);
            }
            String handleNote = buildHandle.getHandle_note();
            if (handleNote == null || handleNote.isEmpty()) {
                handleNote = mContext.getString(R.string.reported_detail_no_content);
            }
            holder.textHandleNote.setText(handleNote);
            if (onItemClickListener != null) {
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        onItemClickListener.onItemClick(holder.itemView, holder.getLayoutPosition());
                    }
                });
            }
        }
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.image_report_icon)
        ImageView imageReportIcon;
        @BindView(R.id.text_handle_applier)
        TextView textHandleApplier;
        @BindView(R.id.text_handle_time)
        TextView textHandleTime;
        @BindView(R.id.text_check_state)
        TextView textCheckState;
        @BindView(R.id.text_handle_note)
        TextView textHandleNote;

        MyViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
