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

import { honorsSidebarProps } from '../../../../../../tests/util/sharedExamples/honorsSidebarProps'

import { wrapInDragAndDropTestContext } from '../../../../../../tests/util/dndTestUtils'
import Item from '../../../../../common/records/Item'
import Quiz from '../../../../../common/records/Quiz'
import QuizEntry from '../../../../../common/records/QuizEntry'

import Sidebar, { mapStateToProps, mapDispatchToProps } from '../index'
import SidebarPresenter from '../presenter'

describe('Building Index Sidebar', () => {
  const intType = new InteractionType({})
  const itemA = new Item({id: '5', position: 4})
  const itemB = new Item({id: '6', position: 8})
  sinon.stub(itemA, 'getInteractionType').returns(intType)
  sinon.stub(itemB, 'getInteractionType').returns(intType)
  const quizEntryA = new QuizEntry({id: '3'})
  const quizEntryB = new QuizEntry({id: '8'})
  sinon.stub(quizEntryA, 'getEntry').returns(itemA)
  sinon.stub(quizEntryB, 'getEntry').returns(itemB)
  const quizEntries = List([
    quizEntryA, quizEntryB
  ])
  const moveQuizEntryStub = sinon.stub()
  const moveTemporaryQuizEntryStub = sinon.stub()
  const newHoverPositionStub = sinon.stub()
  const scrollToItemStub = sinon.stub()

  const props = {
    connectDropTarget: (Component) => (Component), // inserted by React DnD
    isOver: false, // inserted by React DnD
    moveQuizEntry: moveQuizEntryStub, // Used for DND
    moveTemporaryQuizEntry: moveTemporaryQuizEntryStub, // Used for DND
    newHoverPosition: newHoverPositionStub, // Used for DND
    quizId: '1', // Used for DND
    scrollToItem: scrollToItemStub,
    sidebarOpen: true,
    quizEntries
  }

  const state = {
    quizzes: Map({
      activeQuizId: '1',
      '1': new Quiz({
        id: '1',
        quizEntries: List(['42', '24'])
      })
    }),
    quizEntries: Map({
      '24': new QuizEntry({
        id: '24',
        pointsPossible: 9000
      }),
      '42': new QuizEntry({
        id: '42',
        pointsPossible: 1
      })
    })
  }

  const ConnectedSidebar = wrapInDragAndDropTestContext(withContext(Sidebar, state))

  const testbed = createTestbed(ConnectedSidebar, props)

  shouldRenderComponent(testbed, {}, Sidebar)

  honorsSidebarProps(mapStateToProps, mapDispatchToProps, Map(state))

  it('calculates the correct total points possible', () => {
    testbed.render()
    const sidebar = testbed.findChildByType(SidebarPresenter)
    expect(sidebar.props.totalPointsPossible).to.eq(9001)
  })
})
