package com.handset.serialportsensor.adapter;

import android.text.TextUtils;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.handset.serialportsensor.R;
import com.handset.serialportsensor.bean.RawData;
import java.util.List;
import java.util.Locale;


public class MsgAdapter extends BaseMultiItemQuickAdapter<RawData, BaseViewHolder> {

    private String mFormat;

    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     *
     * @param data A new list is created out of this one to avoid mutable list
     */
    public MsgAdapter(List<RawData> data) {
        super(data);

        addItemType(RawData.TYPE_RECEIVE, R.layout.rv_item_receive);
        addItemType(RawData.TYPE_SEND, R.layout.rv_item_send);
    }

    @Override
    protected void convert(BaseViewHolder helper, RawData item) {
        if (TextUtils.isEmpty(mFormat)) {
            mFormat = mContext.getString(R.string.time_format);
        }
        if (item.getItemType() == RawData.TYPE_RECEIVE) {
            helper.setGone(R.id.item_receive_date, !TextUtils.isEmpty(item.getDate()))
                .setText(R.id.item_receive_date, TextUtils.isEmpty(item.getDate()) ? ""
                    : String.format(Locale.getDefault(), mFormat, item.getDate()))
                .setText(R.id.item_receive_contain, item.getMessage());
        } else {
            helper.setGone(R.id.item_send_date, !TextUtils.isEmpty(item.getDate()))
                .setText(R.id.item_send_date, TextUtils.isEmpty(item.getDate()) ? ""
                    : String.format(Locale.getDefault(), mFormat, item.getDate()))
                .setText(R.id.item_send_contain, item.getMessage());
        }
    }
}
