import { List } from 'immutable'
import InteractionType from 'quiz-interactions/lib/records/InteractionType'

import CommonSidebarItem from '../../../../../../common/components/layout/sidebar/SidebarItem'
import DragHandle from '../../../../../../common/components/shared/drag_and_drop/DragHandle'
import QuizEntry from '../../../../../../common/records/QuizEntry'
import SidebarItem from '../presenter'

import Item from '../../../../../../common/records/Item'

describe('SidebarItem Building Presenter', () => {
  const item = new Item({id: '22', itemBody: '<div>Hello</div>'})
  const intType = new InteractionType({slug: 'choice', name: 'Multiple Choice'})
  item.getInteractionType = sinon.stub().returns(intType)

  const quizEntry = new QuizEntry({position: 2})

  const props = {
    item: item,
    quizEntry: quizEntry,
    quizEntries: List([quizEntry]),
    pointsPossible: 2,
    sidebarOpen: false,
    sidebarActions: {},
    itemsCollectionSize: 3
  }

  const testbed = createTestbed(wrapInDragAndDropTestContext(withContext(SidebarItem)), props)

  shouldRenderComponent(
    testbed, {}, SidebarItem
  )

  it('renders common sidebarItem', () => {
    testbed.render()
    expect(testbed.findChildrenByType(CommonSidebarItem).length).to.eq(1)
  })

  it('should include a drag handle if draggable', () => {
    testbed.render({sidebarOpen: true, connectDragSource: (C) => C, connectDragPreview: (C) => C})

    const dragHandles = testbed.findChildrenByType(DragHandle)
    expect(dragHandles).to.have.length(1)
  })

  it('should not include a drag handle not draggable', () => {
    testbed.render({sidebarOpen: false})

    const dragHandles = testbed.findChildrenByType(DragHandle)
    expect(dragHandles).to.have.length(0)
  })

  // TODO - reenable these once a better testing strategy is discovered.
  xdescribe('building sidebar drag and drop', () => {
    const dragAndTestWithMonitor = (testCb) => {
      testbed.render()
      const rootElement = testbed.subject
      const sidebarItem = testbed.findChildrenByType(CommonSidebarItem)[0]
      const handlerID = sidebarItem.getHandlerId()
      const manager = rootElement.getManager()
      const dndBackend = manager.getBackend()
      const monitor = manager.getMonitor()

      dndBackend.simulateBeginDrag([handlerID], {
        clientOffset: {x: 0, y: 100},
        getSourceClientOffset: () => ({x: 0, y: 50})
      })

      testCb(monitor)

      dndBackend.simulateEndDrag([handlerID])
    }

    it('should getItem that is being dragged', () => {
      dragAndTestWithMonitor((monitor) => {
        const draggedItem = monitor.getItem()
        expect(draggedItem.id).to.be.equal('3')
      })
    })

    it('should get location of item dropped', () => {
      dragAndTestWithMonitor((monitor) => {
        expect(monitor.getClientOffset()).to.be.deep.equal({x: 0, y: 100})
      })
    })
  })

  describe('a11y tests', () => {
    it('should meet a11y standards', (done) => {
      testbed.render()
      testbed.checkA11yStandards(done)
    })
  })
})
