package com.rdiot.yx485.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.rdiot.yx485.R;
import com.rdiot.yx485.bean.DeviceTreeBean;

import java.util.List;

public class ContentAdapter extends RecyclerView.Adapter<ContentAdapter.ViewHolder> {

    private List<DeviceTreeBean.DataBean> mData;
    private OnItemClickListener listener;
    private Context mContext;
    private int mSelectedIndex = 0;

    public ContentAdapter(List<DeviceTreeBean.DataBean> data, OnItemClickListener listener) {
        this.mData = data;
        this.listener = listener;
    }

    @SuppressLint("NotifyDataSetChanged")
    public void setSelected(int position) {
        if (mData != null && mData.size() > position) {
            mSelectedIndex = position;
        }
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        mContext = parent.getContext();
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.device_add_list_content, parent, false);
        return new ViewHolder(view);
    }


    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        if (mData != null && !TextUtils.isEmpty(mData.get(position).name)) {
            if (position == mSelectedIndex) {
                holder.title.setVisibility(View.VISIBLE);
                holder.itemView.setBackgroundColor(ContextCompat.getColor(mContext, R.color.white));
                holder.text.setTextColor(holder.text.getContext().getResources().getColor(R.color.title_333333));
            } else {
                holder.title.setVisibility(View.GONE);
                holder.itemView.setBackgroundColor(Color.parseColor("#F9F9F9"));
                holder.text.setTextColor(holder.text.getContext().getResources().getColor(R.color.title_999999));
            }
            holder.text.setText(mData.get(position).name);
            holder.itemView.setOnClickListener(v -> {
                if (listener != null) {
                    listener.onItemClick(position);
                }
            });
        }

    }

    @Override
    public int getItemCount() {
        if (mData == null) {
            return 0;
        }
        return mData.size();
    }

    public interface OnItemClickListener {
        void onItemClick(int position);
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        TextView text;
        TextView title;

        ViewHolder(View itemView) {
            super(itemView);
            text = itemView.findViewById(R.id.text);
            title = itemView.findViewById(R.id.title);
        }
    }
}