package xn.xxp.home.window;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.ViewGroup;

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

import com.blankj.utilcode.util.LogUtils;

import java.util.List;

import http.vo.response.SafeClassify;
import xn.xxp.databinding.ItemSafeClassifyTextBinding;

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

    private Context context;
    private List<SafeClassify> safeClassifyList;

    /**
     * @param safeClassifyList 筛选classifyType为2
     */
    public WindowTextlAdapter(Context context, List<SafeClassify> safeClassifyList) {
        this.context = context;
        this.safeClassifyList = safeClassifyList;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(ItemSafeClassifyTextBinding.inflate(LayoutInflater.from(context), parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        ItemSafeClassifyTextBinding binding = holder.binding;
        SafeClassify safeClassify = safeClassifyList.get(position);
        if (null != safeClassify) {
            // name
            binding.emergencyContactNameTV.setText(TextUtils.isEmpty(safeClassify.classifyName) ? "" : safeClassify.classifyName);
            try {
                if (!TextUtils.isEmpty(safeClassify.showColour)) {
                    int color = Color.parseColor(safeClassify.showColour);
                    binding.emergencyContactNameTV.setBackgroundColor(color);
                }
            } catch (Exception e) {
                LogUtils.e("颜色设置失败", Log.getStackTraceString(e));
            }
            List<String> classifyList = safeClassify.classifyList;
            if (null != classifyList && !classifyList.isEmpty()) {
                StringBuilder stringBuilder = new StringBuilder();
                for (int i = 0; i < classifyList.size(); i++) {
                    String itemText = classifyList.get(i);
                    if (!TextUtils.isEmpty(itemText)) {
                        stringBuilder.append(itemText).append("\n").append("\n");
                    }
                }
                if (stringBuilder.length() > 3) {
                    stringBuilder.delete(stringBuilder.length() - 2, stringBuilder.length());
                }
                binding.emergencyContactTV.setText(stringBuilder.toString());
            } else {
                binding.emergencyContactTV.setText("");
            }

        }
    }

    @SuppressLint("NotifyDataSetChanged")
    public void update(List<SafeClassify> safeClassifyList) {
        this.safeClassifyList = safeClassifyList;
        notifyDataSetChanged();
    }

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

    public static class ViewHolder extends RecyclerView.ViewHolder {
        ItemSafeClassifyTextBinding binding;

        public ViewHolder(ItemSafeClassifyTextBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }
}
