import React, { useState, useEffect } from "react";
import PropTypes from "prop-types";
import Box from "@mui/material/Box";
import * as Juce from "juce-framework-frontend";
// import Checkbox from "@mui/material/Checkbox";
// import FormGroup from "@mui/material/FormGroup";
// import FormControlLabel from "@mui/material/FormControlLabel";
import { controlParameterIndexAnnotation } from "../types/JuceTypes.js";
import { ToggleSwitch } from "./ToggleSwitch.js";

export default function JuceCheckbox({ identifier }) {
  JuceCheckbox.propTypes = {
    identifier: PropTypes.string,
  };

  const checkboxState = Juce.getToggleState(identifier);

  const [value, setValue] = useState(checkboxState.getValue());
  // eslint-disable-next-line no-unused-vars
  const [properties, setProperties] = useState(checkboxState.properties);

  // const handleChange = (event) => {
  //   checkboxState.setValue(event.target.checked);
  //   setValue(event.target.checked);
  // };
  const handleChange = (value) => {
    checkboxState.setValue(value);
    setValue(value);
  };

  useEffect(() => {
    const valueListenerId = checkboxState.valueChangedEvent.addListener(() => {
      setValue(checkboxState.getValue());
    });
    const propertiesListenerId =
      checkboxState.propertiesChangedEvent.addListener(() =>
        setProperties(checkboxState.properties)
      );

    return function cleanup() {
      checkboxState.valueChangedEvent.removeListener(valueListenerId);
      checkboxState.propertiesChangedEvent.removeListener(propertiesListenerId);
    };
  });

  // const cb = <Checkbox checked={value} onChange={handleChange} />;
  // const cb = <ToggleSwitch value={value} onChange={handleChange} />;
  return (
    <Box
      margin={0}
      padding={0}
      {...{
        [controlParameterIndexAnnotation]:
          checkboxState.properties.parameterIndex,
      }}
    >
      <ToggleSwitch value={value} onChange={handleChange} />
      {/* <FormGroup>
        <FormControlLabel control={cb} />
      </FormGroup> */}
    </Box>
  );
}
