package com.tofly.ghrq_route.module.trouble.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.LinearLayout;
import android.widget.TextView;

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

import com.lcp.arecyclerview.widget.ARecyclerBaseAdapter;
import com.tofly.ghrq_route.R;
import com.tofly.ghrq_route.module.notifyManage.NotifyManageBean;
import com.tofly.ghrq_route.module.trouble.TroubleTool;
import com.tofly.ghrq_route.module.trouble.domain.TroubleBean;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

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

public class TroubleListAdapter extends ARecyclerBaseAdapter<TroubleListAdapter.MyViewHolder, TroubleBean> {
    private int select_state;
    private TroubleTool troubleTool;

    public TroubleListAdapter(Context context, List<TroubleBean> data) {
        super(context, data);
        troubleTool = TroubleTool.getInstance();
        select_state = 0;
    }

    public void setData(List<TroubleBean> data, int select_state) {
        this.select_state = select_state;
        super.setData(data);
    }

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

    @Override
    public void onBindViewHolder(@NonNull final MyViewHolder holder, int position) {
        TroubleBean troubleBean = getItem(position);
        if (troubleBean != null) {
            holder.textJobTitle.setText(troubleBean.getTroubleName());
            holder.textJobContent.setText(troubleBean.getTroubleInfo());
            holder.imageJobState.setVisibility(View.GONE);
            holder.textJobState.setVisibility(View.GONE);
            String create_time = troubleBean.getFindTime();
            if (create_time != null) {
                holder.textCreateTime.setText(create_time);
            }

            holder.imageNewJob.setVisibility(View.GONE);
            NotifyManageBean msgBean = troubleTool.getNotifyMsgBean(troubleBean.getId());
            if (msgBean != null) {
                boolean isRead = msgBean.getIsRead();
                holder.imageNewJob.setVisibility(isRead? View.GONE : View.VISIBLE);
            }

            if (onItemClickListener != null) {
                holder.itemView.setOnClickListener(v ->
                        onItemClickListener.onItemClick(holder.itemView, holder.getLayoutPosition()));
            }
        }
    }

    static class MyViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.text_job_title)
        TextView textJobTitle;
        @BindView(R.id.layout_job_status)
        LinearLayout layoutJobStatus;
        @BindView(R.id.image_job_state)
        ImageView imageJobState;
        @BindView(R.id.text_job_state)
        TextView textJobState;
        @BindView(R.id.text_create_time)
        TextView textCreateTime;
        @BindView(R.id.text_job_content)
        TextView textJobContent;
        @BindView(R.id.image_new_job)
        ImageView imageNewJob;

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