package io.github.jjsimas.chart2d.demo;

import io.github.jjsimas.chart2d.CartesianChart;
import io.github.jjsimas.chart2d.CartesianChartModel;
import io.github.jjsimas.chart2d.CartesianChartOptions;
import io.github.jjsimas.chart2d.CartesianOrientation;
import io.github.jjsimas.chart2d.CartesianSeries;
import io.github.jjsimas.chart2d.ChartTheme;
import io.github.jjsimas.chart2d.LegendPosition;
import io.github.jjsimas.chart2d.PieChart;
import io.github.jjsimas.chart2d.PieChartModel;
import io.github.jjsimas.chart2d.PieChartOptions;
import io.github.jjsimas.chart2d.PieSlice;
import io.github.jjsimas.chart2d.SeriesRenderer;
import io.github.jjsimas.chart2d.WarningBand;
import java.awt.Color;
import java.awt.Dimension;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JTabbedPane;
import javax.swing.SwingUtilities;

public final class ModernChart2DDemo {

  private ModernChart2DDemo() {
  }

  public static void main(String[] args) {
    SwingUtilities.invokeLater(() -> {
      JFrame frame = new JFrame("Chart2D Modern Demo");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      JTabbedPane tabs = new JTabbedPane();
      tabs.add("Vertical", buildVerticalChart());
      tabs.add("Horizontal", buildHorizontalChart());
      tabs.add("Pie", buildPieChart());

      frame.setContentPane(tabs);
      frame.setMinimumSize(new Dimension(720, 520));
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
    });
  }

  static CartesianChart buildVerticalChartForExport() {
    ChartTheme theme = ChartTheme.builder()
      .background(new Color(247, 250, 253))
      .plotBackground(Color.WHITE)
      .build();

    CartesianChartModel model = new CartesianChartModel(
      "Revenue vs Forecast",
      "Quarter",
      "USD (M)",
      List.of("Q1", "Q2", "Q3", "Q4", "Q5", "Q6"),
      List.of(
        new CartesianSeries("Revenue", List.of(12.0, 18.0, 15.0, 23.0, 28.0, 32.0), SeriesRenderer.BAR, new Color(63, 125, 228)),
        new CartesianSeries("Forecast", List.of(10.0, 16.0, 17.0, 21.0, 26.0, 30.0), SeriesRenderer.LINE, new Color(242, 108, 79)),
        new CartesianSeries("Variance", List.of(2.0, 2.0, -2.0, 2.0, 2.0, 2.0), SeriesRenderer.SCATTER, new Color(45, 176, 120))
      ),
      List.of(new WarningBand(25.0, 35.0, new Color(255, 232, 220, 160))),
      CartesianChartOptions.builder()
        .orientation(CartesianOrientation.VERTICAL)
        .legendPosition(LegendPosition.RIGHT)
        .showGrid(true)
        .preferredCategorySpanModel(44)
        .build(),
      theme
    );
    return new CartesianChart(model);
  }

  static CartesianChart buildHorizontalChartForExport() {
    CartesianChartModel model = new CartesianChartModel(
      "Ticket Resolution Time",
      "Team",
      "Hours",
      List.of("Core", "Payments", "Identity", "Analytics", "Support"),
      List.of(
        new CartesianSeries("Median", List.of(5.0, 9.0, 7.0, 11.0, 6.0), SeriesRenderer.BAR, new Color(23, 140, 171)),
        new CartesianSeries("P90", List.of(10.0, 15.0, 12.0, 18.0, 11.0), SeriesRenderer.AREA, new Color(229, 166, 47))
      ),
      List.of(new WarningBand(12.0, 20.0, new Color(255, 218, 218, 150))),
      CartesianChartOptions.builder()
        .orientation(CartesianOrientation.HORIZONTAL)
        .legendPosition(LegendPosition.BOTTOM)
        .showGrid(true)
        .stackedBars(false)
        .preferredCategorySpanModel(40)
        .build(),
      ChartTheme.defaults()
    );
    return new CartesianChart(model);
  }

  static PieChart buildPieChartForExport() {
    PieChartModel model = new PieChartModel(
      "Traffic Mix",
      List.of(
        new PieSlice("Organic", 42.0, new Color(54, 116, 217)),
        new PieSlice("Paid", 21.0, new Color(242, 109, 79)),
        new PieSlice("Referral", 18.0, new Color(75, 181, 120)),
        new PieSlice("Email", 11.0, new Color(240, 189, 76)),
        new PieSlice("Other", 8.0, new Color(154, 121, 213))
      ),
      PieChartOptions.builder()
        .legendPosition(LegendPosition.RIGHT)
        .showLabels(true)
        .build(),
      ChartTheme.defaults()
    );
    return new PieChart(model);
  }

  private static CartesianChart buildVerticalChart() {
    return buildVerticalChartForExport();
  }

  private static CartesianChart buildHorizontalChart() {
    return buildHorizontalChartForExport();
  }

  private static PieChart buildPieChart() {
    return buildPieChartForExport();
  }
}
