package com.stm.asset.adapter;

import android.annotation.SuppressLint;
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.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.stm.asset.R;
import com.stm.asset.model.InventoryInfo;

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

public class AdvantageAdapter extends RecyclerView.Adapter<AdvantageAdapter.AdvantageViewHolder> {

    private List<InventoryInfo> dataList;
    private final Context context;

    public AdvantageAdapter(Context context) {
        this.dataList = new ArrayList<>();
        this.context = context;
    }

    @NonNull
    @Override
    public AdvantageAdapter.AdvantageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflater = LayoutInflater.from(context).inflate(R.layout.view_check_task_item, parent, false);
        return new AdvantageAdapter.AdvantageViewHolder(inflater);
    }

    @Override
    public void onBindViewHolder(@NonNull AdvantageAdapter.AdvantageViewHolder holder, int position) {
        InventoryInfo taskInfo = dataList.get(position);
        holder.numberTxt.setText(taskInfo.getNumber());
        String statusName = "";
        if ("0".equals(taskInfo.getItem01())) {
            statusName = "未盘点";
        } else if ("1".equals(taskInfo.getItem01())) {
            statusName = "相符";
        } else if ("2".equals(taskInfo.getItem01())) {
            statusName = "盘亏";
        } else if ("3".equals(taskInfo.getItem01())) {
            statusName = "盘盈";
        }
        holder.statusTxt.setText(statusName);
    }

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

    /**
     * 刷新数据
     */
    @SuppressLint("NotifyDataSetChanged")
    public void setData(List<InventoryInfo> data) {
        if (dataList == null) {
            dataList = new ArrayList<>();
        }
        dataList.clear();
        if (data != null && data.size() > 0) {
            dataList.addAll(data);
        }
        notifyDataSetChanged();
    }

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

    static class AdvantageViewHolder extends RecyclerView.ViewHolder {
        LinearLayout nameLayout;
        TextView numberTxt, statusTxt;

        public AdvantageViewHolder(View itemView) {
            super(itemView);
            nameLayout = itemView.findViewById(R.id.task_item_name_layout);
            numberTxt = itemView.findViewById(R.id.task_item_number_txt);
            statusTxt = itemView.findViewById(R.id.task_item_status_txt);
            nameLayout.setVisibility(View.GONE);
        }

    }

}
