package com.stm.asset.adapter;

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

import com.seuic.uhf.EPC;
import com.stm.asset.R;
import com.stm.asset.utils.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 类名称：InventoryAdapter
 * 类描述：盘询数据展示适配器
 * 创建人：story
 * 创建时间：2021-12-09 15:34
 */
public class InventoryAdapter extends BaseAdapter {

    private LayoutInflater inflater;
    private List<EPC> dataList;

    public InventoryAdapter(Context context) {
        inflater = LayoutInflater.from(context);
        dataList = new ArrayList<>();
    }

    @Override
    public int getCount() {
        return dataList == null ? 0 : dataList.size();
    }

    @Override
    public Object getItem(int position) {
        return dataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView =inflater.inflate(R.layout.view_inventory_item, null);
            viewHolder.epcTxt = convertView.findViewById(R.id.item_inventory_epc_txt);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        EPC epc = dataList.get(position);
        try {
            viewHolder.epcTxt.setText(epc.getId());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return convertView;
    }

    public void setData(List<EPC> data, boolean isReset) {
        if (dataList == null) {
            dataList = new ArrayList<>();
        }
        if (isReset) {
            dataList.clear();
        }
        if (data != null && data.size() > 0) {
            // 处理标签数据
            for (EPC item : data) {
                String epcStr = item.getId().replaceAll(" ", "");
                // 数字且位数是4的倍数
                if (!StringUtils.isEmptyOrNull(epcStr) && StringUtils.isNumber(epcStr) && epcStr.length() % 4 == 0) {
                    if (!hasItem(epcStr)) {
                        try {
                            Log.i("InventoryAdapter", "epcStr:" + epcStr.replaceAll(" ", ""));
                            dataList.add(item);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
        notifyDataSetChanged();
    }

    private boolean hasItem(String epc) {
        for (EPC item : dataList) {
            String epcStr = item.getId();
            if (epc.replaceAll(" ", "").equals(epcStr.replaceAll(" ", ""))) {
                return true;
            }
        }
        return false;
    }

    public List<EPC> queryData() {
        return dataList;
    }

    private class ViewHolder {
        private TextView epcTxt;
    }

}
