package com.offline.music.playermp3.customerview;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.LinearInterpolator;

import com.offline.music.playermp3.helper.CommonUtils;

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

public class AnimaPlayingView extends View {

    private Paint paint;
    private int barColor = Color.GREEN;  // 音量条的颜色
    private int numBars = 5;  // 音量条的数量
    private float[] barHeights;  // 每个音量条的高度
    private int maxHeight = CommonUtils.dpToPx(20);  // 音量条的最大高度
    private int minHeight = CommonUtils.dpToPx(4);  // 音量条的最小高度

    // 每个柱子的宽度
    private int barWidth = CommonUtils.dpToPx(4);
    // 柱子之间的间距
    private int barSpacing = CommonUtils.dpToPx(2);

    private int cornerRadius = CommonUtils.dpToPx(16);
    private Random random;

    private ValueAnimator animator;
    private List<ValueAnimator> valueAnimatorList;

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

    public AnimaPlayingView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public AnimaPlayingView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        valueAnimatorList = new ArrayList<>();
        paint = new Paint();
        paint.setColor(barColor);
        paint.setStyle(Paint.Style.FILL);
        barHeights = new float[numBars];
        random = new Random();

        // 初始化音量条高度
        for (int i = 0; i < numBars; i++) {
            barHeights[i] = 0;
        }
        initAnimating();

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        int height = getHeight();
        int width = getWidth();

        int barCount = barHeights.length;
        int totalWidth = barCount * barWidth + (barCount - 1) * barSpacing;
        int startX = (width - totalWidth) / 2;


        for (int i = 0; i < numBars; i++) {

            float left = startX + i * (barWidth + barSpacing);
            float top = height - barHeights[i];
            float right = left + barWidth;
            float bottom = height;

            canvas.drawRoundRect(left, top, right, bottom, cornerRadius, cornerRadius, paint);
        }
    }

    public void startAnimating() {
        for (ValueAnimator valueAnimator : valueAnimatorList) {
            valueAnimator.start();
        }
    }

    // 启动固定动画，使音量条的高度变化
    public void initAnimating() {
        for (int i = 0; i < numBars; i++) {
            animator = ValueAnimator.ofFloat(0, 1);
            animator.setDuration(1000);  // 每0.5秒更新一次
            animator.setRepeatCount(ValueAnimator.INFINITE);
            animator.setRepeatMode(ValueAnimator.REVERSE);
            animator.setInterpolator(new LinearInterpolator());
            int finalI = i;
            animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    // 更新每个音量条的高度，使其不断变化
                    float animatedValue = (Float) animation.getAnimatedValue();
                    float v = animatedValue * maxHeight;
                    barHeights[finalI] = v;
                    invalidate();  // 重绘视图
                }
            });
            valueAnimatorList.add(animator);
            animator.setStartDelay(i * 200L);
        }


    }

    public void pauseAnimating() {
        for (ValueAnimator valueAnimator : valueAnimatorList) {
            valueAnimator.pause();
        }
        for (int i = 0; i < numBars; i++) {
            barHeights[i] = minHeight;
        }
        invalidate();
    }


    // 随机生成音量条的高度
    private float randomHeight() {
        return minHeight + random.nextFloat() * (maxHeight - minHeight);
    }

    // 设置音量条的颜色
    public void setBarColor(int color) {
        barColor = color;
        paint.setColor(barColor);
        invalidate();
    }

    // 设置音量条的最大高度
    public void setMaxHeight(int maxHeight) {
        this.maxHeight = maxHeight;
        invalidate();
    }

    // 设置音量条的最小高度
    public void setMinHeight(int minHeight) {
        this.minHeight = minHeight;
        invalidate();
    }

    // 设置音量条数量
    public void setNumBars(int numBars) {
        this.numBars = numBars;
        barHeights = new float[numBars];
        invalidate();
    }
}


