package org.kne.cloud.network.klalb.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.LineBorder;

import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.dial.DialPlot;
import org.jfree.chart.plot.dial.DialPointer;
import org.jfree.chart.plot.dial.DialTextAnnotation;
import org.jfree.chart.plot.dial.StandardDialFrame;
import org.jfree.chart.plot.dial.StandardDialRange;
import org.jfree.chart.plot.dial.StandardDialScale;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.general.DefaultValueDataset;
import org.kne.ui.LinearGradientColor;

public class DialPanel extends JPanel {

    private static final int SEGMENTS = 100;  // 渐变色分段数，越高越平滑
    private static final double INNER_RADIUS = 0.85;
    private static final double OUTER_RADIUS = 0.90;

    private DialTextAnnotation textAnnotation;
    private DialPlot plot;

    public DialPanel(int size, String title, DefaultValueDataset primary, 
                     DefaultValueDataset secondary, Color pointerColor, Color outlineColor) {
        JPanel jp = new JPanel();
        jp.setLayout(new FlowLayout(FlowLayout.CENTER, 1, 1));
        plot = createDialPlot(primary, secondary, pointerColor, outlineColor);
        textAnnotation = new DialTextAnnotation("");
        textAnnotation.setFont(UIEnv.getFont().deriveFont(20.0f));
        plot.addLayer(textAnnotation);
        jp.setLayout(new BorderLayout());
        Dimension dim = new Dimension(size, size);
        jp.add(createChartPanel(createChart(plot), dim), BorderLayout.CENTER);
        JLabel jlb = new JLabel(title);
        jlb.setFont(UIEnv.getFont().deriveFont(16.0f));
        jlb.setHorizontalAlignment(SwingConstants.CENTER);
        jp.add(jlb, BorderLayout.SOUTH);
        add(jp);
    }

    public DialTextAnnotation getTextAnnotation() {
        return textAnnotation;
    }

    /**
     * 创建图表面板
     */
    private ChartPanel createChartPanel(JFreeChart chart, Dimension dashSize) {
        ChartPanel panel = new ChartPanel(chart);
        panel.setPreferredSize(dashSize);
        panel.setSize(dashSize);
        panel.setBackground(Color.WHITE);
        panel.setBorder(new LineBorder(Color.LIGHT_GRAY));
        return panel;
    }

    /**
     * 创建图表
     */
    private JFreeChart createChart(DialPlot plot) {
        JFreeChart chart = new JFreeChart(plot);
        chart.setBorderPaint(Color.WHITE);
        chart.setTitle((TextTitle) null);
        plot.setView(0, 0, 1, 1);
        return chart;
    }

    /**
     * 创建仪表盘图表
     */
    private DialPlot createDialPlot(DefaultValueDataset primary, DefaultValueDataset secondary,
                                    Color pointerColor, Color outlineColor) {
        DialPlot plot = new DialPlot();
        plot.setDataset(1, primary);
        plot.setDataset(0, secondary);

        StandardDialFrame frame = new StandardDialFrame();
        frame.setVisible(false);
        plot.setDialFrame(frame);

        // 添加刻度
        StandardDialScale scale = new StandardDialScale(0, 100, -120, -300, 10, 5);
        scale.setTickRadius(0.80);
        scale.setTickLabelsVisible(false);
        plot.addScale(0, scale);


        // 添加指针
        addDialPointers(plot, pointerColor, outlineColor);

        return plot;
    }

    /**
     * 添加渐变色范围（平滑过渡）
     * 从绿色 → 黄色 → 红色，基于 KLALBUtils.getColorByLoadPercentage()
     */
    public void addGradientRanges(LinearGradientColor lgc) {
        double segmentAngle = 100.0 / SEGMENTS;  // 每个分段的角度跨度

        for (int i = 0; i < SEGMENTS; i++) {
            double start = i * segmentAngle;
            double end = (i + 1) * segmentAngle;
            // 计算当前分段对应的负载百分比（取分段的中点）
            float loadPercent = (float) (start + segmentAngle / 2);
            Color color = lgc.getColor(loadPercent/100);

            StandardDialRange range = new StandardDialRange(start, end, color);
            addDialRange(range);
        }
    }

   

    public void addDialRange(StandardDialRange range) {
        range.setInnerRadius(INNER_RADIUS);
        range.setOuterRadius(OUTER_RADIUS);
        plot.addLayer(range);
    }

    /**
     * 为仪表盘添加指针
     */
    private void addDialPointers(DialPlot plot, Color primaryColor, Color secondaryColor) {
        // 次要指针（透明）
        DialPointer.Pointer secondaryPointer = new DialPointer.Pointer();
        secondaryPointer.setRadius(0.8);
        secondaryPointer.setFillPaint(new Color(secondaryColor.getRed(), secondaryColor.getGreen(),
                secondaryColor.getBlue(), 0));
        secondaryPointer.setOutlinePaint(secondaryColor);
        secondaryPointer.setDatasetIndex(0);
        plot.addLayer(secondaryPointer);

        // 主要指针
        DialPointer.Pointer primaryPointer = new DialPointer.Pointer();
        primaryPointer.setRadius(0.8);
        primaryPointer.setFillPaint(primaryColor);
        primaryPointer.setOutlinePaint(primaryColor);
        primaryPointer.setDatasetIndex(1);
        plot.addLayer(primaryPointer);
    }
}