package com.rdiot.yx485.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.rdiot.yx485.R;
import com.rdiot.yx485.bean.ModelRepleBean;
import com.rdiot.yx485.databinding.ItemListModelBinding;
import com.rdiot.yx485.http.Url;
import com.zcolin.frame.util.DisplayUtil;

import java.util.List;

public class TopModelAdapter extends RecyclerView.Adapter<TopModelAdapter.DeviceViewHolder> {

    private List<ModelRepleBean.DataBean> devices;
    private OnItemClickListener listener;
    private Context mContext;

    public TopModelAdapter(List<ModelRepleBean.DataBean> devices, OnItemClickListener listener) {
        this.devices = devices;
        this.listener = listener;
    }

    @NonNull
    @Override
    public DeviceViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        mContext = parent.getContext();
        ItemListModelBinding binding = ItemListModelBinding.inflate(
                LayoutInflater.from(mContext), parent, false);
        return new DeviceViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull DeviceViewHolder holder, int position) {
        holder.setIsRecyclable(false);
        holder.bind(position);
    }

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

    public interface OnItemClickListener {
        void onItemClick(String id);
    }

    public class DeviceViewHolder extends RecyclerView.ViewHolder {
        ItemListModelBinding mBinding;

        public DeviceViewHolder(ItemListModelBinding binding) {
            super(binding.getRoot());
            mBinding = binding;
        }

        public void bind(int position) {
            // 为第一个item添加左边距
            ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) mBinding.getRoot().getLayoutParams();
            if (position == 0) {
                params.leftMargin = DisplayUtil.dip2px(mContext, 15);
            } else {
                params.leftMargin = 0;
            }
            mBinding.getRoot().setLayoutParams(params);

            ModelRepleBean.DataBean device = devices.get(position);
            if (device == null) {
                return;
            }

            if (!TextUtils.isEmpty(device.icon)) {
                Glide.with(mContext)
                        .load(Url.BASE_URL + device.icon)
                        .error(Glide.with(mContext).load(R.drawable.find_d))
                        .into(mBinding.image);
            }
            mBinding.name.setText(device.name);
            mBinding.tvState.setSelected(device.status);

            mBinding.getRoot().setOnClickListener(v -> {
                if (listener != null) {
                    listener.onItemClick(device.record_id);
                }
            });
        }
    }
}