"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
require("core-js/modules/es.json.stringify.js");
require("core-js/modules/web.dom-collections.iterator.js");
var _stylesModule = _interopRequireDefault(require("./styles.module.css"));
var _marty_selected = _interopRequireDefault(require("../../assets/marty_selected.svg"));
var _cog_selected = _interopRequireDefault(require("../../assets/cog_selected.svg"));
var _disconnect_button = require("../../assets/connection/disconnect_button.svg");
var _disconnect_buttonHover = require("../../assets/connection/disconnect_button-hover.svg");
var _react = require("react");
var _RaftSignal = _interopRequireDefault(require("../RaftSignal"));
var _RaftBattery = _interopRequireDefault(require("../RaftBattery"));
var _raft = require("@robotical/webapp-types/dist-types/src/types/raft");
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
const SHOW_LOGS = true;
const TAG = "RobotButton";
const RobotButton = _ref => {
  let {
    robotType,
    connectedRaft,
    onClickDisconnect
  } = _ref;
  const [connectedRaftName, setConnectedRaftName] = (0, _react.useState)("");
  const [batteryStrength, setBatteryStrength] = (0, _react.useState)(0);
  const [RSSI, setRSSI] = (0, _react.useState)(-200);
  const [isDisconnectHovered, setIsDisconnectHovered] = (0, _react.useState)(false);
  (0, _react.useEffect)(() => {
    if (!connectedRaft) return;
    const batteryUpdateTimout = setInterval(() => {
      setBatteryStrength(connectedRaft.getBatteryStrength());
      setRSSI(connectedRaft.getRSSI());
    }, 200);
    return () => clearInterval(batteryUpdateTimout);
  }, [connectedRaft]);
  (0, _react.useEffect)(() => {
    if (connectedRaft) {
      setTimeout(() => {
        connectedRaft.getRaftName().then(name => {
          console.log("name", name);
          setConnectedRaftName(name);
        }).catch(error => {
          console.error(SHOW_LOGS, TAG, "Error getting raft name " + JSON.stringify(error));
        });
      }, 500);
    }
  }, [connectedRaft]);
  return /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
    className: [_stylesModule.default.robotButtonContainer].join(" "),
    children: /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
      className: _stylesModule.default.robotButtonContainerInner,
      children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
        className: _stylesModule.default.robotButtonIconContainer,
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)("img", {
          src: robotType === _raft.RaftTypeE.COG ? _cog_selected.default : _marty_selected.default,
          alt: "Robot icon",
          className: _stylesModule.default.robotButtonIcon
        })
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
        className: _stylesModule.default.batteryContainer,
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
          className: _stylesModule.default.batteryIcon,
          children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_RaftBattery.default, {
            batteryStrength: batteryStrength
          })
        })
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
        className: _stylesModule.default.signalContainer,
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_RaftSignal.default, {
          signalStrength: RSSI
        })
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
        className: _stylesModule.default.robotButtonNameContainer,
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
          className: _stylesModule.default.robotButtonName,
          children: truncateRobotName(connectedRaftName)
        })
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
        className: _stylesModule.default.trashContainer,
        onClick: () => onClickDisconnect((connectedRaft === null || connectedRaft === void 0 ? void 0 : connectedRaft.id) || ""),
        onMouseEnter: () => setIsDisconnectHovered(true),
        onMouseLeave: () => setIsDisconnectHovered(false),
        children: isDisconnectHovered ? /*#__PURE__*/(0, _jsxRuntime.jsx)(_disconnect_buttonHover.ReactComponent, {}) : /*#__PURE__*/(0, _jsxRuntime.jsx)(_disconnect_button.ReactComponent, {})
      })]
    })
  });
};
var _default = exports.default = RobotButton;
function truncateRobotName(name) {
  if (name.length > 13) {
    return name.substring(0, 11) + "...";
  }
  return name;
}