import React from 'react';
import cn from 'classnames';

import RetinaImage from 'react-retina-image';

import { DragSource, DropTarget } from 'react-dnd';

import styles from 'cem/styles/ui/card';
import UI from 'cem/components/ui';
const { Form } = UI;

const Image = (props) => (
  // <UI.Image className={styles.image} src={props.src} height="256" title="" alt="" />
  <RetinaImage className={styles.image} src={`${props.src}-256`} height="256" title="" alt="" />
);

const sourceSpec = {
  beginDrag: (props) => ({
    id: props.id.value,
    index: props.index,
  }),
  isDragging: (props, monitor) => props.id.value === monitor.getItem().id,
};

const targetSpec = {
  hover(props, monitor) {
    const dragIndex = monitor.getItem().index;
    const hoverIndex = props.index;
    if (dragIndex === hoverIndex) {
      return;
    }
    monitor.getItem().index = hoverIndex;
    props.reorder(dragIndex, hoverIndex);
  },
};

const collectSource = (connect, monitor) => ({
  connectDragSource: connect.dragSource(),
  isDragging: monitor.isDragging(),
});

const collectTarget = (connect) => ({
  connectDropTarget: connect.dropTarget(),
});

const PhotosDescription = (props) => (
  <div className={styles.cardWrapper}>
    {props.comment && (
      <Form.Group>
        <Form.Input type="text" placeholder="Комментарий" block {...props.comment} />
      </Form.Group>
    )}
    <Form.Group>
      <Form.Label block>
        <Form.Input type="checkbox" {...props.isPublic} /> на сайте {props.id.value}
      </Form.Label>
    </Form.Group>
  </div>
);

// export default (cardType) => DropTarget(cardType, targetSpec, collectTarget)(DragSource(cardType, sourceSpec, collectSource)(
//   (props) => {
//     const { connectDropTarget, connectDragSource, isDragging } = props;
//     return connectDragSource(connectDropTarget(
//       <div className={cn(styles.cardPhotos, { [styles.isDragging]: isDragging })}>
//         <Image src="//images.unsplash.com/photo-1443453489887-98f56bc5bb38?fit=crop&fm=jpg&h=750&ixlib=rb-0.3.5&q=80&w=1300" block />
//         <PhotosDescription id={props.id} />
//       </div>
//     ));
//   }
// ));

const Card = (props) => {
  const { connectDropTarget, connectDragSource, isDragging, url } = props;
  return connectDragSource(connectDropTarget(
    <div className={cn(styles.cardPhotos, { [styles.isDragging]: isDragging })}>
      <Image src={url.value} block />
      <PhotosDescription {...props} />
    </div>
  ));
};

export const PhotoCard = DropTarget(`properties.photos.card`, targetSpec, collectTarget)(DragSource(`properties.photos.card`, sourceSpec, collectSource)(Card));
export const LayoutCard = DropTarget(`properties.photos.cardLayout`, targetSpec, collectTarget)(DragSource(`properties.photos.cardLayout`, sourceSpec, collectSource)(Card));
