import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { Button, Modal } from 'patd';
import translator from '../../translate';

const styles={
  hintDiv:{
    marginTop:10,
  },
  hintIcon:{
    display: 'inline-block',
    height: 8,
    width: 8,
    background: 'rgb(172, 177, 187)',
    marginRight: 5,
  },
}

const DeleteConfirm = (props) => {
  const {
    open, onCancel, confirmLabel, cancelLabel, resourceName, onConfirm,hintText,
    reverseActions,
  } = props;
  const actions = [
    <Button
      label={confirmLabel}
      primary
      key="save"
      onClick={onConfirm}
    />,
    <Button label={cancelLabel} key="cancel" onClick={onCancel} />,
  ];
  if (reverseActions) {
    actions.reverse();
  }
  return (
    <Modal
      size="sm"
      open={open}
      handleClose={onCancel}
      actions={actions}
      title={translator['Delete Confirm']}
    >
      {translator['Confirm to delete $name']({ name: resourceName })}
      {hintText ? <div style={styles.hintDiv}>
        <span style={styles.hintIcon} />
        {hintText}
      </div> :''}
    </Modal>
  );
};

DeleteConfirm.propTypes = {
  open: PropTypes.bool.isRequired,
  onConfirm: PropTypes.func.isRequired,
  onCancel: PropTypes.func.isRequired,
  confirmLabel: PropTypes.string,
  cancelLabel: PropTypes.string,
  resourceName: PropTypes.string,
  reverseActions: PropTypes.bool,
  hintText:PropTypes.string,
};

DeleteConfirm.defaultProps = {
  confirmLabel: translator.Delete,
  cancelLabel: translator.Cancel,
  resourceName: '',
  reverseActions: false,
  hintText:'',
};

class OpenStateManager extends Component {
  static propTypes = {
    reverseActions: PropTypes.bool,
    hintText:PropTypes.string,
  };
  static defaultProps = {
    reverseActions: false,
    hintText:'',
  };
  constructor(props) {
    super(props);
    this.state = {
      open: false,
      onConfirm: null,
    };
  }
  open = (resourceName, callback) => {
    this.setState({ open: true, resourceName, onConfirm: callback });
  };
  close = () => {
    this.setState({ open: false });
  };

  render() {
    return (this.state.open && <DeleteConfirm
      open={this.state.open}
      resourceName={this.state.resourceName}
      onCancel={this.close}
      onConfirm={this.state.onConfirm}
      reverseActions={this.props.reverseActions}
      hintText={this.props.hintText}
    />);
  }
}

export default OpenStateManager;
