package com.philisense.rdgo.adapter;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.philisense.rdgo.R;
import com.philisense.rdgo.bean.response.PutOutRecordBean;
import com.philisense.rdgo.databinding.ItemOrderBinding;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by shaop on 2018/1/5.
 */

public class OrderAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<PutOutRecordBean.DataBean> dataArray;
    private Context context;

    public OrderAdapter(Context context) {
        this.context = context;
        dataArray = new ArrayList<>();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ItemOrderBinding mBindHeader = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.item_order, parent, false);
        return new OrderViewHolder(mBindHeader.getRoot());
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        OrderViewHolder lineViewHolder = (OrderViewHolder) holder;
        lineViewHolder.bindView(dataArray.get(position));


        String time = dataArray.get(position).getOpTime();
        time = time.replace("Z", " UTC");//注意是空格+UTC
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");
        Date s = null;
        String ss = "";
        SimpleDateFormat result_form = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        try {
            s = formatter.parse(time);
            ss = result_form.format(s);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        lineViewHolder.itemOrderBinding.itemOrderTime.setText(ss);

        String zhuan="";
        String gene=dataArray.get(position).getGene();
        if("0".equals(gene)){
            zhuan="转基因";
        }else if ("1".equals(gene)){
            zhuan="非转基因";
        }else if("2".equals(gene)){
            zhuan="其他";
        }else {
            zhuan="未说明";
        }
        lineViewHolder.itemOrderBinding.jiyin.setText(zhuan);
        lineViewHolder.itemOrderBinding.itemOrderPerson.setText("负责人"+dataArray.get(position).getName());
        lineViewHolder.itemOrderBinding.boxNum.setText(dataArray.get(position).getNum()+"箱");

    }

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

    /**
     * 添加数据
     *
     * @param data 数据
     */
    public void insertData(List<PutOutRecordBean.DataBean> data) {

    }

    /**
     * 初始化数据
     *
     * @param data 数据
     */
    public void setData(List<PutOutRecordBean.DataBean> data) {
        dataArray = data;
        notifyDataSetChanged();
    }

    private class OrderViewHolder extends RecyclerView.ViewHolder {

        ItemOrderBinding itemOrderBinding;

        OrderViewHolder(View view) {
            super(view);
            itemOrderBinding = DataBindingUtil.getBinding(view);
        }

        public void bindView(PutOutRecordBean.DataBean dataBean) {
            itemOrderBinding.setData(dataBean);
        }
    }
}

