package com.sipai.jspatrol.Patrol;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.sipai.jspatrol.Bean.ProPatrolPoint;
import com.sipai.jspatrol.R;
import com.sipai.jspatrol.utils.Global.GlobalVal;

import java.util.List;

/**
 * Created by lvliming on 2019/2/21.
 */

public class ProPatrolPointAdapter extends RecyclerView.Adapter<ProPatrolPointAdapter.ViewHolder> {
    private List<ProPatrolPoint> list;
    private Context context;
    private onRecyclerViewItemClick mOnClick;

    public ProPatrolPointAdapter(List<ProPatrolPoint> list, Context context,onRecyclerViewItemClick mOnClick) {
        this.list = list;
        this.context = context;
        this.mOnClick = mOnClick;
    }

    @Override
    public ProPatrolPointAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_pro_point,parent,false);
        final ViewHolder viewHolder = new ViewHolder(view);
//        viewHolder.view.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//
//            }
//        });
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ProPatrolPointAdapter.ViewHolder holder, int position) {
        ProPatrolPoint item = list.get(position);
        holder.tv_name.setText(item.getName());
        if (item.getProcessSection()!=null) {
            holder.tv_area.setText(item.getProcessSection().getName());
        }else if (item.getProcessSectionName()!=null&&!item.getProcessSectionName().isEmpty()){
            holder.tv_area.setText(item.getProcessSectionName());
        }
        holder.tv_worker.setText(item.getWorkerName());
        if (item.getFinishDt()!=null&&item.getFinishDt().length()>16) {
            holder.tv_finishDt.setText(item.getFinishDt().substring(0,16));
        }
        if (item.getStatus().equals(GlobalVal.Status_Issue)) {
            holder.ll_all.setBackgroundColor(context.getResources().getColor(R.color.bg_patrol_point_uncom));
            holder.tv_name.setTextColor(context.getResources().getColor(R.color.black));
            holder.tv_area.setTextColor(context.getResources().getColor(R.color.bg_patrol_point_area));
        }else if(item.getStatus().equals(GlobalVal.Status_Finish)) {
            holder.ll_all.setBackgroundColor(context.getResources().getColor(R.color.bg_patrol_point_com));
            holder.tv_name.setTextColor(context.getResources().getColor(R.color.task_top_little_blue));
            holder.tv_area.setTextColor(context.getResources().getColor(R.color.task_top_little_blue));
            holder.tv_worker.setTextColor(context.getResources().getColor(R.color.task_top_little_blue));
            holder.tv_finishDt.setTextColor(context.getResources().getColor(R.color.task_top_little_blue));
        }
    }

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

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        View view;
        LinearLayout ll_all;
        TextView tv_name,tv_area,tv_worker,tv_finishDt;
        public ViewHolder(View itemView) {
            super(itemView);
            view = itemView;
            ll_all = itemView.findViewById(R.id.moudle_item_pro_patrol_point_ll_all);
            tv_name = itemView.findViewById(R.id.moudle_item_pro_patrol_point_tv_name);
            tv_area = itemView.findViewById(R.id.moudle_item_pro_patrol_point_tv_area);
            tv_worker = itemView.findViewById(R.id.moudle_item_pro_patrol_point_tv_worker);
            tv_finishDt = itemView.findViewById(R.id.moudle_item_pro_patrol_point_tv_date);
            view.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (mOnClick!=null){
                mOnClick.onItemClick(v,getAdapterPosition());
            }
        }
    }

    public interface onRecyclerViewItemClick{
        void onItemClick(View v,int position);
    }
}
