import _regeneratorRuntime from "@babel/runtime/regenerator";
import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
import _inherits from "@babel/runtime/helpers/inherits";
import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
import _defineProperty from "@babel/runtime/helpers/defineProperty";

var _dec, _dec2, _class, _class2, _temp;

import React, { PureComponent } from 'react';
import { withTheme } from '@material-ui/core/styles';
import PropTypes from 'prop-types';
import { style } from 'typestyle';
import { DragSource } from 'react-dnd';
import Paper from '@material-ui/core/Paper';
import IconButton from '@material-ui/core/IconButton';
import EditorDragHandle from '@material-ui/icons/DragHandle';
import ActionSettings from '@material-ui/icons/Settings';
import Filename from './Filename';
import { PreferenceDialog } from '../../FileDialog/';
import DragTypes from '../../utils/dragTypes';
import withFiles from '../../store/withFiles';
var cn = {
  container: style({
    flex: '1 1 auto',
    display: 'flex',
    justifyContent: 'space-between',
    alignItems: 'center'
  })
};

var getCn = function getCn(props) {
  return {
    root: style({
      marginTop: 4,
      marginRight: 8,
      transition: props.theme.transitions.create()
    }),
    card: style({
      boxSizing: 'border-box',
      height: 40,
      display: 'flex',
      justifyContent: 'space-between',
      alignItems: 'center',
      paddingLeft: props.theme.spacing.unit * 2,
      borderTopRightRadius: props.isSelected ? 0 : 2,
      borderBottomRightRadius: props.isSelected ? 0 : 2,
      opacity: props.isDragging ? 0.5 : 1,
      transition: props.theme.transitions.create()
    }),
    dragHandle: style({
      flex: '0 0 auto',
      cursor: 'move',
      width: props.theme.spacing.unit * 3,
      height: props.theme.spacing.unit * 3,
      marginRight: props.theme.spacing.unit
    })
  };
};

var FileCard = (_dec = withTheme(), _dec2 = withFiles({
  watch: ''
}), _dec(_class = _dec2(_class = (_temp = _class2 =
/*#__PURE__*/
function (_PureComponent) {
  _inherits(FileCard, _PureComponent);

  function FileCard() {
    var _getPrototypeOf2;

    var _this;

    _classCallCheck(this, FileCard);

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _possibleConstructorReturn(this, (_getPrototypeOf2 = _getPrototypeOf(FileCard)).call.apply(_getPrototypeOf2, [this].concat(args)));

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "handleConfirmSettings",
    /*#__PURE__*/
    function () {
      var _ref = _asyncToGenerator(
      /*#__PURE__*/
      _regeneratorRuntime.mark(function _callee(event) {
        var _this$props, file, openFileDialog, change;

        return _regeneratorRuntime.wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                event.stopPropagation();
                _this$props = _this.props, file = _this$props.file, openFileDialog = _this$props.openFileDialog;
                _context.next = 4;
                return openFileDialog(PreferenceDialog, {
                  content: file
                });

              case 4:
                change = _context.sent;

                if (change) {
                  _this.props.deleteFiles([file]);

                  _this.props.putFile(file.set(change));
                }

              case 6:
              case "end":
                return _context.stop();
            }
          }
        }, _callee, this);
      }));

      return function (_x) {
        return _ref.apply(this, arguments);
      };
    }());

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "handleNameChange", function (event, name) {
      var file = _this.props.file;

      _this.props.deleteFiles([file]);

      return _this.props.putFile(file.rename(name));
    });

    return _this;
  }

  _createClass(FileCard, [{
    key: "render",
    value: function render() {
      var dcn = getCn(this.props);
      var _this$props2 = this.props,
          file = _this$props2.file,
          handleFileSelect = _this$props2.handleFileSelect,
          connectDragSource = _this$props2.connectDragSource,
          connectDragPreview = _this$props2.connectDragPreview;
      return connectDragPreview(React.createElement("div", {
        className: dcn.root
      }, React.createElement(Paper, {
        onClick: function onClick() {
          return handleFileSelect(file);
        },
        className: dcn.card
      }, connectDragSource(React.createElement("div", {
        className: dcn.dragHandle
      }, React.createElement(EditorDragHandle, null))), React.createElement("div", {
        className: cn.container
      }, React.createElement(Filename, {
        file: file,
        onChange: this.handleNameChange
      })), React.createElement(IconButton, {
        onClick: this.handleConfirmSettings
      }, React.createElement(ActionSettings, null)))));
    }
  }]);

  return FileCard;
}(PureComponent), _defineProperty(_class2, "propTypes", {
  theme: PropTypes.object.isRequired,
  file: PropTypes.object.isRequired,
  handleFileSelect: PropTypes.func.isRequired,
  openFileDialog: PropTypes.func.isRequired,
  connectDragSource: PropTypes.func.isRequired,
  connectDragPreview: PropTypes.func.isRequired,
  isDragging: PropTypes.bool.isRequired,
  // Injected by withFiles (for JSX)
  putFile: PropTypes.func.isRequired,
  deleteFiles: PropTypes.func.isRequired
}), _temp)) || _class) || _class);
var spec = {
  beginDrag: function beginDrag(props) {
    return {
      files: [props.file]
    };
  }
};

var collect = function collect(connect, monitor) {
  return {
    connectDragSource: connect.dragSource(),
    connectDragPreview: connect.dragPreview(),
    isDragging: monitor.isDragging()
  };
};

export default DragSource(DragTypes.File, spec, collect)(FileCard);