import React, { useEffect, useRef, useState } from "react";
import { Redirect } from "react-router";
import { useLocation } from "react-router-dom";
import {
  Button,
  Checkbox,
  FormControlLabel,
  FormGroup,
  FormLabel,
  Radio,
  RadioGroup,
  TextField
} from "@material-ui/core";

import useStyles from "../../useStyles";
import {
  useVehicleContext,
  VehicleProvider
} from "../../Contexts/VehicleContext";
import { useStatusContext } from "../../Contexts/StatusContext";
import { useUserContext } from "../../Contexts/UserContext";
import { logger } from "../../../services/monitoring";


const MainForm = () => {
  const queries = new URLSearchParams(useLocation().search);
  const vin = queries.get("vin") ?? "";

  const { vehicle, getVehicle, updateVehicle, busy } = useVehicleContext();
  const { token: { idToken: { jwtToken: token } } } = useUserContext();
  const { setMessage, setTitle, setSitePath } = useStatusContext();
  const [redirect, setRedirect] = useState(null);
  const classes = useStyles();

  const modelEl = useRef(null);
  const yearEl = useRef(null);
  const trimEl = useRef(null);
  const [selectedLogLevel, setSelectedLogLevel] = useState("info");
  const [canbusEnabled, setCANBusEnabled] = useState(true);
  const [dataLoggerEnabled, setDataLoggerEnabled] = useState(false);
  const [maxMemBufferSize, setMaxMemBufferSize] = useState(0);
  const [maxDiskBufferSize, setMaxDiskBufferSize] = useState(0);

  useEffect(() => {
    setTitle("Update Vehicle");
    setSitePath([
      {
        label: `Vehicle ${vin}`,
        link: "/vehicles",
      },
      {
        label: "Update Vehicle",
      },
    ]);
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, []);

  useEffect(() => {
    (async () => {
      try {
        if (!vin || !token) return;
        await getVehicle(vin, token);
      } catch (e) {
        setMessage(e.message);
        logger.warn(e.stack);
      }
    })();
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [token]);

  useEffect(() => {
    setSelectedLogLevel(vehicle.log_level ?? selectedLogLevel);

    modelEl.current.value = vehicle.model ?? "Ocean"
    yearEl.current.value = vehicle.year ?? "2022"
    trimEl.current.value = vehicle.trim ?? "Base"

    if (vehicle.canbus) {
      setCANBusEnabled(vehicle.canbus.enabled ?? canbusEnabled);
      setDataLoggerEnabled(vehicle.canbus.data_logger_enabled ?? dataLoggerEnabled);
      setMaxMemBufferSize(vehicle.canbus.max_mem_buffer_size ?? maxMemBufferSize);
      setMaxDiskBufferSize(vehicle.canbus.max_disk_buffer_size ?? maxDiskBufferSize);
    }
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [vehicle]);

  const onLogLevelChange = (event) => {
    setSelectedLogLevel(event.target.value);
  }

  const onCANBusChange = (event) => {
    setCANBusEnabled(event.target.checked);
  }

  const onDataLoggerChange = (event) => {
    setDataLoggerEnabled(event.target.checked);
  }

  const onMaxMemBufferSizeChange = (event) => {
    setMaxMemBufferSize(event.target.value);
  }

  const onMaxDiskBufferSizeChange = (event) => {
    setMaxDiskBufferSize(event.target.value);
  }

  const onSubmit = async (event) => {
    try {
      event.preventDefault();

      const formData = {
        vin: vin,
        model: modelEl.current.value,
        year: parseInt(yearEl.current.value),
        trim: trimEl.current.value,
        log_level: selectedLogLevel,
        canbus: {
          enabled: canbusEnabled,
          data_logger_enabled: canbusEnabled ? dataLoggerEnabled : false,
          max_mem_buffer_size: canbusEnabled ? parseInt(maxMemBufferSize) : 0,
          max_disk_buffer_size: canbusEnabled && dataLoggerEnabled ? parseInt(maxDiskBufferSize) : 0
        }
      };

      const result = await updateVehicle(vin, formData, token);
      if (!result || result.error) return;

      setMessage(`Updated ${result.vin}`);
      setRedirect(`/vehicle-status/${result.vin}`);
    } catch (e) {
      setMessage(e.message);
      logger.warn(e.stack);
    }
  };

  if (redirect && redirect.length > 0) {
    return <Redirect to={redirect} />;
  }

  return (
    <div className={classes.paper}>
      <form className={classes.form} noValidate action="{onSubmit}">
        <TextField
          id="vin"
          name="vin"
          label="VIN"
          variant="outlined"
          margin="normal"
          inputProps={{
            maxLength: "17",
            readOnly: true,
          }}
          disabled
          value={vin}
          required
          fullWidth
        />
        <TextField
          id="model"
          name="model"
          label="Model"
          defaultValue="Ocean"
          variant="outlined"
          margin="normal"
          inputProps={{
            maxLength: "255",
          }}
          required
          fullWidth
          inputRef={modelEl}
        />
        <TextField
          id="year"
          name="year"
          label="Year"
          type="number"
          defaultValue="2022"
          variant="outlined"
          margin="normal"
          inputProps={{
            maxLength: "4",
            minLength: "4",
          }}
          required
          fullWidth
          inputRef={yearEl}
        />
        <TextField
          id="trim"
          name="trim"
          label="Trim"
          defaultValue="Base"
          variant="outlined"
          margin="normal"
          inputProps={{
            maxLength: "4",
            minLength: "4",
          }}
          required
          fullWidth
          inputRef={trimEl}
        />
        <FormLabel id="demo-row-radio-buttons-group-label">Log Level</FormLabel>
        <RadioGroup
          row
          aria-labelledby="demo-row-radio-buttons-group-label"
          name="log-level-group"
          value={selectedLogLevel}
          onChange={onLogLevelChange}
          margin="normal"
        >
          <FormControlLabel value="trace" control={<Radio />} label="Trace" />
          <FormControlLabel value="debug" control={<Radio />} label="Debug" />
          <FormControlLabel value="info" control={<Radio />} label="Info" />
          <FormControlLabel value="warning" control={<Radio />} label="Warning" />
          <FormControlLabel value="error" control={<Radio />} label="Error" />
          <FormControlLabel value="critical" control={<Radio />} label="Critical" />
        </RadioGroup>
        <FormLabel id="demo-row-radio-buttons-group-label">CAN Bus</FormLabel>
        <FormGroup>
          <FormControlLabel control={
            <Checkbox
              checked={canbusEnabled}
              onChange={onCANBusChange}
            />
          } label="CAN Bus Enabled" />
          <TextField
            id="max_mem_buffer_size"
            name="max_mem_buffer_size"
            label='Max Memory Buffer Size (0 uses default size)'
            value={maxMemBufferSize}
            onChange={onMaxMemBufferSizeChange}
            variant="outlined"
            margin="normal"
            inputProps={{
              maxLength: "12",
            }}
            type="number"
            disabled={!canbusEnabled}
            required
            fullWidth
          />
          <FormControlLabel control={
            <Checkbox
              checked={dataLoggerEnabled}
              onChange={onDataLoggerChange}
              disabled={!canbusEnabled}
            />
          } label="Data Logger Enabled" />
        </FormGroup>
        <TextField
          id="max_disk_buffer_size"
          name="max_disk_buffer_size"
          label='Max Disk Buffer Size (0 uses default size)'
          value={maxDiskBufferSize}
          onChange={onMaxDiskBufferSizeChange}
          variant="outlined"
          margin="normal"
          inputProps={{
            maxLength: "12",
          }}
          type="number"
          disabled={!dataLoggerEnabled}
          required
          fullWidth
        />
        <Button
          type="submit"
          disabled={busy}
          fullWidth
          variant="contained"
          color="primary"
          className={classes.submit}
          onClick={onSubmit}
        >
          Submit
        </Button>
      </form>
    </div>
  );
};

const VehicleUpdateForm = (props) => (
  <VehicleProvider>
    <MainForm {...props} />
  </VehicleProvider>
);

export default VehicleUpdateForm;
