package com.quyunshuo.module.home.weight;

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

import androidx.annotation.Nullable;

public class PowerSymbolView extends View {
    private Paint ringPaint;       // 渐变圆环的画笔
    private Paint symbolPaint;    // 电源符号的画笔
    private Path powerPath;       // 电源符号的路径
    private int[] gradientColors = {
            0xFFFCE38A, // 浅黄
            0xFFF38181, // 珊瑚粉
            0xFF6A89CC, // 灰蓝
            0xFFB8E986  // 薄荷绿
    };
    private float rotationAngle = 0; // 旋转角度
    private ValueAnimator animator;

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

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

    private void init() {
        // 关闭硬件加速（确保渐变效果正确）
        setLayerType(LAYER_TYPE_SOFTWARE, null);

        // 初始化圆环画笔
        ringPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        ringPaint.setStyle(Paint.Style.STROKE);
        ringPaint.setStrokeWidth(10);
        ringPaint.setStrokeCap(Paint.Cap.ROUND);

        // 初始化电源符号画笔
        symbolPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        symbolPaint.setColor(Color.RED);
        symbolPaint.setStyle(Paint.Style.STROKE);
        symbolPaint.setStrokeWidth(15);
        symbolPaint.setStrokeCap(Paint.Cap.ROUND);

        // 构建电源符号路径
        buildPowerSymbolPath();

        // 启动旋转动画
        startRingAnimation();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int centerX = getWidth() / 2;
        int centerY = getHeight() / 2;
        float radius = Math.min(centerX, centerY) * 0.7f;

        // 1. 绘制渐变圆环
        Shader shader = new SweepGradient(centerX, centerY, gradientColors, null);
        ringPaint.setShader(shader);
        canvas.rotate(rotationAngle, centerX, centerY);
        canvas.drawCircle(centerX, centerY, radius, ringPaint);

        // 2. 绘制电源符号（固定不旋转）
        canvas.save();
        canvas.rotate(-rotationAngle, centerX, centerY); // 抵消圆环旋转
        canvas.drawPath(powerPath, symbolPaint);
        canvas.restore();
    }

    // 构建电源符号路径（居中）
    private void buildPowerSymbolPath() {
        powerPath = new Path();
        int centerX = getWidth() / 2;
        int centerY = getHeight() / 2;
        float symbolSize = Math.min(getWidth(), getHeight()) * 0.3f;

        // 垂直线段（底部到顶部）
        powerPath.moveTo(centerX, centerY + symbolSize / 2);
        powerPath.lineTo(centerX, centerY - symbolSize / 2);

        // 顶部圆圈
        powerPath.addCircle(centerX, centerY - symbolSize / 2, symbolSize / 8, Path.Direction.CW);
    }

    // 启动圆环旋转动画
    private void startRingAnimation() {
        animator = ValueAnimator.ofFloat(0, 360);
        animator.setDuration(2000);
        animator.setRepeatCount(ValueAnimator.INFINITE);
        animator.setInterpolator(new LinearInterpolator());
        animator.addUpdateListener(animation -> {
            rotationAngle = (float) animation.getAnimatedValue();
            invalidate();
        });
        animator.start();
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (animator != null) {
            animator.cancel();
        }
    }
}