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.sketch.domain.SketchBean;
import com.tofly.ghrq_route.module.sketch.domain.SketchBean.SKETCH_TASK_STATUS;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

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

public class SketchListAdapter extends ARecyclerBaseAdapter<SketchListAdapter.MyViewHolder, SketchBean> {
    private int select_state;

    public SketchListAdapter(Context context, List<SketchBean> data) {
        super(context, data);
        select_state = 0;
    }

    public void setData(List<SketchBean> 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) {
        SketchBean sketchBean = getItem(position);
        if (sketchBean != null) {
            holder.textSketchTitle.setText(sketchBean.getCaption());
            holder.textSketchContent.setText(sketchBean.getContent());
            String create_time = sketchBean.getCreatedate();
            if (create_time != null) {
                holder.textCreateTime.setText(create_time);
            }

            int status = sketchBean.getStatus();
            SKETCH_TASK_STATUS task_status = SKETCH_TASK_STATUS.Status(status);
            int textColor = mContext.getResources().getColor(R.color.text_unUploaded);
            Drawable imageState = mContext.getResources().getDrawable(R.mipmap.image_unuploaded);
            String[] sketchStatus = mContext.getResources().getStringArray(R.array.sketch_select_state);
            int state = 1;
            if (task_status == SKETCH_TASK_STATUS.SKETCH_TASK_COMMITED ||
            task_status == SKETCH_TASK_STATUS.SKETCH_TASK_HANDLED ) {
                state = 2;
                textColor = mContext.getResources().getColor(R.color.text_handled);
                imageState = mContext.getResources().getDrawable(R.mipmap.image_handled);
            }
            holder.textSketchState.setText(sketchStatus[state]);
            holder.textSketchState.setTextColor(textColor);
            holder.imageSketchState.setImageDrawable(imageState);

            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 textSketchTitle;
        @BindView(R.id.image_job_state)
        ImageView imageSketchState;
        @BindView(R.id.text_job_state)
        TextView textSketchState;
        @BindView(R.id.text_create_time)
        TextView textCreateTime;
        @BindView(R.id.text_job_content)
        TextView textSketchContent;

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