import {
  Table,
  TableBody,
  TableCell,
  TableFooter,
  TablePagination,
  TableRow,
} from "@material-ui/core";
import { logger } from "../../services/monitoring";
import React, { useEffect, useState } from "react";
import { useParams } from "react-router";
import {
  CarUpdatesProvider,
  useCarUpdatesContext
} from "../Contexts/CarUpdatesContext";
import { useUserContext } from "../Contexts/UserContext";
import TableHeaderSortable from "../Table/HeaderSortable";
import useStyles from "../useStyles";

const tableColumns = [
  {
    id: "sums_version",
    label: "SUMS Version",
  },
  {
    id: "sums_rxswin",
    label: "RXSwin Version",
  },
];

const MainForm = () => {
  const { sums_version } = useParams();
  const classes = useStyles();
  const [orderBy, setOrderBy] = useState("id");
  const [order, setOrder] = useState("desc");
  const {
    getSUMSVersionRxSwins,
    stopMonitor,
  } = useCarUpdatesContext();
  const [versionRxSwins, setVersionRxSwins] = useState([]);
  const {
    token: {
      idToken: { jwtToken: token },
    },
  } = useUserContext();

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

  const handleSort = (event, property) => {
    try {
      if (property === orderBy) {
        if (order === "asc") {
          setOrder("desc");
        } else {
          setOrder("asc");
        }
      } else {
        setOrderBy(property);
        setOrder("asc");
      }
    } catch (e) {
      logger.warn(e.stack);
    }
  };

  return (
    <Table>
      <TableHeaderSortable
        classes={classes}
        orderBy={orderBy}
        order={order}
        columnData={tableColumns}
        onSortRequest={handleSort}
      />
      <TableBody>
      </TableBody>
    </Table>
  );
};

const SumsRxSwin = () => (
  <CarUpdatesProvider>
    <MainForm />
  </CarUpdatesProvider>
);

export default SumsRxSwin;