package com.naz.sdkdemo.adaapter;

import android.text.TextUtils;
import android.view.View;
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.StockInfo;

import java.util.List;

public class UpTrademarkAdapter extends BaseQuickAdapter<StockInfo, BaseViewHolder> {

   public UpTrademarkAdapter(@Nullable List<StockInfo> data) {
      super(R.layout.item_up_trademark, data);
   }

   @Override
   protected void convert(@NonNull BaseViewHolder baseViewHolder, StockInfo info) {
      baseViewHolder.setText(R.id.tv_title, info.box_code_show);
      if (info.select) {
         changeUi(baseViewHolder, true);
      } else {
         changeUi(baseViewHolder, false);
      }
   }

   @Override
   protected void convertPayloads(@NonNull BaseViewHolder helper, StockInfo 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.drawable.bg_item_select_red);
         helper.getView(R.id.iv_cancel).setVisibility(View.VISIBLE);
         ((TextView)helper.getView(R.id.tv_title)).setTextColor(mContext.getResources().getColor(R.color.color_ffE6212A));
      } else {
         helper.itemView.setBackgroundResource(R.drawable.bg_item_unselect_gray);
         helper.getView(R.id.iv_cancel).setVisibility(View.GONE);
         ((TextView)helper.getView(R.id.tv_title)).setTextColor(mContext.getResources().getColor(R.color.color_ff333333));
      }
   }
}
