package com.tofly.ghrq_route.module.survey.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.survey.SurveyTool;
import com.tofly.ghrq_route.module.survey.domain.SurveyBean;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

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

public class SurveyAdapter extends ARecyclerBaseAdapter<SurveyAdapter.MyViewHolder, SurveyBean> {
    private int select_state;
    private SurveyTool surveyTool;
    private OnItemClickListener onOverSignClickListener;

    public void setOnOverSignClickListener(OnItemClickListener onOverSignClickListener) {
        this.onOverSignClickListener = onOverSignClickListener;
    }

    public SurveyAdapter(Context context, List<SurveyBean> data) {
        super(context, data);
        surveyTool = SurveyTool.getInstance();
        select_state = 0;
    }

    public SurveyAdapter(Context context, List<SurveyBean> data, int select_state) {
        super(context, data);
        surveyTool = SurveyTool.getInstance();
        this.select_state = select_state;
    }

    public void setData(List<SurveyBean> 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) {
        SurveyBean surveyBean = getItem(position);
        if (surveyBean != null) {
            holder.textJobTitle.setText(surveyBean.getName());
            holder.textJobContent.setText(surveyBean.getBuriedAddress());
            String create_time = surveyBean.getCreateTime();
            if (create_time != null) {
                holder.textCreateTime.setText(create_time);
            }

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

            holder.layoutJobStatus.setVisibility(View.GONE);
            boolean bRejected = surveyBean.getRejectedFlag() == 1;
            if (bRejected) {
                holder.layoutJobStatus.setVisibility(View.VISIBLE);
                holder.imageJobState.setImageResource(R.mipmap.image_unhandled);
                holder.textJobState.setText("被驳回");
                holder.textJobState.setTextColor(mContext.getColor(R.color.text_unHandled));
            }

            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);
        }
    }
}
