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

import android.content.Context;
import android.graphics.drawable.Drawable;
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.lcp.arecyclerview.widget.ARecyclerBaseAdapter;
import com.tofly.ghrq_route.R;
import com.tofly.ghrq_route.module.job.JobTool;
import com.tofly.ghrq_route.module.job.domain.JobBean;
import com.tofly.ghrq_route.module.job.domain.JobBean.JOB_TASK_STATUS;
import com.tofly.ghrq_route.module.notifyManage.NotifyManageBean;


import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

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

public class JobListAdapter extends ARecyclerBaseAdapter<JobListAdapter.MyViewHolder, JobBean> {
    private int select_state;
    private JobTool jobTool;

    public JobListAdapter(Context context, List<JobBean> data) {
        super(context, data);
        jobTool = JobTool.getInstance();
        select_state = 0;
    }

    public void setData(List<JobBean> 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) {
        JobBean jobBean = getItem(position);
        if (jobBean != null) {
            holder.textJobTitle.setText(jobBean.getCaption());
            holder.textJobContent.setText(jobBean.getContent());
            String create_time = jobBean.getDispatchDate();
            if (create_time != null) {
                holder.textCreateTime.setText(create_time);
            }

            int textColor = mContext.getResources().getColor(R.color.text_unHandled);
            Drawable imageState = mContext.getResources().getDrawable(R.mipmap.image_unhandled);
            String[] jobStatus = mContext.getResources().getStringArray(R.array.job_select_state);
            int state = 0;
            int job_status = jobBean.getTaskStatus();
            JOB_TASK_STATUS status = JOB_TASK_STATUS.Status(job_status);
            switch (status) {
                case JOB_TASK_UNHANDLED:
                    state = 1;
                    break;
                case JOB_TASK_UNCOMMIT:
                    state = 2;
                    textColor = mContext.getResources().getColor(R.color.text_unUploaded);
                    imageState = mContext.getResources().getDrawable(R.mipmap.image_unuploaded);
                    break;
                case JOB_TASK_COMMITED:
                    state = 3;
                    textColor = mContext.getResources().getColor(R.color.text_handled);
                    imageState = mContext.getResources().getDrawable(R.mipmap.image_handled);
                    break;
            }
            holder.textJobState.setText(jobStatus[state]);
            holder.textJobState.setTextColor(textColor);
            holder.imageJobState.setImageDrawable(imageState);

            holder.imageNewJob.setVisibility(View.GONE);
            NotifyManageBean msgBean =  jobTool.getNotifyMsgBean(jobBean.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.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);
        }
    }
}
