import _ from 'lodash'

import { TOCProvider } from 'substance'

class NotesProvider extends TOCProvider {
  computeEntries () {
    const docMain = this.getDocument()
    const nodesMain = docMain.getNodes()

    const docMini = this.config.miniEditorSession.document
    let nodesMini = ''
    if (docMini) nodesMini = docMini.getNodes()

    if (docMini)console.log('paragraphs', nodesMini)

    // get all notes from the document
    const notesMain = _.pickBy(nodesMain, function (value, key) {
      return value.type === 'note'
    })
    const notesMini = _.pickBy(nodesMini, function (value, key) {
      return value.type === 'isolated-note'
    })
    const entriesMain = this.sortNodes(notesMain)
    const entriesMini = this.sortNodesMini(notesMini)

    // console.log(entriesMain, entriesMini)
    const entries = {
      main: entriesMain,
      mini: entriesMini
    }

    return entries
  }

  sortNodes (nodes) {
    let notes = _.clone(nodes)
    const doc = this.getDocument()
    const container = doc.get('body')

    // sort notes by
    //   the index of the containing block
    //   their position within that block

    notes = _.map(notes, function (note) {
      const blockId = note.path[0]
      const blockPosition = container.getPosition(blockId)
      const nodePosition = note.start.offset

      return {
        id: note.id,
        'note-content': note['note-content'],
        blockPosition: blockPosition,
        nodePosition: nodePosition
      }
    })

    return _.sortBy(notes, ['blockPosition', 'nodePosition'])
  }

  sortNodesMini (nodes) {
    let notes = _.clone(nodes)
    if (this.config.miniEditorSession === '') return

    // const doc = this.config.miniEditorSession.document
    notes = _.map(notes, function (note) {
      // const blockId = note.path[0]
      // const blockPosition = container.getPosition(blockId)
      // const nodePosition = note.start.offset

      return {
        id: note.id,
        content: note.content,
        parentNoteId: note.parentNoteId
        // blockPosition: blockPosition,
        // nodePosition: nodePosition,
        // node: note
      }
    })
    return notes
    // return _.sortBy(notes, ['blockPosition', 'nodePosition'])
  }

  createShowNote (note) {

    if (note['note-content'] !== '') {
      console.log('empty')
      return
    }

    const notes = this.computeEntries()

    if (notes.mini.length === 0) {
      this.createIsolatedNote(note)
      return
    }

    _.forEach(notes.main, function (mainNote) {
      _.forEach(notes.mini, function (miniNote) {
        if (miniNote.parentNoteId === mainNote.id) {
          return false
        } else {
          this.createIsolatedNote(note)
        }
      }.bind(this))
    }.bind(this))
  }

  createNodeData (tx, note) {
    return {
      'type': 'isolated-note',
      'content': note['note-content'],
      'parentNoteId': note.id,
      'index': 1
    }
  }

  createIsolatedNote (note) {
    this.config.miniEditorSession.transaction(function (tx) {
      let nodeData = this.createNodeData(tx, note)
      tx.insertBlockNode(nodeData)
    }.bind(this))
  }

}

NotesProvider.tocTypes = ['note']

export default NotesProvider