package com.naz.sdkdemo.adaapter;

import android.text.TextUtils;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.naz.sdkdemo.R;
import com.naz.sdkdemo.bean.SearchResult;
import com.naz.sdkdemo.bean.TrademarkInfo;

import java.util.List;

public class ProductSearchAdapter extends BaseQuickAdapter<TrademarkInfo, BaseViewHolder> {
   public ProductSearchAdapter(@Nullable List<TrademarkInfo> data) {
      super(R.layout.item_search_product, data);
   }

   @Override
   protected void convert(@NonNull BaseViewHolder baseViewHolder, TrademarkInfo info) {
      baseViewHolder.setText(R.id.tv_name, info.material_code_show);
      baseViewHolder.setText(R.id.tv_count, info.qty+"");
      if (info.select) {
         changeUi(baseViewHolder, true);
      } else {
         changeUi(baseViewHolder, false);
      }
   }

   @Override
   protected void convertPayloads(@NonNull BaseViewHolder helper, TrademarkInfo item, @NonNull List<Object> payloads) {
      super.convertPayloads(helper, item, payloads);
      Object payload = payloads.get(0);
      if (TextUtils.equals(payload.toString(), "select")) {
         changeUi(helper, true);
         item.select = true;
      } else if (TextUtils.equals(payload.toString(), "unselect")) {
         changeUi(helper, false);
         item.select = false;
      }
   }

   private void changeUi(BaseViewHolder helper, Boolean select) {
      if (select) {
         helper.itemView.setBackgroundResource(R.mipmap.bg_search_red);
         ((TextView)helper.getView(R.id.tv_count)).setTextColor(mContext.getResources().getColor(R.color.color_ffE6212A));
         ((TextView)helper.getView(R.id.tv_name)).setTextColor(mContext.getResources().getColor(R.color.color_ffE6212A));
      } else {
         helper.itemView.setBackgroundResource(R.mipmap.bg_search_gray);
         ((TextView)helper.getView(R.id.tv_count)).setTextColor(mContext.getResources().getColor(R.color.color_ff333333));
         ((TextView)helper.getView(R.id.tv_name)).setTextColor(mContext.getResources().getColor(R.color.color_ff333333));

      }
   }
}
