import React, { useState } from "react";
import { createRoot } from "react-dom/client";
import { AgCharts } from "ag-charts-react";
import { AgChartOptions } from "ag-charts-enterprise";
import { getData } from "./data";
import "ag-charts-enterprise";

const ChartExample = () => {
  const [options, setOptions] = useState<AgChartOptions>({
    data: getData(),
    title: {
      text: "Height vs Weight for Major League Baseball Players",
    },
    footnote: {
      text: "Source: Statistics Online Computational Resource",
      spacing: 35,
    },
    padding: {
      left: 35,
    },
    series: [
      {
        type: "scatter",
        xKey: "weight",
        yKey: "height",
      },
    ],
    axes: [
      {
        position: "bottom",
        type: "number",
        nice: false,
        crossLines: [
          {
            type: "line",
            value: 210,
            lineDash: [5, 4],
            label: {
              text: "Height (Inches)",
              position: "top-left",
            },
          },
        ],
      },
      {
        position: "left",
        type: "number",
        nice: false,
        crossLines: [
          {
            type: "line",
            value: 75,
            lineDash: [5, 4],
            label: {
              text: "Weight (Pounds)",
              position: "inside-top-right",
            },
          },
        ],
      },
    ],
    annotations: {
      enabled: true, // Do we need this if annotations in initial state?
    },
    initialState: {
      annotations: [
        {
          type: "parallel-channel",
          start: {
            x: 153,
            y: 75.0,
          },
          end: {
            x: 187,
            y: 78.0,
          },
          height: 6,
        },
        {
          type: "horizontal-line",
          value: 76.0,
        },
        {
          type: "horizontal-line",
          value: 82.0,
          text: {
            label: "Support Level",
            position: "center",
            alignment: "right",
          },
        },
        {
          type: "horizontal-line",
          value: 67.8,
        },
        {
          type: "horizontal-line",
          value: 80.8,
          text: {
            label: "Resistance",
            position: "center",
            alignment: "left",
          },
        },
        {
          type: "horizontal-line",
          text: {
            label: "Short-term Support",
            position: "top",
            alignment: "center",
          },
          value: 79.03092783505156,
          lineStyle: "dotted",
        },
        {
          type: "text",
          text: "Distribution",
          x: 190,
          y: 79.0103092783505,
        },
        {
          type: "comment",
          text: "Accumulation",
          x: 165,
          y: 74,
        },
        {
          type: "callout",
          text: "Markup",
          start: {
            x: 165,
            y: 70,
          },
          end: {
            x: 169,
            y: 70,
          },
        },
        {
          type: "line",
          start: {
            x: 252,
            y: 71,
          },
          end: {
            x: 258,
            y: 81,
          },
          extendEnd: true,
          lineStyle: "dashed",
        },
        {
          type: "arrow-up",
          x: 165,
          y: 73,
        },
        {
          type: "arrow-down",
          x: 220,
          y: 73,
        },
        {
          type: "note",
          text: "This is a note",
          x: 180,
          y: 80,
        },
        {
          type: "vertical-line",
          text: {
            label: "Vertical Line",
            position: "center",
          },
          value: 240,
          strokeWidth: 3,
        },
        {
          type: "disjoint-channel",
          start: {
            x: 220,
            y: 81,
          },
          end: {
            x: 235,
            y: 75,
          },
          startHeight: 6,
          endHeight: -4,
        },
        {
          type: "arrow",
          start: {
            x: 165,
            y: 81,
          },
          end: {
            x: 265,
            y: 69,
          },
          lineStyle: "dotted",
          strokeWidth: 6,
        },
      ],
    },
  });

  return <AgCharts options={options as any} />;
};

const root = createRoot(document.getElementById("root")!);
root.render(<ChartExample />);

/** DARK MODE START **/
import { AgCharts as __chartAPI } from "ag-charts-community";

let darkmode =
  (localStorage["documentation:darkmode"] ||
    String(matchMedia("(prefers-color-scheme: dark)").matches)) === "true";

const isAgThemeOrUndefined = (theme) => {
  return (
    theme == null || (typeof theme === "string" && theme.startsWith("ag-"))
  );
};

const getDarkmodeTheme = (theme = "ag-default", preset) => {
  const baseTheme =
    preset === "price-volume" ? "ag-financial" : theme.replace(/-dark$/, "");
  return darkmode ? baseTheme + "-dark" : baseTheme;
};

__chartAPI.optionsMutationFn = function update(options, preset) {
  const nextOptions = { ...options };
  const theme = options.theme;
  if (isAgThemeOrUndefined(theme)) {
    nextOptions.theme = getDarkmodeTheme(theme, preset);
  } else if (
    typeof theme === "object" &&
    isAgThemeOrUndefined(theme.baseTheme)
  ) {
    nextOptions.theme = {
      ...options.theme,
      baseTheme: getDarkmodeTheme(theme.baseTheme, preset),
    };
  }
  return nextOptions;
};

const applyDarkmode = () => {
  document.documentElement.setAttribute("data-dark-mode", darkmode);
  const charts = document.querySelectorAll("[data-ag-charts]");
  charts.forEach((element) => {
    const chart = __chartAPI.getInstance(element.parentElement);
    if (chart == null) return;
    // This is just needed to trigger the theme update
    chart.update(chart.getOptions());
  });
  return charts.length !== 0;
};

if (!applyDarkmode()) {
  /* React defers updates. Rather than try and hook into the API, just wait until the darkmode is applied. */
  const observer = new MutationObserver(() => {
    if (applyDarkmode()) {
      observer.disconnect();
    }
  });
  observer.observe(document.body, {
    attributes: true,
    childList: true,
    subtree: true,
  });
}
window.addEventListener("message", (event) => {
  if (event.data && event.data.type === "color-scheme-change") {
    darkmode = event.data.darkmode;
    applyDarkmode();
  }
});
/** DARK MODE END **/
