import { decode } from 'blurhash';
import classNames from 'classnames';
import Icon from 'mastodon/components/icon';
import { autoPlayGif, displayMedia } from 'mastodon/initial_state';
import { isIOS } from 'mastodon/is_mobile';
import PropTypes from 'prop-types';
import React from 'react';
import ImmutablePropTypes from 'react-immutable-proptypes';
import ImmutablePureComponent from 'react-immutable-pure-component';
export default class MediaItem extends ImmutablePureComponent {
  static propTypes = {
    attachment: ImmutablePropTypes.map.isRequired,
    displayWidth: PropTypes.number.isRequired,
    onOpenMedia: PropTypes.func.isRequired,
  };
  state = {
    visible: displayMedia !== 'hide_all' && !this.props.attachment.getIn(['status', 'sensitive']) || displayMedia === 'show_all',
    loaded: false,
  };
  componentDidMount () {
    if (this.props.attachment.get('blurhash')) {
      this._decode();
    }
  }
  componentDidUpdate (prevProps) {
    if (prevProps.attachment.get('blurhash') !== this.props.attachment.get('blurhash') && this.props.attachment.get('blurhash')) {
      this._decode();
    }
  }
  _decode () {
    const hash   = this.props.attachment.get('blurhash');
    const pixels = decode(hash, 32, 32);
    if (pixels) {
      const ctx       = this.canvas.getContext('2d');
      const imageData = new ImageData(pixels, 32, 32);
      ctx.putImageData(imageData, 0, 0);
    }
  }
  setCanvasRef = c => {
    this.canvas = c;
  }
  handleImageLoad = () => {
    this.setState({ loaded: true });
  }
  handleMouseEnter = e => {
    if (this.hoverToPlay()) {
      e.target.play();
    }
  }
  handleMouseLeave = e => {
    if (this.hoverToPlay()) {
      e.target.pause();
      e.target.currentTime = 0;
    }
  }
  hoverToPlay () {
    return !autoPlayGif && ['gifv', 'video'].indexOf(this.props.attachment.get('type')) !== -1;
  }
  handleClick = e => {
    if (e.button === 0 && !(e.ctrlKey || e.metaKey)) {
      e.preventDefault();
      if (this.state.visible) {
        this.props.onOpenMedia(this.props.attachment);
      } else {
        this.setState({ visible: true });
      }
    }
  }
  render () {
    const { attachment, displayWidth } = this.props;
    const { visible, loaded } = this.state;
    const width  = `${Math.floor((displayWidth - 4) / 3) - 4}px`;
    const height = width;
    const status = attachment.get('status');
    const title = status.get('spoiler_text') || attachment.get('description');
    let thumbnail = '';
    let icon;
    if (attachment.get('type') === 'unknown') {
      // Skip
    } else if (attachment.get('type') === 'audio') {
      thumbnail = (
        
          
      );
    } else if (['gifv', 'video'].indexOf(attachment.get('type')) !== -1) {
      const autoPlay = !isIOS() && autoPlayGif;
      const label    = attachment.get('type') === 'video' ?