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

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

import androidx.recyclerview.widget.RecyclerView;

import com.tofly.ghrq_route.R;
import com.tofly.ghrq_route.module.gps.domain.LocationGPSDataBean;
import com.tofly.ghrq_route.common.util.date.DateUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by lihualong on 2017/9/23.
 */

public class SignPathAdapter extends RecyclerView.Adapter {
    private Context context;
    private List<List<LocationGPSDataBean>> lists;
    private SignPathOnClickListener signOnClickListener;

    public SignPathAdapter(Context context, List<List<LocationGPSDataBean>> lists) {
        this.context = context;
        this.lists = lists;
    }

    public void setData(List<List<LocationGPSDataBean>> lists){
        this.lists = lists;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(context).inflate(R.layout.item_sign_path, parent, false);
        return new Viewholder(inflate);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        Viewholder viewholder = (Viewholder) holder;
        viewholder.setData(position);
        viewholder.itemView.setOnClickListener(v -> signOnClickListener.onClick(v,position));
    }
    public void setSignOnClickListener(SignPathOnClickListener signOnClickListener){
        this.signOnClickListener = signOnClickListener;
    }
    public interface SignPathOnClickListener{
        void onClick(View view,int position);
    }
    @Override
    public int getItemCount() {
        return lists.size();
    }

    protected class Viewholder extends RecyclerView.ViewHolder {
        @BindView(R.id.sign_path_item_starttime)
        TextView starTime;
        @BindView(R.id.sign_path_item_endtime)
        TextView endTime;

        public Viewholder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        public void setData(int position) {
            List<LocationGPSDataBean> locationManagers = lists.get(position);
            if (!locationManagers.isEmpty()) {
                int size = locationManagers.size();
                if (size > 0) {
                    starTime.setText(DateUtils.formatMSTime(locationManagers.get(0).getDate()));
                    endTime.setText(DateUtils.formatMSTime(locationManagers.get(size - 1).getDate()));
                }
            }
        }
    }

}
