/**
 * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
 * For licensing, see LICENSE.md or http://ckeditor.com/license
 */

/**
 * @fileOverview The "div" plugin. It wraps the selected block level elements with a 'div' element with specified styles and attributes.
 *
 */

( function() {
	CKEDITOR.plugins.add( 'figure', {
		requires: 'dialog',
		// jscs:disable maximumLineLength
		lang: 'af,ar,bg,bn,bs,ca,cs,cy,da,de,el,en,en-au,en-ca,en-gb,eo,es,et,eu,fa,fi,fo,fr,fr-ca,gl,gu,he,hi,hr,hu,id,is,it,ja,ka,km,ko,ku,lt,lv,mk,mn,ms,nb,nl,no,pl,pt,pt-br,ro,ru,si,sk,sl,sq,sr,sr-latn,sv,th,tr,tt,ug,uk,vi,zh,zh-cn', // %REMOVE_LINE_CORE%
		// jscs:enable maximumLineLength
		icons: 'createfigure', // %REMOVE_LINE_CORE%
//		hidpi: true, // %REMOVE_LINE_CORE%
		init: function( editor ) {
			if ( editor.blockless )
				return;

			var lang = editor.lang.figure,
				allowed = 'figure(*)';

			if ( CKEDITOR.dialog.isTabEnabled( editor, 'editfigure', 'advanced' ) )
				allowed += ';figure[dir,id,lang,title]{*}';

			editor.addCommand( 'createfigure', new CKEDITOR.dialogCommand( 'createfigure', {
				allowedContent: allowed,
				requiredContent: 'figure',
				contextSensitive: true,
				refresh: function( editor, path ) {
					var context = editor.config.figure_wrapTable ? path.root : path.blockLimit;
					this.setState( 'figure' in context.getDtd() ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED );
				}
			} ) );

			editor.addCommand( 'editfigure', new CKEDITOR.dialogCommand( 'editfigure', { requiredContent: 'figure' } ) );
			editor.addCommand( 'removefigure', {
				requiredContent: 'figure',
				exec: function( editor ) {
					var selection = editor.getSelection(),
						ranges = selection && selection.getRanges(),
						range,
						bookmarks = selection.createBookmarks(),
						walker,
						toRemove = [];

					function findDiv( node ) {
						var figure = CKEDITOR.plugins.figure.getSurroundDiv( editor, node );
						if ( figure && !figure.data( 'cke-figure-added' ) ) {
							toRemove.push( figure );
							figure.data( 'cke-figure-added' );
						}
					}

					for ( var i = 0; i < ranges.length; i++ ) {
						range = ranges[ i ];
						if ( range.collapsed )
							findDiv( selection.getStartElement() );
						else {
							walker = new CKEDITOR.dom.walker( range );
							walker.evaluator = findDiv;
							walker.lastForward();
						}
					}

					for ( i = 0; i < toRemove.length; i++ )
						toRemove[ i ].remove( true );

					selection.selectBookmarks( bookmarks );
				}
			} );

			editor.ui.addButton && editor.ui.addButton( 'CreateFigure', {
				label: lang.toolbar,
				command: 'createfigure',
				toolbar: 'figure,50'
			} );

			if ( editor.addMenuItems ) {
				editor.addMenuItems( {
					editfigure: {
						label: lang.edit,
						command: 'editfigure',
						group: 'figure',
						order: 1
					},

					removefigure: {
						label: lang.remove,
						command: 'removefigure',
						group: 'figure',
						order: 5
					}
				} );

				if ( editor.contextMenu ) {
					editor.contextMenu.addListener( function( element ) {
						if ( !element || element.isReadOnly() )
							return null;


						if ( CKEDITOR.plugins.figure.getSurroundDiv( editor ) ) {
							return {
								editfigure: CKEDITOR.TRISTATE_OFF,
								removefigure: CKEDITOR.TRISTATE_OFF
							};
						}

						return null;
					} );
				}
			}

			CKEDITOR.dialog.add( 'createfigure', this.path + 'dialogs/figure.js' );
			CKEDITOR.dialog.add( 'editfigure', this.path + 'dialogs/figure.js' );
		}
	} );

	CKEDITOR.plugins.figure = {
		getSurroundDiv: function( editor, start ) {
			var path = editor.elementPath( start );
			return editor.elementPath( path.blockLimit ).contains( function( node ) {
				// Avoid read-only (i.e. contenteditable="false") figures (#11083).
				return node.is( 'figure' ) && !node.isReadOnly();
			}, 1 );
		}
	};
} )();
