import React, { ReactElement, useRef, useState } from 'react';
import { EditorState, convertFromRaw } from 'draft-js';
import Editor from '@draft-js-plugins/editor';
import createVideoPlugin from '@draft-js-plugins/video';
import editorStyles from './editorStyles.css';

const videoPlugin = createVideoPlugin();
const { types } = videoPlugin;
const plugins = [videoPlugin];
/* eslint-disable */
const initialState = {
  entityMap: {
    '0': {
      type: types.VIDEOTYPE,
      mutability: 'IMMUTABLE',
      data: {
        src: 'https://www.youtube.com/watch?v=iEPTlhBmwRg',
      },
    },
  },
  blocks: [
    {
      key: '9gm3s',
      text:
        'You can have video in your text field. This is a very rudimentary example, but you can enhance the video plugin with resizing, focus or alignment plugins.',
      type: 'unstyled',
      depth: 0,
      inlineStyleRanges: [],
      entityRanges: [],
      data: {},
    },
    {
      key: 'ov7r',
      text: ' ',
      type: 'atomic',
      depth: 0,
      inlineStyleRanges: [],
      entityRanges: [
        {
          offset: 0,
          length: 1,
          key: 0,
        },
      ],
      data: {},
    },
    {
      key: 'e23a8',
      text: 'See advanced examples further down …',
      type: 'unstyled',
      depth: 0,
      inlineStyleRanges: [],
      entityRanges: [],
      data: {},
    },
  ],
};
/* eslint-enable */
const SimpleVideoEditor = (): ReactElement => {
  const [editorState, setEditorState] = useState(
    EditorState.createWithContent(convertFromRaw(initialState))
  );
  const editor = useRef();

  const onChange = (value): void => {
    setEditorState(value);
  };

  const focus = (): void => {
    editor.current.focus();
  };

  return (
    <div className={editorStyles.editor} onClick={focus}>
      <Editor
        editorState={editorState}
        onChange={onChange}
        plugins={plugins}
        ref={(element) => {
          editor.current = element;
        }}
      />
    </div>
  );
};

export default SimpleVideoEditor;
