package com.naz.sdkdemo.adaapter;

import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
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.TrademarkInfo;
import com.naz.sdkdemo.utils.MyUtils;

import java.util.List;

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

   @Override
   protected void convert(@NonNull BaseViewHolder holder, TrademarkInfo info) {
      changeUi(holder, info.select);
      TextView etCount = holder.getView(R.id.et_count);
      info.count = 0;
      etCount.setText(String.valueOf(info.count));
      holder.getView(R.id.tv_add).setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            info.count ++;
            etCount.setText(String.valueOf(info.count));
         }
      });
      holder.getView(R.id.tv_reduce).setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            if (info.count<=0)
               return;
            info.count --;
            etCount.setText(String.valueOf(info.count));
         }
      });
      holder.setText(R.id.tv_number, String.format("产品编码：%s", info.material_code));
      holder.setText(R.id.tv_content, info.material_code_show);
      holder.setText(R.id.tv_tag1, info.decor_code_show);
      holder.setText(R.id.tv_tag2, info.decor_two_code_show);
      holder.setText(R.id.tv_tag3, info.craft_type_code_show);
      holder.setText(R.id.tv_tag4, info.craft_type_two_code_show);
      holder.setVisible(R.id.tv_tag1, !TextUtils.isEmpty(info.decor_code_show));
      holder.setVisible(R.id.tv_tag2, !TextUtils.isEmpty(info.decor_two_code_show));
      holder.setVisible(R.id.tv_tag3, !TextUtils.isEmpty(info.craft_type_code_show));
      holder.setVisible(R.id.tv_tag4, !TextUtils.isEmpty(info.craft_type_two_code_show));

      holder.setText(R.id.tv_specif, String.format("规格型号：%s %s", info.decor_model, info.craft_type_dk_show));
      if (info.prod_trademark_compare_view != null) {
         holder.setText(R.id.tv_trademark, String.format("商标：%s", info.prod_trademark_compare_view.brand_code_show));
      } else {
         holder.setText(R.id.tv_trademark, String.format("商标：%s", ""));
      }
      holder.setText(R.id.tv_unit, info.unit_code_show);
   }

   @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(), "change")) {
         item.select = !item.select;
         changeUi(helper, item.select);
      }
   }

   private void changeUi(BaseViewHolder helper, Boolean select) {
      if (select) {
         helper.itemView.setBackgroundResource(R.drawable.bg_item_select_red);
      } else {
         helper.itemView.setBackgroundResource(R.mipmap.bg_white_round_background);
      }
      int size = MyUtils.dp2px(mContext, 16);
      helper.itemView.setPadding(size, size, size, size);
   }
}
