'use strict'

/**
 * This module expose the plugin event handlers
 * @module
 * @name eventHandlers
 */

// var HeaderFooterFactory = require('./classes/HeaderFooterFactory')
var uiUtils = require('./utils/ui')
// var menuItems = require('./components/menu-items')
// var $ = window.jQuery

module.exports = {
  'Init': {
    setBodies: setBodies,
    setStackedLayout: setStackedLayout,
    setPageLayout: setPageLayout,
    applyDefaultFormat: applyDefaultFormat
  },
  'NodeChange': {},
  'SetContent': {},
  'BeforeSetContent': {},
  'Focus': {
    enterHeadFoot: enterHeadFoot
  },
  'Blur': {
    leaveHeadFoot: leaveHeadFoot
  },
  'Focus Blur Paste SetContent NodeChange': {
    applyCurrentFormat: applyCurrentFormat
  }
}

function setBodies (evt) {
  var editor = evt.target
  this.documentBodies.mce[this.type] = editor.getBody()
  if (!this.documentBodies.app) {
    this.documentBodies.app = window.document.body
  }
  this.documentBody = editor.getBody()
}

function setStackedLayout (evt) {
  uiUtils.mapMceLayoutElements(this.bodyClass, this.stackedLayout)
}

function setPageLayout (evt) {
  if (this.isMaster) {
    uiUtils.mapPageLayoutElements(this.pageLayout)
  }
}

function enterHeadFoot (evt) {
  this.enable()
}

function leaveHeadFoot (evt) {
  this.disable()
}

function applyCurrentFormat (evt) {
  var that = this
  if (this.currentFormat) {
    // console.info('evt', that.type, evt.type)
    if (evt.type === 'blur' || evt.type === 'focus') {
      setTimeout(function () {
        that.currentFormat.applyToPlugin(that)
      }, 200)
    } else {
      that.currentFormat.applyToPlugin(that)
    }
  }
}

function applyDefaultFormat (evt) {
  this.currentFormat = this.defaultFormat
  this.currentFormat.applyToPlugin(this)
}

'use strict'

/**
 * This module expose the plugin event handlers
 * @module
 * @name eventHandlers
 */

// var HeaderFooterFactory = require('./classes/HeaderFooterFactory')
var uiUtils = require('./utils/ui')
// var menuItems = require('./components/menu-items')
// var $ = window.jQuery

module.exports = {
  'Init': {
    setBodies: setBodies,
    setStackedLayout: setStackedLayout,
    setPageLayout: setPageLayout,
    applyDefaultFormat: applyDefaultFormat
  },
  'NodeChange': {},
  'SetContent': {},
  'BeforeSetContent': {},
  'Focus': {
    enterHeadFoot: enterHeadFoot
  },
  'Blur': {
    leaveHeadFoot: leaveHeadFoot
  },
  'Focus Blur Paste SetContent NodeChange HeadersFooters:SetFormat': {
    applyCurrentFormat: applyCurrentFormat
  }
}

function setBodies (evt) {
  var editor = evt.target
  this.documentBodies.mce[this.type] = editor.getBody()
  if (!this.documentBodies.app) {
    this.documentBodies.app = window.document.body
  }
  this.documentBody = editor.getBody()
}

function setStackedLayout (evt) {
  uiUtils.mapMceLayoutElements(this.bodyClass, this.stackedLayout)
}

function setPageLayout (evt) {
  if (this.isMaster) {
    uiUtils.mapPageLayoutElements(this.pageLayout)
  }
}

function enterHeadFoot (evt) {
  this.enable()
}

function leaveHeadFoot (evt) {
  this.disable()
}

function applyCurrentFormat (evt) {
  var that = this
  if (this.currentFormat) {
    // console.info('evt', that.type, evt.type)
    if (evt.type === 'blur' || evt.type === 'focus') {
      setTimeout(function () {
        that.currentFormat.applyToPlugin(that)
      }, 200)
    } else {
      that.currentFormat.applyToPlugin(that)
    }
  }
}

function applyDefaultFormat (evt) {
  this.currentFormat = this.defaultFormat
  this.currentFormat.applyToPlugin(this)
}

call applyCurrentFormat() on HeadersFooters:SetFormat