import React, { useState, useRef, Fragment } from "react";
import { createRoot } from "react-dom/client";
import { AgCharts } from "ag-charts-react";
import {
  AgCartesianChartOptions,
  AgChartsInstance,
} from "ag-charts-enterprise";
import "ag-charts-enterprise";
import clone from "clone";

const data = [
  { quarter: "week 3", week: 3, iphone: 60 },
  { quarter: "week 4", week: 4, iphone: 185 },
  { quarter: "week 5", week: 5, iphone: 148 },
  { quarter: "week 6", week: 6, iphone: 130 },
  { quarter: "week 9", week: 9, iphone: 62 },
  { quarter: "week 10", week: 10, iphone: 137 },
  { quarter: "week 11", week: 11, iphone: 121 },
];

const ChartExample = () => {
  const chartRef = useRef<AgChartsInstance>(null);
  const [options, setOptions] = useState<AgCartesianChartOptions>({
    animation: {
      enabled: true,
    },
    data: [...data],
    series: [
      {
        type: "line",
        xKey: "quarter",
        yKey: "iphone",
        label: {
          formatter: ({ value }) => String(value),
        },
        // visible: false
      },
    ],
    axes: [
      {
        position: "left",
        type: "number",
        keys: ["iphone"],
      },
      {
        position: "bottom",
        type: "category",
      },
    ],
  });

  const actionReset = () => {
    const nextOptions = clone(options);

    nextOptions.data = [...data];

    setOptions(nextOptions);
  };

  const actionAddEndWeek = () => {
    const nextOptions = clone(options);

    const data = nextOptions.data ?? [];
    const nextWeek = data.slice(-1)[0].week + 1;
    nextOptions.data = [
      ...data,
      {
        quarter: `week ${nextWeek}`,
        week: nextWeek,
        iphone: 78 * (Math.random() - 0.5),
      },
    ];

    setOptions(nextOptions);
  };

  const actionAddStartWeek = () => {
    const nextOptions = clone(options);

    const data = nextOptions.data ?? [];
    const prevWeek = data[0].week - 1;
    nextOptions.data = [
      {
        quarter: `week ${prevWeek}`,
        week: prevWeek,
        iphone: 78 * (Math.random() - 0.5),
      },
      ...data,
    ];

    setOptions(nextOptions);
  };

  const actionAddWeek12and13 = () => {
    const nextOptions = clone(options);

    nextOptions.data = [
      ...(nextOptions.data ?? []),
      { quarter: "week 12", week: 12, iphone: 78 },
      { quarter: "week 13", week: 13, iphone: 138 },
    ];
    nextOptions.data.sort((a: any, b: any) => a.week - b.week);

    setOptions(nextOptions);
  };

  const actionAddWeek7and8 = () => {
    const nextOptions = clone(options);

    nextOptions.data = [
      ...(nextOptions.data ?? []),
      { quarter: "week 7", week: 7, iphone: 142 },
      { quarter: "week 8", week: 8, iphone: 87 },
    ];
    nextOptions.data.sort((a: any, b: any) => a.week - b.week);

    setOptions(nextOptions);
  };

  const reorder = () => {
    const nextOptions = clone(options);

    nextOptions.data = [...(nextOptions.data ?? [])];
    nextOptions.data?.forEach((d) => (d.random = Math.random()));
    nextOptions.data?.sort((a, b) => a.random - b.random);

    setOptions(nextOptions);
  };

  const rapidUpdate = () => {
    chartRef.current!.updateDelta({
      data: [...data, { quarter: "week 12", iphone: 78 }],
    });
    chartRef.current!.waitForUpdate().then(() => {
      chartRef.current!.updateDelta({
        data: [
          ...data,
          { quarter: "week 12", iphone: 78 },
          { quarter: "week 13", iphone: 138 },
        ],
      });
    });
  };

  return (
    <Fragment>
      <div className="toolbar">
        <button onClick={actionReset}>Reset</button>
        <hr />
        <button onClick={actionAddStartWeek}>Add Start Week</button>
        <button onClick={actionAddEndWeek}>Add End Week</button>
        <button onClick={actionAddWeek12and13}>Add Weeks 12+13</button>
        <button onClick={actionAddWeek7and8}>Add Weeks 7+8</button>
        <hr />
        <button onClick={reorder}>Reorder</button>
        <button onClick={rapidUpdate}>Rapid Update</button>
      </div>
      <AgCharts ref={chartRef} options={options as any} />
    </Fragment>
  );
};

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 **/
