import { expect } from 'chai'

import {
  backtrackingNextQuestion,
  editResponse,
  loadResumeData,
  previousQuestion,
  setCurrentSessionItemPosition,
  updateIsTaking,
  updateQuizSessionStatusOnPageLoad,
  updateTimerInfo,
  updateTimerMinimized,
  updateQuizSubmitFlag
} from '../taking'

import {
  BACKTRACKING_NEXT_QUESTION,
  LOAD_RESUME_DATA,
  PREVIOUS_QUESTION,
  RESPONSE,
  UPDATE_IS_TAKING,
  UPDATE_TIMER_INFO,
  SET_CURRENT_SESSION_ITEM_POSITION,
  UPDATE_QUIZ_SESSION_STATUS_ON_PAGE_LOAD,
  UPDATE_TIMER_MINIMIZED,
  UPDATE_QUIZ_SUBMIT_FLAG
} from '@instructure/quiz-common/lib/constants'

import SessionItem from '../../records/SessionItem'

const sessionItem = new SessionItem({
  position: '1',
  item: { id: '1' }
})

describe('taking actions', () => {
  describe('update quiz session status when started', () => {
    it('returns the correct action', () => {
      expect(updateQuizSessionStatusOnPageLoad('in_progress')).to.eql({
        type: UPDATE_QUIZ_SESSION_STATUS_ON_PAGE_LOAD,
        payload: {
          quizSessionStatusOnPageLoad: 'in_progress'
        }
      })
    })
  })

  describe('set current session item position', () => {
    it('returns the correct action', () => {
      expect(setCurrentSessionItemPosition(5)).to.eql({
        type: SET_CURRENT_SESSION_ITEM_POSITION,
        payload: {
          position: 5
        }
      })
    })
  })

  describe('update timerMinimized', () => {
    it('returns the correct actions', () => {
      expect(updateTimerMinimized(true)).to.eql({
        type: UPDATE_TIMER_MINIMIZED,
        payload: {
          timerMinimized: true
        }
      })

      expect(updateTimerMinimized(false)).to.eql({
        type: UPDATE_TIMER_MINIMIZED,
        payload: {
          timerMinimized: false
        }
      })
    })
  })

  describe('update isTaking', () => {
    it('returns the correct actions', () => {
      expect(updateIsTaking(true)).to.eql({
        type: UPDATE_IS_TAKING,
        payload: {
          isTaking: true
        }
      })
    })

    describe('previous question', () => {
      it('returns the correct action', () => {
        expect(previousQuestion()).to.eql({
          type: PREVIOUS_QUESTION,
          payload: {}
        })
      })
    })

    describe('backtracking next question', () => {
      it('returns the correct action', () => {
        expect(backtrackingNextQuestion()).to.eql({
          type: BACKTRACKING_NEXT_QUESTION,
          payload: {}
        })
      })
    })

    describe('update isTaking', () => {
      it('returns the correct actions', () => {
        expect(updateIsTaking(true)).to.eql({
          type: UPDATE_IS_TAKING,
          payload: {
            isTaking: true
          }
        })

        expect(updateIsTaking(false)).to.eql({
          type: UPDATE_IS_TAKING,
          payload: {
            isTaking: false
          }
        })
      })
    })

    describe('updating remaining time', () => {
      it('returns the correct action without timeRecording', () => {
        const timeRemaining = {timeLeftInSeconds: 3, timerRunning: true}
        expect(updateTimerInfo(timeRemaining)).to.eql({
          type: UPDATE_TIMER_INFO,
          payload: {
            timeLeftInSeconds: 3,
            timerRunning: true,
            timeRecording: false
          }
        })
      })

      it('returns the correct action', () => {
        const timeRemaining = {timeLeftInSeconds: 3, timerRunning: true, timeRecording: true}
        expect(updateTimerInfo(timeRemaining)).to.eql({
          type: UPDATE_TIMER_INFO,
          payload: {
            timeLeftInSeconds: 3,
            timerRunning: true,
            timeRecording: true
          }
        })
      })
    })

    describe('updating submitting flag', () => {
      it('returns the correct action', () => {
        expect(updateQuizSubmitFlag(true)).to.eql({
          type: UPDATE_QUIZ_SUBMIT_FLAG,
          payload: {
            value: true
          }
        })

        expect(updateQuizSubmitFlag(false)).to.eql({
          type: UPDATE_QUIZ_SUBMIT_FLAG,
          payload: {
            value: false
          }
        })

        expect(updateQuizSubmitFlag(null)).to.eql({
          type: UPDATE_QUIZ_SUBMIT_FLAG,
          payload: {
            value: null
          }
        })
      })
    })

    describe('editing a Response action creators', () => {
      it('returns responses keyed by position', () => {
        const editResponseAction = editResponse(sessionItem, 'hello')

        expect(editResponseAction).to.have.property('type', RESPONSE)
        expect(editResponseAction.payload).to.have.property('position', '1')
        expect(editResponseAction.payload).to.have.property('itemId', '1')
        expect(editResponseAction.payload.userResponse).to.be.deep.equal({value: 'hello'})
      })
    })

    describe('load resume data', () => {
      it('can load multiple responses', () => {
        const respOne = {
          position: 1,
          userResponse: {value: 'hey'},
          itemId: 1
        }
        const respTwo = {
          position: 2,
          userResponse: {value: 'hello'},
          itemId: 2
        }
        const loadResumeAction = loadResumeData([respOne, respTwo])
        expect(loadResumeAction).to.have.property('type', LOAD_RESUME_DATA)
        expect(loadResumeAction.payload).to.include(respOne)
        expect(loadResumeAction.payload).to.include(respTwo)
      })
    })
  })
})
