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.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.lcp.arecyclerview.widget.ARecyclerBaseAdapter;
import com.tofly.ghrq_route.R;
import com.tofly.ghrq_route.module.trouble.domain.TroubleBean;
import com.tofly.ghrq_route.module.trouble.domain.TroublePlanRecordBean;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

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

public class PlanTroubleRecordAdapter extends ARecyclerBaseAdapter<PlanTroubleRecordAdapter.MyViewHolder, TroublePlanRecordBean> {

    private Context mContext;

    private List<TroublePlanRecordBean> mData;

    private boolean mbIsClear;
    String[] clearCheckStates;

    public PlanTroubleRecordAdapter(Context context, List<TroublePlanRecordBean> data, boolean isClear) {
        super(context, data);
        mContext = context;
        mData = data;
        mbIsClear = isClear;
        clearCheckStates = mContext.getResources().getStringArray(R.array.trouble_clear_check_state);
    }

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

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

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

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

    @Override
    public void onBindViewHolder(final MyViewHolder holder, int position) {
        TroublePlanRecordBean planRecord = getItem(position);
        if (planRecord != null) {
            String username = planRecord.getCreate_username();
            holder.textReporter.setText(username);
            holder.textReportTime.setText(planRecord.getCreate_time());
            String notes = planRecord.getNotes();
            if (notes == null || notes.isEmpty())
                notes = "无备注";
            holder.textReportNotes.setText(notes);
            if (onItemClickListener != null){
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        onItemClickListener.onItemClick(holder.itemView, holder.getLayoutPosition());
                    }
                });
            }
            holder.textUploadState.setVisibility(View.GONE);
            if (planRecord.getStatus() == TroubleBean.SAVED_STATE
                    || planRecord.getStatus() == TroubleBean.CLEAR_SAVED_STATE){
                holder.textUploadState.setVisibility(View.VISIBLE);
                String localState = mContext.getResources().getString(R.string.trouble_record_unupload);
                holder.textUploadState.setText(localState);
            }else {
                /*if (mbIsClear){
                    int check_state = planRecord.getCheck_state();
                    if (check_state >= 0 && check_state < clearCheckStates.length){
                        holder.textUploadState.setVisibility(View.VISIBLE);
                        holder.textUploadState.setText(clearCheckStates[check_state]);
                    }

                }*/
            }
        }
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.text_report_state)
        TextView textReporter;
        @BindView(R.id.text_upload_state)
        TextView textUploadState;
        @BindView(R.id.text_report_time)
        TextView textReportTime;
        @BindView(R.id.text_report_addr)
        TextView textReportNotes;

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