'use strict'

var eventHandlers = require('../event-handlers')
var units = require('../units')
var uiComponents = require('./components')
var findNodes = require('../dom/find-nodes')
// var uiHelpers = require('./helpers')

var $ = window.jQuery

module.exports = {
  openMainWinFunction: openMainWinFunction
}

/**
 * Make the openMainWin function as a closure
 * @method
 * @param {Editor} editor The tinymce active editor instance
 * @returns {function} openMainWin The openMainWin closure function
 */
function openMainWinFunction (editor) {
  return openMainWin

  /**
   * Open the main paragraph properties window
   * @function
   * @inner
   * @returns {undefined}
   */
  function openMainWin () {
    var paragraphes = findNodes.getSelectedParagraphes(editor.selection)
    var paragraphStyleData = {}

    var propertiesWithUnits = [
      ['text-indent', 'textIndent'],
      ['line-height', 'lineHeight'],
      ['padding-top', 'paddingTop'],
      ['padding-right', 'paddingRight'],
      ['padding-bottom', 'paddingBottom'],
      ['padding-left', 'paddingLeft'],
      ['margin-top', 'marginTop'],
      ['margin-right', 'marginRight'],
      ['margin-bottom', 'marginBottom'],
      ['margin-left', 'marginLeft'],
      ['border-width', 'borderWidth']
    ]

    var propertiesWithoutUnits = [
      ['border-style', 'borderStyle'],
      ['border-color', 'borderColor']
    ]

    $.each(propertiesWithUnits, setEachFormPropertyWithUnit)
    $.each(propertiesWithoutUnits, setEachFormPropertyWithoutUnit)

    var generalTab = uiComponents.createGeneralTab(paragraphStyleData)
    var spacingsTab = uiComponents.createSpacingTab(paragraphStyleData)
    var bordersTab = uiComponents.createBordersTab(editor, paragraphStyleData)

    editor.windowManager.open({
      bodyType: 'tabpanel',
      title: 'Paragraph properties',
      body: [ generalTab, spacingsTab, bordersTab ],
      data: paragraphStyleData,
      onsubmit: eventHandlers.processAllChangesOnMainWinSubmit(editor, paragraphes)
    })

    function setEachFormPropertyWithUnit (i, item) {
      units.setFormPropertyWithUnit(editor.dom, paragraphes, paragraphStyleData, item[0], item[1])
    }

    function setEachFormPropertyWithoutUnit (i, item) {
      units.setFormPropertyWithoutUnit(editor.dom, paragraphes, paragraphStyleData, item[0], item[1])
    }
  }
}

@@ -697,24 +697,335 @@
 ainWin () %7B%0A
+    // disable visualblock option if enabled%0A    // (should be re-enabled after)%0A    var visualblocksClass = 'mce-visualblocks'%0A    var $body = $(editor.getBody())%0A    var hadVisualblocksClass = $body.hasClass(visualblocksClass)%0A    if (hadVisualblocksClass) %7B%0A      $body.removeClass(visualblocksClass)%0A    %7D%0A%0A
     var para
@@ -2254,48 +2254,17 @@
 it: 
-eventHandlers.processAllChangesO
+o
 nMainWin
 Subm
@@ -2263,35 +2263,17 @@
 nWin
+dow
 Submit
-(editor, paragraphes)
 %0A   
@@ -2599,15 +2599,303 @@
 %5D)%0A    %7D
+%0A%0A    function onMainWindowSubmit () %7B%0A      // re-enable visualblocks if it was defined before%0A      if (hadVisualblocksClass) %7B%0A        $body.addClass(visualblocksClass)%0A      %7D%0A      // process all changes%0A      eventHandlers.processAllChangesOnMainWinSubmit(editor, paragraphes)%0A    %7D
 %0A  %7D%0A%7D%0A

Fix #43 visualblocks disturb parag spacings (#47)