package com.example.pap_teste.components;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;

import com.example.pap_teste.R;

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

public class InteractiveRatingBar extends LinearLayout {

    public interface OnRatingChangeListener {
        void onRatingChanged(double rating);
    }

    private OnRatingChangeListener listener;
    private int numStars = 5;
    private double currentRating = 5.0;
    private List<ImageView> stars = new ArrayList<>();
    
    // Size settings
    private int starSizeDp = 40;
    private int starPaddingDp = 4;

    public InteractiveRatingBar(Context context) {
        super(context);
        init(context);
    }

    public InteractiveRatingBar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public InteractiveRatingBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        setOrientation(HORIZONTAL);
        setGravity(android.view.Gravity.CENTER);

        int sizePx = (int) (starSizeDp * context.getResources().getDisplayMetrics().density);
        int paddingPx = (int) (starPaddingDp * context.getResources().getDisplayMetrics().density);

        for (int i = 0; i < numStars; i++) {
            ImageView star = new ImageView(context);
            LayoutParams params = new LayoutParams(sizePx, sizePx);
            params.setMargins(paddingPx, paddingPx, paddingPx, paddingPx);
            star.setLayoutParams(params);
            
            // Set scale type to fit center
            star.setScaleType(ImageView.ScaleType.FIT_CENTER);
            
            addView(star);
            stars.add(star);
        }

        updateStars(currentRating, false);
    }

    public void setOnRatingChangeListener(OnRatingChangeListener listener) {
        this.listener = listener;
    }

    public double getRating() {
        return currentRating;
    }

    public void setRating(double rating) {
        this.currentRating = Math.max(0.0, Math.min(rating, numStars));
        updateStars(currentRating, false);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_MOVE) {
            float x = event.getX();
            calculateRatingFromTouch(x);
            return true;
        }
        return super.onTouchEvent(event);
    }

    private void calculateRatingFromTouch(float x) {
        if (stars.isEmpty()) return;

        double newRating = 0;
        
        for (int i = 0; i < numStars; i++) {
            ImageView star = stars.get(i);
            float starLeft = star.getLeft();
            float starRight = star.getRight();
            float starCenter = starLeft + (star.getWidth() / 2f);

            if (x >= starRight) {
                newRating = i + 1.0;
            } else if (x >= starCenter && x < starRight) {
                newRating = i + 1.0;
                break;
            } else if (x >= starLeft && x < starCenter) {
                newRating = i + 0.5;
                break;
            } else if (x < starLeft && i == 0) {
                newRating = 0.5; // Minimum 0.5 when touched
                break;
            }
        }

        // Clamp values
        newRating = Math.max(0.5, Math.min(newRating, numStars));

        if (newRating != currentRating) {
            currentRating = newRating;
            updateStars(currentRating, true);
            if (listener != null) {
                listener.onRatingChanged(currentRating);
            }
        }
    }

    private void updateStars(double rating, boolean animate) {
        for (int i = 0; i < numStars; i++) {
            ImageView star = stars.get(i);
            int previousResId = (Integer) (star.getTag() != null ? star.getTag() : 0);
            int newResId;

            double starVal = i + 1;
            if (rating >= starVal) {
                newResId = R.drawable.ic_star_full;
            } else if (rating >= starVal - 0.5) {
                newResId = R.drawable.ic_star_half;
            } else {
                newResId = R.drawable.ic_star_empty;
            }

            if (previousResId != newResId) {
                star.setImageResource(newResId);
                star.setTag(newResId);
                
                if (animate) {
                    // Micro-animation (Bounce scale)
                    star.animate()
                        .scaleX(1.2f)
                        .scaleY(1.2f)
                        .setDuration(100)
                        .withEndAction(() -> {
                            star.animate()
                                .scaleX(1.0f)
                                .scaleY(1.0f)
                                .setDuration(100)
                                .start();
                        })
                        .start();
                }
            }
        }
    }
}
