package com.example.cloudmusic.textNewUI;

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 java.util.ArrayList;
import java.util.List;

public class Demo1 extends View {


    private Paint linePaint; // 画线的画笔
    private Paint pointPaint; // 画点的画笔
    private Paint textPaint; // 画文字的画笔
    private List<Float> dataPoints; // 数据点的列表
    private int lastRise;

    public Demo1(Context context) {
        super(context);
        init(); // 调用初始化方法
    }

    public Demo1(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(); // 调用初始化方法
    }

    public Demo1(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(); // 调用初始化方法
    }

    private void init() {
        linePaint = new Paint(); // 初始化画线的画笔
        //0 145 244
        int lineColor = Color.rgb(239, 161, 51);
        linePaint.setColor(lineColor); // 设置画笔颜色为蓝色
        linePaint.setStrokeWidth(5); // 设置画笔宽度为5
        linePaint.setAntiAlias(true); // 设置抗锯齿

        pointPaint = new Paint(); // 初始化画点的画笔
        pointPaint.setColor(lineColor); // 设置画笔颜色为红色
        pointPaint.setStrokeWidth(10); // 设置画笔宽度为10
        pointPaint.setAntiAlias(true); // 设置抗锯齿

        textPaint = new Paint(); // 初始化画文字的画笔
        int textColor = Color.rgb(0, 0, 0);
        textPaint.setColor(textColor); // 设置画笔颜色为黑色
        textPaint.setTextSize(24); // 设置文字大小为30
        textPaint.setAntiAlias(true); // 设置抗锯齿

        dataPoints = new ArrayList<>(); // 初始化数据点的列表
    }

    public void setDataPoints(List<Float> dataPoints) {
        this.dataPoints = dataPoints; // 设置数据点列表
        invalidate(); // 重新绘制视图
    }

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

        if (dataPoints == null || dataPoints.size() < 2) {
            return; // 如果没有数据或者数据点少于2个，不绘制
        }

        float width = getWidth(); // 获取视图宽度
        float height = getHeight(); // 获取视图高度
        float maxDataPoint = getMaxDataPoint(); // 获取数据点的最大值
        float minDataPoint = getMinDataPoint(); // 获取数据点的最小值
        float padding = 24;
        float xInterval = (width - padding * 3) / (dataPoints.size() - 1); // 计算X轴上每个点的间隔
        float yScale = (height - padding * 4) / (maxDataPoint - minDataPoint); // 计算Y轴上的比例
        lastRise = -1;
        float startY = 0;

        for (int i = 0; i < dataPoints.size() - 1; i++) {
            float startX = i * xInterval + padding; // 计算起点的X坐标
            startY = height - (dataPoints.get(i) - minDataPoint) * yScale - padding * 2; // 计算起点的Y坐标
            float stopX = (i + 1) * xInterval + padding; // 计算终点的X坐标
            float stopY = height - (dataPoints.get(i + 1) - minDataPoint) * yScale - padding * 2; // 计算终点的Y坐标
            canvas.drawLine(startX, startY, stopX, stopY, linePaint); // 绘制线段
            canvas.drawCircle(startX, startY, 5, pointPaint); // 绘制起点的圆点
            drawText(canvas, startX, startY, stopY, i);
        }

        // 绘制最后一个点和其数据值
        float lastX = (dataPoints.size() - 1) * xInterval + padding; // 计算最后一个点的X坐标
        float lastY = height - (dataPoints.get(dataPoints.size() - 1) - minDataPoint) * yScale - padding * 2; // 计算最后一个点的Y坐标
        canvas.drawCircle(lastX, lastY, 5, pointPaint); // 绘制最后一个点的圆点
        drawText(canvas, lastX, lastY, startY, dataPoints.size() - 1);// 在最后一个点上方绘制数据值和单位
    }

    private void drawText(Canvas canvas, float startX, float startY, float stopY, int i) {
        //如果是降
        long a = (long) ((double) dataPoints.get(i));
        String text = a + "°";
        if (startY < stopY) {
            if (lastRise == 0) {//如果是降
                canvas.drawText(text, startX, startY - 12, textPaint);
            } else {
                canvas.drawText(text, startX - 24, startY - 12, textPaint);
            }
            lastRise = 0;
        } else if (startY > stopY) {//如果是升
            if (lastRise == 1) {
                canvas.drawText(text, startX - 48, startY - 12, textPaint);
            } else {
                canvas.drawText(text, startX - 24, startY + 24, textPaint);
            }
            lastRise = 1;
        } else {
            canvas.drawText(text, startX, startY - 12, textPaint);
        }
    }

    private float getMaxDataPoint() {
        float max = 0;
        for (float point : dataPoints) {
            if (point > max) {
                max = point; // 找到数据点中的最大值
            }
        }
        return max; // 返回最大值
    }

    private float getMinDataPoint() {
        float min = 0;
        for (float point : dataPoints) {
            if (point < min) {
                min = point; // 找到数据点中的最小值
            }
        }
        return min; // 返回最小值
    }
}

