package xn.xxp.home.window;

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

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

import com.blankj.utilcode.util.LogUtils;

import java.util.List;

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

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

    private Context context;
    private List<SafeClassify> safeClassifyList;

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


    }

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

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        ItemSafeClassifyBinding binding = holder.binding;
        SafeClassify safeClassify = safeClassifyList.get(position);
        if (null != safeClassify) {
            // 第一条item
            if (0 == position) {
                if (!TextUtils.isEmpty(safeClassify.showColour)) {
                    try {
                        int color = Color.parseColor(safeClassify.showColour);
                        GradientDrawable topAndLeftGD = new GradientDrawable();
                        topAndLeftGD.setShape(GradientDrawable.RECTANGLE);
                        topAndLeftGD.setCornerRadii(new float[]{10f, 10f, 0f, 0f, 0f, 0f, 0f, 0f,});
                        topAndLeftGD.setColor(color);
                        binding.safeClassifyTV.setBackground(topAndLeftGD);
                    } catch (Exception e) {
                        LogUtils.e("颜色设置失败", Log.getStackTraceString(e));
                    }
                }
            }
            // 最后一条item
            else if ((safeClassifyList.size() - 1) == position) {
                if (!TextUtils.isEmpty(safeClassify.showColour)) {
                    try {
                        int color = Color.parseColor(safeClassify.showColour);
                        GradientDrawable bottomAndLeftGD = new GradientDrawable();
                        bottomAndLeftGD.setShape(GradientDrawable.RECTANGLE);
                        bottomAndLeftGD.setCornerRadii(new float[]{0f, 0f, 0f, 0f, 0f, 0f, 10f, 10f});
                        bottomAndLeftGD.setColor(color);
                        binding.safeClassifyTV.setBackground(bottomAndLeftGD);
                    } catch (Exception e) {
                        LogUtils.e("颜色设置失败", Log.getStackTraceString(e));
                    }
                }
            }
            // 其它item
            else {
                if (!TextUtils.isEmpty(safeClassify.showColour)) {
                    try {
                        int color = Color.parseColor(safeClassify.showColour);
                        GradientDrawable gradientDrawable = new GradientDrawable();
                        gradientDrawable.setShape(GradientDrawable.RECTANGLE);
                        gradientDrawable.setColor(color);
                        binding.safeClassifyTV.setBackground(gradientDrawable);
                    } catch (Exception e) {
                        LogUtils.e("颜色设置失败", Log.getStackTraceString(e));
                    }
                }
            }

            if (!TextUtils.isEmpty(safeClassify.classifyName)) {
                char[] charArray = safeClassify.classifyName.toCharArray();
                StringBuilder verticalText = new StringBuilder();
                for (char c : charArray) {
                    verticalText.append(c);
                    verticalText.append("\n");
                }
                verticalText.delete(verticalText.length() - 1, verticalText.length());
                binding.safeClassifyTV.setText(verticalText.toString());
            }

            List<String> classifyList = safeClassify.classifyList;
            if (null != classifyList && classifyList.isEmpty()) {
                classifyList.add("");
            }
            if (holder.windowHorizontalAdapter == null) {
                holder.windowHorizontalAdapter = new WindowHorizontalAdapter(context, classifyList);
                holder.binding.itemSafeClassifyRV.setAdapter(holder.windowHorizontalAdapter);
            } else {
                holder.windowHorizontalAdapter.updateData(classifyList);
            }
        }
    }

    @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 {
        ItemSafeClassifyBinding binding;
        WindowHorizontalAdapter windowHorizontalAdapter;

        public ViewHolder(ItemSafeClassifyBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
            binding.itemSafeClassifyRV.setLayoutManager(new LinearLayoutManager(binding.getRoot().getContext(), LinearLayoutManager.HORIZONTAL, false));
            windowHorizontalAdapter = null;
        }
    }
}
