package com.yztiot.streetlamp;

import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

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

import java.util.List;

public class LightModeAdapter extends RecyclerView.Adapter<LightModeAdapter.LightModeViewHolder> {
    private List<LightMode> lightModes;
    private int selectedPosition = -1;
    private int containerId;

    private OnLightModeSelectedListener listener;

    public LightModeAdapter(List<LightMode> lightModes, int containerId, OnLightModeSelectedListener listener) {
        this.lightModes = lightModes;
        this.containerId = containerId;
        this.listener = listener;
    }
    public interface OnLightModeSelectedListener {
        void onLightModeSelected(LightMode lightMode, int containerId, int position, StringBuilder frameBuilder);
        void onFrameBuilt(String frame);
    }

    public void setSelectedPosition(int position) {
        this.selectedPosition = position;
        notifyDataSetChanged(); // 刷新视图
    }

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

    @Override
    public void onBindViewHolder(LightModeViewHolder holder, int position) {
        LightMode lightMode = lightModes.get(position);
        holder.bind(lightMode, position == selectedPosition);

        holder.itemView.setOnClickListener(v -> {
            // 记录当前选中的位置
            int oldPosition = selectedPosition;
            selectedPosition = position; // 更新为当前选中的位置

            // 更新显示
            notifyItemChanged(oldPosition);
            notifyItemChanged(selectedPosition);

            if (listener != null) {
                listener.onLightModeSelected(lightMode, containerId, position, new StringBuilder()); // 传递选中的模式
            }

            Log.d("LightModeAdapter", "Clicked containerId: " + containerId + ", position: " + position);
        });
    }

    @Override
    public int getItemCount() {
        return lightModes.size();
    }

    public static class LightModeViewHolder extends RecyclerView.ViewHolder {
        private TextView modeStatus;
        private View colorShow, colorMode;

        public LightModeViewHolder(View itemView) {
            super(itemView);
            modeStatus = itemView.findViewById(R.id.color_mode_status);
            colorShow = itemView.findViewById(R.id.color_show);
            colorMode = itemView.findViewById(R.id.color_mode);
        }

        public void bind(LightMode lightMode, boolean isSelected) {
            itemView.setTag(lightMode);
            modeStatus.setText(lightMode.getModeName());

            // 设置选中状态样式
            if (isSelected) {
                colorMode.setBackgroundResource(R.drawable.light_mode_on);
                modeStatus.setTextColor(Color.BLACK);
            } else {
                colorMode.setBackgroundResource(R.drawable.light_mode_off);  // 你需要创建这个背景
                modeStatus.setTextColor(Color.GRAY);
            }

            // 渐变色设置（与之前一样）
            if (lightMode.getColorResId() != 0) {
                int color = ContextCompat.getColor(itemView.getContext(), lightMode.getColorResId());
                GradientDrawable circleDrawable = new GradientDrawable();
                circleDrawable.setColor(color);
                circleDrawable.setShape(GradientDrawable.OVAL);
                colorShow.setBackground(circleDrawable);
            } else if (lightMode.getColor1ResId() != 0 && lightMode.getColor2ResId() != 0) {
                int color1 = ContextCompat.getColor(itemView.getContext(), lightMode.getColor1ResId());
                int color2 = ContextCompat.getColor(itemView.getContext(), lightMode.getColor2ResId());
                GradientDrawable gradientDrawable = new GradientDrawable(
                        GradientDrawable.Orientation.LEFT_RIGHT,
                        new int[]{color1, color2});
                gradientDrawable.setShape(GradientDrawable.OVAL);
                colorShow.setBackground(gradientDrawable);
            } else if (lightMode.getColor1ResId() != 0 && lightMode.getColor2ResId() != 0 && lightMode.getColor3ResId() != 0) {
                int color1 = ContextCompat.getColor(itemView.getContext(), lightMode.getColor1ResId());
                int color2 = ContextCompat.getColor(itemView.getContext(), lightMode.getColor2ResId());
                int color3 = ContextCompat.getColor(itemView.getContext(), lightMode.getColor3ResId());
                GradientDrawable gradientDrawable = new GradientDrawable(
                        GradientDrawable.Orientation.LEFT_RIGHT,
                        new int[]{color1, color2, color3});
                gradientDrawable.setShape(GradientDrawable.OVAL);
                colorShow.setBackground(gradientDrawable);
            } else {
                int[] rainbowColors = {
                        Color.parseColor("#FF6B6B"),
                        Color.parseColor("#FFA94D"),
                        Color.parseColor("#FFD43B"),
                        Color.parseColor("#69DB7C"),
                        Color.parseColor("#38D9A9"),
                        Color.parseColor("#4DABF7"),
                        Color.parseColor("#9775FA")
                };
                GradientDrawable rainbowDrawable = new GradientDrawable(
                        GradientDrawable.Orientation.LEFT_RIGHT,
                        rainbowColors);
                rainbowDrawable.setShape(GradientDrawable.OVAL);
                colorShow.setBackground(rainbowDrawable);
            }
        }

    }
}