import React from "react";
import PropTypes from "prop-types";
import { TableCell, TableRow, IconButton, Collapse } from "@mui/material";
import { KeyboardArrowDown, KeyboardArrowUp } from "@mui/icons-material";
import MachineCollapsedContent from "./common/MachineCollapsedContent";
import { useSensitiveInfo } from "../../../hooks";
import ActionsGroup from "./common/ActionsGroup";

const MachineTableRow = ({ machine, actions, logs, addLog }) => {
  const [open, setOpen] = React.useState(false);
  const { mask } = useSensitiveInfo();

  return (
    <React.Fragment>
      <TableRow
        sx={{
          "& .MuiTableCell-root": {
            borderBottom: "unset"
          }
        }}
      >
        <TableCell>
          <IconButton aria-label="expand row" size="small" onClick={() => setOpen(!open)}>
            {open ? <KeyboardArrowUp /> : <KeyboardArrowDown />}
          </IconButton>
        </TableCell>
        <TableCell component="th" scope="row">
          {mask(machine.fullMachineName)}
        </TableCell>
        <TableCell>{mask(machine.machineName)}</TableCell>
        <TableCell>{mask(machine.iPv4Address)}</TableCell>
        <TableCell>{mask(machine.macAddress)}</TableCell>
        <TableCell align="right">
          <ActionsGroup machine={machine} actions={actions} addLog={addLog} />
        </TableCell>
      </TableRow>
      <TableRow>
        <TableCell style={{ paddingBottom: 0, paddingTop: 0 }} colSpan={6}>
          <Collapse in={open} timeout="auto" unmountOnExit>
            <MachineCollapsedContent
              description={machine.description}
              logs={logs}
              style={{ paddingBottom: "0.5rem" }}
            />
          </Collapse>
        </TableCell>
      </TableRow>
    </React.Fragment>
  );
};

MachineTableRow.propTypes = {
  machine: PropTypes.shape({
    machineId: PropTypes.number.isRequired,
    machineName: PropTypes.string.isRequired,
    fullMachineName: PropTypes.string.isRequired,
    macAddress: PropTypes.string.isRequired,
    iPv4Address: PropTypes.string,
    description: PropTypes.string
  }).isRequired,
  actions: PropTypes.array.isRequired,
  logs: PropTypes.array.isRequired,
  addLog: PropTypes.func.isRequired
};

export default MachineTableRow;
