"use strict";

exports.__esModule = true;
exports.default = void 0;

var _react = _interopRequireWildcard(require("react"));

var _styles = require("@material-ui/core/styles");

var _Button = _interopRequireDefault(require("@material-ui/core/Button"));

var _Dialog = _interopRequireDefault(require("@material-ui/core/Dialog"));

var _DialogTitle = _interopRequireDefault(require("@material-ui/core/DialogTitle"));

var _DialogContent = _interopRequireDefault(require("@material-ui/core/DialogContent"));

var _DialogActions = _interopRequireDefault(require("@material-ui/core/DialogActions"));

var _IconButton = _interopRequireDefault(require("@material-ui/core/IconButton"));

var _Close = _interopRequireDefault(require("@material-ui/icons/Close"));

var _Typography = _interopRequireDefault(require("@material-ui/core/Typography"));

var _Grid = _interopRequireDefault(require("@material-ui/core/Grid"));

var _Rating = _interopRequireDefault(require("@material-ui/lab/Rating"));

var _Stars = _interopRequireDefault(require("@material-ui/icons/Stars"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

const styles = theme => ({
  root: {
    margin: 0,
    padding: theme.spacing(2)
  },
  closeButton: {
    position: 'absolute',
    right: theme.spacing(1),
    top: theme.spacing(1),
    color: theme.palette.grey[500]
  },
  nowrap: {
    whiteSpace: 'nowrap',
    overflow: 'hidden',
    textOverflow: 'ellipsis'
  }
});

const DialogTitle = (0, _styles.withStyles)(styles)(props => {
  const {
    children,
    classes,
    onClose,
    ...other
  } = props;
  return /*#__PURE__*/_react.default.createElement(_DialogTitle.default, _extends({
    disableTypography: true,
    className: classes.root
  }, other), /*#__PURE__*/_react.default.createElement(_Typography.default, {
    variant: "h6"
  }, children), onClose ? /*#__PURE__*/_react.default.createElement(_IconButton.default, {
    "aria-label": "close",
    className: classes.closeButton,
    onClick: onClose
  }, /*#__PURE__*/_react.default.createElement(_Close.default, null)) : null);
});
const DialogContent = (0, _styles.withStyles)(theme => ({
  root: {
    padding: theme.spacing(2)
  }
}))(_DialogContent.default);
const DialogActions = (0, _styles.withStyles)(theme => ({
  root: {
    margin: 0,
    padding: theme.spacing(1)
  }
}))(_DialogActions.default);

function NowPlayingDialog(props) {
  const {
    open,
    setOpen,
    currentPlaying,
    classes,
    subsonic
  } = props;
  const [song, setSong] = (0, _react.useState)(currentPlaying.song);
  (0, _react.useEffect)(() => {
    subsonic.getSong(currentPlaying.song.id).then(s => {
      setSong(s.song);
    });
  }, [currentPlaying.song]); // eslint-disable-line

  const id = song.id;

  const onChangeRating = (evt, v) => {
    (async () => {
      v = v || 0; // TODO: add these...

      await subsonic._execute('setRating', {
        id,
        rating: v
      });
      const s = await subsonic.getSong(id);
      setSong(s.song);
    })();
  };

  const onStarToggle = () => {
    (async () => {
      if (song.starred) {
        // TODO: add these...
        await subsonic._execute('unstar', {
          id
        });
      } else {
        await subsonic._execute('star', {
          id
        });
      }

      const s = await subsonic.getSong(id);
      setSong(s.song);
    })();
  };

  const handleClose = () => {
    setOpen(false);
  };

  return /*#__PURE__*/_react.default.createElement(_Dialog.default, {
    onClose: handleClose,
    "aria-labelledby": "customized-dialog-title",
    open: open,
    styles: {
      width: '80vw'
    }
  }, /*#__PURE__*/_react.default.createElement(DialogTitle, {
    id: "customized-dialog-title",
    onClose: handleClose
  }, song.title), /*#__PURE__*/_react.default.createElement(DialogContent, {
    dividers: true
  }, /*#__PURE__*/_react.default.createElement(_Grid.default, {
    container: true
  }, /*#__PURE__*/_react.default.createElement(_Grid.default, {
    item: true,
    xs: 4
  }, /*#__PURE__*/_react.default.createElement("img", {
    src: currentPlaying.artwork,
    style: {
      width: 192,
      height: 'auto',
      paddingRight: 20
    },
    alt: song.album
  })), /*#__PURE__*/_react.default.createElement(_Grid.default, {
    item: true,
    xs: 1
  }), /*#__PURE__*/_react.default.createElement(_Grid.default, {
    container: true,
    item: true,
    xs: 7
  }, /*#__PURE__*/_react.default.createElement(_Grid.default, {
    item: true,
    xs: 3,
    className: classes.nowrap
  }, /*#__PURE__*/_react.default.createElement(_Typography.default, {
    gutterBottom: true
  }, "Artist")), /*#__PURE__*/_react.default.createElement(_Grid.default, {
    item: true,
    xs: 9,
    className: classes.nowrap
  }, /*#__PURE__*/_react.default.createElement(_Typography.default, {
    gutterBottom: true
  }, song.artist)), /*#__PURE__*/_react.default.createElement(_Grid.default, {
    item: true,
    xs: 3,
    className: classes.nowrap
  }, /*#__PURE__*/_react.default.createElement(_Typography.default, {
    gutterBottom: true
  }, "Album")), /*#__PURE__*/_react.default.createElement(_Grid.default, {
    item: true,
    xs: 9,
    className: classes.nowrap
  }, /*#__PURE__*/_react.default.createElement(_Typography.default, {
    gutterBottom: true
  }, song.album)), /*#__PURE__*/_react.default.createElement(_Grid.default, {
    item: true,
    xs: 3,
    className: classes.nowrap
  }, /*#__PURE__*/_react.default.createElement(_Typography.default, {
    gutterBottom: true
  }, "Year")), /*#__PURE__*/_react.default.createElement(_Grid.default, {
    item: true,
    xs: 9,
    className: classes.nowrap
  }, /*#__PURE__*/_react.default.createElement(_Typography.default, {
    gutterBottom: true
  }, song.year)), /*#__PURE__*/_react.default.createElement(_Grid.default, {
    item: true,
    xs: 3,
    className: classes.nowrap
  }, /*#__PURE__*/_react.default.createElement(_Typography.default, {
    gutterBottom: true
  }, "Genre")), /*#__PURE__*/_react.default.createElement(_Grid.default, {
    item: true,
    xs: 9,
    className: classes.nowrap
  }, /*#__PURE__*/_react.default.createElement(_Typography.default, {
    gutterBottom: true
  }, song.genre))), /*#__PURE__*/_react.default.createElement(_Grid.default, {
    container: true,
    item: true,
    xs: 12
  }, /*#__PURE__*/_react.default.createElement(_Grid.default, {
    item: true,
    xs: 4
  }, /*#__PURE__*/_react.default.createElement(_Button.default, {
    size: "small",
    color: song.starred ? 'primary' : 'default',
    onClick: onStarToggle,
    style: {
      display: 'block',
      margin: 'auto'
    }
  }, /*#__PURE__*/_react.default.createElement(_Stars.default, null))), /*#__PURE__*/_react.default.createElement(_Grid.default, {
    item: true,
    xs: 1
  }), /*#__PURE__*/_react.default.createElement(_Grid.default, {
    item: true,
    xs: 4,
    title: "Your Rating"
  }, /*#__PURE__*/_react.default.createElement(_Rating.default, {
    name: 'rating-' + song.id,
    value: song.userRating || 0,
    precision: 1,
    onChange: onChangeRating
  })), /*#__PURE__*/_react.default.createElement(_Grid.default, {
    item: true,
    xs: 3,
    title: "Average Rating"
  }, /*#__PURE__*/_react.default.createElement(_Rating.default, {
    name: 'avg-' + song.id,
    value: song.averageRating || 0,
    readOnly: true
  }))))), /*#__PURE__*/_react.default.createElement(DialogActions, null, /*#__PURE__*/_react.default.createElement(_Button.default, {
    autoFocus: true,
    onClick: handleClose,
    color: "secondary"
  }, "Close")));
}

var _default = (0, _styles.withStyles)(styles)(NowPlayingDialog);

exports.default = _default;