package com.keyborad.theme.trendyborad.trendylistutils;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

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

import com.keyborad.theme.trendyborad.bean.BeanDetails;
import com.keyborad.theme.trendyborad.bean.BeanWrapper;
import com.keyborad.theme.trendyborad.trendycallback.TrendyViewAllCallback;
import com.keyborad.theme.trendyborad.databinding.AdapterHeTrendyBinding;

import java.util.ArrayList;
import java.util.List;

public class TrendyHomeAdapter extends RecyclerView.Adapter<TrendyHomeAdapter.MainViewHolder> {

    private Context mContext;
    private List<BeanWrapper> mList;
    private TrendyViewAllCallback mCallBack;

    public TrendyHomeAdapter(Context context, List<BeanWrapper> list) {
        mContext = context;
        mList = list;
    }

    public void setClickAction(TrendyViewAllCallback callback) {
        mCallBack = callback;
    }

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

    @Override
    public void onBindViewHolder(@NonNull MainViewHolder holder, int position) {
        BeanWrapper wrapper = mList.get(position);
        holder.binding.className.setText(wrapper.getParentName());

        List<BeanDetails> showList = new ArrayList<>();
        if (wrapper.getKeyboardList() != null && !wrapper.getKeyboardList().isEmpty()) {
            int end = Math.min(6, wrapper.getKeyboardList().size());
            showList = new ArrayList<>(wrapper.getKeyboardList().subList(0, end));
        }

        holder.updateChildData(showList);

        holder.binding.tvAll.setOnClickListener(v -> {
            if (mCallBack != null) {
                mCallBack.OnClickSeeAll(wrapper.getParentName());
            }
        });
    }

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

    static class MainViewHolder extends RecyclerView.ViewHolder {

        AdapterHeTrendyBinding binding;
        TrendyHomeChildAdapter childAdapter;
        LinearLayoutManager layoutManager;

        MainViewHolder(@NonNull AdapterHeTrendyBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
            initChildRecycler();
        }

        private void initChildRecycler() {
            layoutManager = new LinearLayoutManager(
                    itemView.getContext(),
                    LinearLayoutManager.HORIZONTAL,
                    false
            );
            binding.childRecycler.setLayoutManager(layoutManager);

            childAdapter = new TrendyHomeChildAdapter(itemView.getContext(), new ArrayList<>());
            binding.childRecycler.setAdapter(childAdapter);

            // 添加滚动监听，实现缩放动画
            binding.childRecycler.addOnScrollListener(new RecyclerView.OnScrollListener() {
                @Override
                public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                    applyScale(recyclerView);
                }

                @Override
                public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                    if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                        applyScale(recyclerView);
                    }
                }
            });
        }

        // 应用缩放动画（中间大、两边小）
        private void applyScale(RecyclerView recyclerView) {
            if (recyclerView == null || recyclerView.getChildCount() == 0) return;

            float centerX = recyclerView.getWidth() / 2f;

            for (int i = 0; i < recyclerView.getChildCount(); i++) {
                View child = recyclerView.getChildAt(i);
                float childCenter = child.getX() + child.getWidth() / 2f;
                float distance = Math.abs(childCenter - centerX);

                float scale = 1f - (distance / centerX) * 0.2f;
                scale = Math.max(0.8f, Math.min(1f, scale));

                child.setScaleX(scale);
                child.setScaleY(scale);
            }
        }

        public void updateChildData(List<BeanDetails> list) {
            childAdapter.updateData(list);
        }
    }
}